如何通过SQL语句为某个已有字段添加自增属性

更新时间:02-10 教程 由 花心症 分享
如何通过SQL语句为某个已有字段添加自增属性?

mysql> alter table tt change id id int default null;

Query OK, 1 row affected (0.23 sec)

Records: 1 Duplicates: 0 Warnings: 0

mysql> show create table tt;

+-------+----------------------------------------------------------------------------------------------------------------------------------------------------------+

Table | Create Table

+-------+----------------------------------------------------------------------------------------------------------------------------------------------------------+

tt | CREATE TABLE `tt` (

`id` int(11) NOT NULL DEFAULT '0',

`Field1` varchar(255) DEFAULT NULL,

PRIMARY KEY (`id`)

) ENGINE=InnoDB DEFAULT CHARSET=utf8

+-------+----------------------------------------------------------------------------------------------------------------------------------------------------------+

1 row in set (0.02 sec)

mysql> alter table tt modify id int not null auto_increment;

Query OK, 1 row affected (0.20 sec)

Records: 1 Duplicates: 0 Warnings: 0

mysql> select * from tt;

+----+--------+

id | Field1

+----+--------+

1 | 中国人

+----+--------+

声明:关于《如何通过SQL语句为某个已有字段添加自增属性》以上内容仅供参考,若您的权利被侵害,请联系13825271@qq.com
本文网址:http://www.25820.com/tutorial/14_2208248.html