如何将SQL中的主键更改为自动增量?
我在 MySQL 中有一个表,它有一个主键:
mysql> desc gifts;
+---------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------------+-------------+------+-----+---------+-------+
| giftID | int(11) | NO | PRI | NULL | |
| name | varchar(80) | YES | | NULL | |
| filename | varchar(80) | YES | | NULL | |
| effectiveTime | datetime | YES | | NULL | |
+---------------+-------------+------+-----+---------+-------+
但我想让它自动增量。
以下语句失败。如何修改才能使其正常工作?谢谢
mysql> alter table gifts modify giftID int primary key auto_increment;
ERROR 1068 (42000): Multiple primary key defined
I have a table in MySQL that has a primary key:
mysql> desc gifts;
+---------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------------+-------------+------+-----+---------+-------+
| giftID | int(11) | NO | PRI | NULL | |
| name | varchar(80) | YES | | NULL | |
| filename | varchar(80) | YES | | NULL | |
| effectiveTime | datetime | YES | | NULL | |
+---------------+-------------+------+-----+---------+-------+
but I wanted to make it auto_increment.
The following statement failed. How can it be modified so that it can work? thanks
mysql> alter table gifts modify giftID int primary key auto_increment;
ERROR 1068 (42000): Multiple primary key defined
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
忽略
主键
属性:某些列属性(例如
PRIMARY KEY
)并不完全是列的属性,而是其他事物的快捷方式。例如,标记为PRIMARY KEY
的列放置在PRIMARY
索引中。此外,PRIMARY
索引中的所有列都被赋予了NOT NULL
属性。 (旁白:要拥有多列主键,您必须使用单独的约束子句,而不是多个PRIMARY KEY
列属性。)由于该列已经在PRIMARY
中索引,修改列时无需再次指定。尝试使用SHOW CREATE TABLEgift;
查看使用PRIMARY KEY
属性的影响。Leave off the
primary key
attribute:Certain column attributes, such as
PRIMARY KEY
, aren't exactly properties of the column so much as shortcuts for other things. A column markedPRIMARY KEY
, for example, is placed in thePRIMARY
index. Futhermore, all columns in thePRIMARY
index are given theNOT NULL
attribute. (Aside: to have a multi-column primary key, you must use a separate constraint clause rather than multiplePRIMARY KEY
column attributes.) Since the column is already in thePRIMARY
index, you don't need to specify it again when you modify the column. TrySHOW CREATE TABLE gifts;
to see the affects of using thePRIMARY KEY
attribute.