MySQL表添加自增列

2018-06-18 01:41:24来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

mysql> create table zc_test2(id int);
Query OK, 0 rows affected (1.37 sec)

mysql> insert into zc_test2 values (10),(9),(8),(7),(6);
Query OK, 5 rows affected (0.04 sec)
Records: 5  Duplicates: 0  Warnings: 0

mysql> select * from zc_test2;
+------+
| id   |
+------+
|   10 |
|    9 |
|    8 |
|    7 |
|    6 |
+------+
5 rows in set (0.00 sec)

mysql> ALTER TABLE zc_test2 add id2 int;
Query OK, 0 rows affected (2.84 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> select * from zc_test2;
+------+------+
| id   | id2  |
+------+------+
|   10 | NULL |
|    9 | NULL |
|    8 | NULL |
|    7 | NULL |
|    6 | NULL |
+------+------+
5 rows in set (0.00 sec)

mysql> alter table zc_test2 change id2 id2 int NOT NULL AUTO_INCREMENT;
ERROR 1075 (42000): Incorrect table definition; there can be only one auto column and it must be defined as a key

mysql> alter table zc_test2 change id2 id2 int NOT NULL AUTO_INCREMENT primary key;
Query OK, 5 rows affected (1.34 sec)
Records: 5  Duplicates: 0  Warnings: 0

mysql> select * from zc_test2;
+------+-----+
| id   | id2 |
+------+-----+
|   10 |   1 |
|    9 |   2 |
|    8 |   3 |
|    7 |   4 |
|    6 |   5 |
+------+-----+
5 rows in set (0.00 sec)

 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:sql 优化的几种方法

下一篇:6.MySQL必知必会之数据过滤-WHERE组合子句