原来是没有主键的,想增加一列为主键。
mysql> desc t111;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| name | varchar(24) | YES | | robert | |
| age | int | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
执行代码,结果报错:
mysql> alter table t111 add stu_id tinyint primary key first;
ERROR 1062 (23000): Duplicate entry '0' for key 't111.PRIMARY'
解决办法,把它设置为自增auto_increment:
mysql> alter table t111 add stu_id int(4) primary key auto_increment first;
Query OK, 0 rows affected, 1 warning (0.06 sec)
Records: 0 Duplicates: 0 Warnings: 1