这里在参考手册学习创建联合主键的时抛出如下错误:
ERROR 1075 (42000): Incorrect table definition; there can be only one auto column and it must be defined as a key
数据库版本:
root@localhost 18:30:18 sampdb> select version()
-> ;
+------------+
| version() |
+------------+
| 5.5.24-log |
+------------+
1 row in set (0.00 sec)
创建语句:
root@localhost 18:28:43 sampdb> CREATE TABLE animals (
-> grp ENUM('fish','mammal','bird') NOT NULL,
-> id MEDIUMINT NOT NULL AUTO_INCREMENT,
-> name CHAR(30) NOT NULL,
-> PRIMARY KEY (grp,id)
-> );
ERROR 1075 (42000): Incorrect table definition; there can be only one auto column and it must be defined as a key
因为在5.5版本后,默认的存储引擎是innodb,这里我建立一个MYISAM存储引擎表:
root@localhost 18:32:52 sampdb> CREATE TABLE animals (
-> grp ENUM('fish','mammal','bird') NOT NULL,
-> id MEDIUMINT NOT NULL AUTO_INCREMENT,
-> name CHAR(30) NOT NULL,
-> PRIMARY KEY (grp,id)
-> ) ENGINE=MyISAM;
Query OK, 0 rows affected (0.01 sec)
这里发现MyISAM存储引擎没有问题。
那么如果在Innodb下创建联合主键呢!
解决办法如下:
root@localhost 18:36:17 sampdb> drop table animals;
Query OK, 0 rows affected (0.00 sec)
这里将自增主键放在最左边,尝试重建表:
root@localhost 18:38:02 sampdb> CREATE TABLE animals (
-> grp ENUM('fish','mammal','bird') NOT NULL,
-> id MEDIUMINT NOT NULL AUTO_INCREMENT,
-> name CHAR(30) NOT NULL,
-> PRIMARY KEY (id,grp)
-> ) ENGINE=Innodb;
Query OK, 0 rows affected (0.08 sec)
OK,问题解决!!!
阅读(2679) | 评论(0) | 转发(0) |