DROP TABLE IF EXISTS t_student_info; CREATE TABLE t_student_info ( id INT(12) NOT NULL AUTO_INCREMENT, id_code BIGINT(18) NOT NULL , class VARCHAR(15), NAME VARCHAR(10) NOT NULL, gender VARCHAR(1), birthday VARCHAR(10), come_from VARCHAR(80), PRIMARY KEY (id) ) ENGINE=INNODB ;DROP TABLE IF EXISTS body_shape; CREATE TABLE body_shape( id INT(12) NOT NULL AUTO_INCREMENT, height INT(3), weight INT(2), score INT(3), rank VARCHAR(5), id_code BIGINT(18) NOT NULL , PRIMARY KEY(id) FOREIGN KEY(id_code) REFERENCES t_student_info(id_code) ON DELETE RESTRICT ON UPDATE CASCADE, INDEX(id_code) ) ENGINE=INNODB ;在5.5下测试没有问题
第一张表没问题 ,第二张表一开始定义外键的时候 没有加index 报错150 后来加了 报上面的错 上面我把行弄错了 应该是第9行报红色字体的错 现在第二张表 却掉 index 如下DROP TABLE IF EXISTS body_shape; CREATE TABLE body_shape( id int(12) NOT NULL auto_increment, height int(3), weight int(2), score int(3), rank varchar(5), id_code bigint(18) NOT NULL , PRIMARY KEY(id) Foreign Key(id_code) References t_student_info(id_code) on DELETE restrict ON update cascade, ) ENGINE=InnoDB ;报错如下ERROR 1064 (42000): You have an error in your SQL syntax; check the manual at corresponds to your MySQL server version for the right syntax to use near 'Foreign Key(id_code) References t_student_info(id_code) on DELETE restrict ON up' at line 9
在MYSQL 5。5下测试通过 SET FOREIGN_KEY_CHECKS = 0; DROP TABLE IF EXISTS t_student_info; CREATE TABLE t_student_info ( id INT(12) NOT NULL AUTO_INCREMENT, id_code INT(15) NOT NULL , class VARCHAR(15), NAME VARCHAR(10) NOT NULL, gender VARCHAR(1), birthday VARCHAR(10), come_from VARCHAR(80), KEY(id_code), PRIMARY KEY (id) ) ENGINE=INNODB ;DROP TABLE IF EXISTS body_shape;CREATE TABLE `body_shape`(id INT(12) NOT NULL AUTO_INCREMENT, height INT(3),weight INT(2),score INT(3),rank VARCHAR(5),id_code INT(15) NOT NULL , KEY(id_code),PRIMARY KEY(id), FOREIGN KEY(id_code) REFERENCES `t_student_info`(id_code) ) ENGINE=INNODB ; SHOW WARNINGS; SHOW ENGINE INNODB STATUS;
还是有问题 --学生信息 DROP TABLE IF EXISTS t_student_info; CREATE TABLE t_student_info ( id INT(12) NOT NULL auto_increment, id_code BIGINT(18) NOT NULL , class VARCHAR(15), name VARCHAR(10) NOT NULL, gender VARCHAR(1), birthday VARCHAR(10), come_from VARCHAR(80), KEY (id_code), PRIMARY KEY (id) ) ENGINE=InnoDB ; ------------------------华丽的分割线---------------- --身体形态 DROP TABLE IF EXISTS body_shape; CREATE TABLE body_shape( id INT(12) NOT NULL auto_increment, height INT(3), weight INT(2), score INT(3), rank VARCHAR(5), id_code BIGINT(18) NOT NULL , PRIMARY KEY(id), KEY (id_code), FOREIGN KEY(id_code) REFERENCES t_student_info(id_code) ON DELETE RESTRICT ON UPDATE CASCADE, ) ENGINE=InnoDB ;第二个表 foreign 那句有问题 !无法解决了
还是刚才啊的代码 ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') ENGINE=InnoDB' at line 11
KEY (id_code), FOREIGN KEY(id_code) REFERENCES t_student_info(id_code) ON DELETE RESTRICT ON UPDATE CASCADE, -- 多了个逗号! ) ENGINE=InnoDB ;
这么吧回到最初的错误上吧 ERROR 1005 (HY000): Can't create table 'tzcs.body_shape' (errno: 150) 代码 DROP TABLE IF EXISTS t_student_info; CREATE TABLE t_student_info ( id INT(12) NOT NULL auto_increment, id_code BIGINT(18) NOT NULL , class VARCHAR(15), name VARCHAR(10) NOT NULL, gender VARCHAR(1), birthday VARCHAR(10), come_from VARCHAR(80), PRIMARY KEY (id) ) ENGINE=InnoDB ; ------------------------华丽的分割线---------------- --身体形态 DROP TABLE IF EXISTS body_shape; CREATE TABLE body_shape( id INT(12) NOT NULL auto_increment, height INT(3), weight INT(2), score INT(3), rank VARCHAR(5), id_code BIGINT(18) NOT NULL , PRIMARY KEY(id), Index(id_code), FOREIGN KEY(id_code) REFERENCES t_student_info (id_code) ON DELETE RESTRICT ON UPDATE CASCADE ) ENGINE=InnoDB ;非常感谢你的耐心!
DROP TABLE IF EXISTS t_student_info; CREATE TABLE t_student_info ( id INT(12) NOT NULL auto_increment, id_code BIGINT(18) NOT NULL , class VARCHAR(15), name VARCHAR(10) NOT NULL, gender VARCHAR(1), birthday VARCHAR(10), come_from VARCHAR(80), PRIMARY KEY (id), Index (id_code) ---------------------- The referenced column should be indexed ) ENGINE=InnoDB ;DROP TABLE IF EXISTS body_shape; CREATE TABLE body_shape( id INT(12) NOT NULL auto_increment, height INT(3), weight INT(2), score INT(3), rank VARCHAR(5), id_code BIGINT(18) NOT NULL , PRIMARY KEY(id), Index (id_code), FOREIGN KEY (id_code) REFERENCES t_student_info (id_code) ON DELETE RESTRICT ON UPDATE CASCADE ) ENGINE=InnoDB ; 运行show innodb status; 找到下面这行: ------------------------ LATEST FOREIGN KEY ERROR ------------------------ 111029 4:35:17 Error in foreign key constraint of table test/#sql-12a0_d: FOREIGN KEY (id_code) REFERENCES t_student_info (id_code) ON DELETE RESTRICT ON UPDATE CASCADE: Cannot find an index in the referenced table where the referenced columns appear as the first columns, or column types in the table and the referenced table do not match for constraint. Note that the internal storage type of ENUM and SET changed in tables created with >= InnoDB-4.1.12, and such columns in old tables cannot be referenced by such columns in new tables. See http://dev.mysql.com/doc/refman/5.1/en/innodb-foreign-key-constraints.html for correct foreign key definition.
CREATE TABLE t_student_info (
id INT(12) NOT NULL AUTO_INCREMENT,
id_code BIGINT(18) NOT NULL ,
class VARCHAR(15),
NAME VARCHAR(10) NOT NULL,
gender VARCHAR(1),
birthday VARCHAR(10),
come_from VARCHAR(80),
PRIMARY KEY (id)
) ENGINE=INNODB ;DROP TABLE IF EXISTS body_shape;
CREATE TABLE body_shape(
id INT(12) NOT NULL AUTO_INCREMENT,
height INT(3),
weight INT(2),
score INT(3),
rank VARCHAR(5),
id_code BIGINT(18) NOT NULL ,
PRIMARY KEY(id)
FOREIGN KEY(id_code) REFERENCES t_student_info(id_code) ON DELETE RESTRICT ON UPDATE CASCADE,
INDEX(id_code)
) ENGINE=INNODB ;在5.5下测试没有问题
后来加了 报上面的错 上面我把行弄错了 应该是第9行报红色字体的错 现在第二张表 却掉 index 如下DROP TABLE IF EXISTS body_shape;
CREATE TABLE body_shape(
id int(12) NOT NULL auto_increment,
height int(3),
weight int(2),
score int(3),
rank varchar(5),
id_code bigint(18) NOT NULL ,
PRIMARY KEY(id)
Foreign Key(id_code) References t_student_info(id_code) on DELETE restrict ON update cascade,
) ENGINE=InnoDB ;报错如下ERROR 1064 (42000): You have an error in your SQL syntax; check the manual at corresponds to your MySQL server version for the right syntax to use near 'Foreign Key(id_code) References t_student_info(id_code) on DELETE restrict ON up' at line 9
PRIMARY KEY(id), -- 少了个逗号。
Foreign Key(id_code
SET FOREIGN_KEY_CHECKS = 0;
DROP TABLE IF EXISTS t_student_info;
CREATE TABLE t_student_info (
id INT(12) NOT NULL AUTO_INCREMENT,
id_code INT(15) NOT NULL ,
class VARCHAR(15),
NAME VARCHAR(10) NOT NULL,
gender VARCHAR(1),
birthday VARCHAR(10),
come_from VARCHAR(80),
KEY(id_code), PRIMARY KEY (id)
) ENGINE=INNODB ;DROP TABLE IF EXISTS body_shape;CREATE TABLE `body_shape`(id INT(12) NOT NULL AUTO_INCREMENT,
height INT(3),weight INT(2),score INT(3),rank VARCHAR(5),id_code INT(15) NOT NULL ,
KEY(id_code),PRIMARY KEY(id),
FOREIGN KEY(id_code) REFERENCES `t_student_info`(id_code)
) ENGINE=INNODB ;
SHOW WARNINGS;
SHOW ENGINE INNODB STATUS;
--学生信息
DROP TABLE IF EXISTS t_student_info;
CREATE TABLE t_student_info (
id INT(12) NOT NULL auto_increment,
id_code BIGINT(18) NOT NULL ,
class VARCHAR(15),
name VARCHAR(10) NOT NULL,
gender VARCHAR(1),
birthday VARCHAR(10),
come_from VARCHAR(80),
KEY (id_code),
PRIMARY KEY (id)
) ENGINE=InnoDB ;
------------------------华丽的分割线----------------
--身体形态
DROP TABLE IF EXISTS body_shape;
CREATE TABLE body_shape(
id INT(12) NOT NULL auto_increment,
height INT(3),
weight INT(2),
score INT(3),
rank VARCHAR(5),
id_code BIGINT(18) NOT NULL ,
PRIMARY KEY(id),
KEY (id_code),
FOREIGN KEY(id_code) REFERENCES t_student_info(id_code) ON DELETE RESTRICT ON UPDATE CASCADE,
) ENGINE=InnoDB ;第二个表 foreign 那句有问题 !无法解决了
还是刚才啊的代码 ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') ENGINE=InnoDB' at line 11
FOREIGN KEY(id_code) REFERENCES t_student_info(id_code) ON DELETE RESTRICT ON UPDATE CASCADE, -- 多了个逗号!
) ENGINE=InnoDB ;
这么吧回到最初的错误上吧
ERROR 1005 (HY000): Can't create table 'tzcs.body_shape' (errno: 150)
代码
DROP TABLE IF EXISTS t_student_info;
CREATE TABLE t_student_info (
id INT(12) NOT NULL auto_increment,
id_code BIGINT(18) NOT NULL ,
class VARCHAR(15),
name VARCHAR(10) NOT NULL,
gender VARCHAR(1),
birthday VARCHAR(10),
come_from VARCHAR(80),
PRIMARY KEY (id)
) ENGINE=InnoDB ;
------------------------华丽的分割线----------------
--身体形态
DROP TABLE IF EXISTS body_shape;
CREATE TABLE body_shape(
id INT(12) NOT NULL auto_increment,
height INT(3),
weight INT(2),
score INT(3),
rank VARCHAR(5),
id_code BIGINT(18) NOT NULL ,
PRIMARY KEY(id),
Index(id_code),
FOREIGN KEY(id_code) REFERENCES t_student_info (id_code) ON DELETE RESTRICT ON UPDATE CASCADE
) ENGINE=InnoDB ;非常感谢你的耐心!
CREATE TABLE t_student_info (
id INT(12) NOT NULL auto_increment,
id_code BIGINT(18) NOT NULL ,
class VARCHAR(15),
name VARCHAR(10) NOT NULL,
gender VARCHAR(1),
birthday VARCHAR(10),
come_from VARCHAR(80),
PRIMARY KEY (id),
Index (id_code) ---------------------- The referenced column should be indexed
) ENGINE=InnoDB ;DROP TABLE IF EXISTS body_shape;
CREATE TABLE body_shape(
id INT(12) NOT NULL auto_increment,
height INT(3),
weight INT(2),
score INT(3),
rank VARCHAR(5),
id_code BIGINT(18) NOT NULL ,
PRIMARY KEY(id),
Index (id_code),
FOREIGN KEY (id_code) REFERENCES t_student_info (id_code) ON DELETE RESTRICT ON UPDATE CASCADE
) ENGINE=InnoDB ;
运行show innodb status;
找到下面这行:
------------------------
LATEST FOREIGN KEY ERROR
------------------------
111029 4:35:17 Error in foreign key constraint of table test/#sql-12a0_d:
FOREIGN KEY (id_code) REFERENCES t_student_info (id_code) ON DELETE RESTRICT ON
UPDATE CASCADE:
Cannot find an index in the referenced table where the
referenced columns appear as the first columns, or column types
in the table and the referenced table do not match for constraint.
Note that the internal storage type of ENUM and SET changed in
tables created with >= InnoDB-4.1.12, and such columns in old tables
cannot be referenced by such columns in new tables.
See http://dev.mysql.com/doc/refman/5.1/en/innodb-foreign-key-constraints.html
for correct foreign key definition.
谢谢 bigint 可以的
15楼的的方法正解 !