I have a table in MySQL that has a primary key:
我在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 | |
+---------------+-------------+------+-----+---------+-------+
but I wanted to make it auto_increment.
但我想让它成为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
1 个解决方案
#1
7
Leave off the primary key
attribute:
不要使用主键属性:
ALTER TABLE gifts MODIFY giftID INT AUTO_INCREMENT;
Certain column attributes, such as PRIMARY KEY
, aren't exactly properties of the column so much as shortcuts for other things. A column marked PRIMARY KEY
, for example, is placed in the PRIMARY
index. Futhermore, all columns in the PRIMARY
index are given the NOT NULL
attribute. (Aside: to have a multi-column primary key, you must use a separate constraint clause rather than multiple PRIMARY KEY
column attributes.) Since the column is already in the PRIMARY
index, you don't need to specify it again when you modify the column. Try SHOW CREATE TABLE gifts;
to see the affects of using the PRIMARY KEY
attribute.
某些列属性(例如PRIMARY KEY)不完全是列的属性,而是其他东西的快捷方式。例如,标记为PRIMARY KEY的列放在PRIMARY索引中。此外,PRIMARY索引中的所有列都被赋予NOT NULL属性。 (除此之外:要使用多列主键,必须使用单独的约束子句而不是多个PRIMARY KEY列属性。)由于该列已在PRIMARY索引中,因此在修改时无需再次指定该列专栏。试试SHOW CREATE TABLE礼物;查看使用PRIMARY KEY属性的影响。
#1
7
Leave off the primary key
attribute:
不要使用主键属性:
ALTER TABLE gifts MODIFY giftID INT AUTO_INCREMENT;
Certain column attributes, such as PRIMARY KEY
, aren't exactly properties of the column so much as shortcuts for other things. A column marked PRIMARY KEY
, for example, is placed in the PRIMARY
index. Futhermore, all columns in the PRIMARY
index are given the NOT NULL
attribute. (Aside: to have a multi-column primary key, you must use a separate constraint clause rather than multiple PRIMARY KEY
column attributes.) Since the column is already in the PRIMARY
index, you don't need to specify it again when you modify the column. Try SHOW CREATE TABLE gifts;
to see the affects of using the PRIMARY KEY
attribute.
某些列属性(例如PRIMARY KEY)不完全是列的属性,而是其他东西的快捷方式。例如,标记为PRIMARY KEY的列放在PRIMARY索引中。此外,PRIMARY索引中的所有列都被赋予NOT NULL属性。 (除此之外:要使用多列主键,必须使用单独的约束子句而不是多个PRIMARY KEY列属性。)由于该列已在PRIMARY索引中,因此在修改时无需再次指定该列专栏。试试SHOW CREATE TABLE礼物;查看使用PRIMARY KEY属性的影响。