如何通过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 | ä¸­å›½äºº |
+----+--------+
温馨提示:答案为网友推荐,仅供参考
相似回答