mysql创建外键错误
三张表,tb_goods,tb_sort,userforme,给给tb_goods表的sort_id列创建外键,关联tb_sort的id,出错。mysql>desctb_...
三张表, tb_goods, tb_sort, userforme ,给给tb_goods表的sort_id列创建外键,关联tb_sort的id,出错。
mysql> desc tb_goods;
+-----------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+----------------+
| id | int(6) | NO | PRI | NULL | auto_increment |
| name | varchar(20) | NO | | NULL | |
| sort_id | int(6) | NO | | NULL | |
| type | char(1) | NO | | NULL | |
| price | double(6,2) | NO | | NULL | |
| introduce | varchar(200) | NO | | NULL | |
+-----------+--------------+------+-----+---------+----------------+
mysql> desc tb_sort;
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| id | int(6) | NO | PRI | NULL | auto_increment |
| name | varchar(20) | NO | | NULL | |
+-------+-------------+------+-----+---------+----------------+
mysql> alter table tb_sort add constraint FK_sort_goods foreign key(id) references tb_goods(sort_id);
ERROR 1005 (HY000): Can't create table 'mysqldb.#sql-c54_1' (errno: 150)
看过一些资料,还是没看出来,请大家帮看看~
http://hi.baidu.com/itsuperman_/item/ecc4a478b6315e025d178930 展开
mysql> desc tb_goods;
+-----------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+----------------+
| id | int(6) | NO | PRI | NULL | auto_increment |
| name | varchar(20) | NO | | NULL | |
| sort_id | int(6) | NO | | NULL | |
| type | char(1) | NO | | NULL | |
| price | double(6,2) | NO | | NULL | |
| introduce | varchar(200) | NO | | NULL | |
+-----------+--------------+------+-----+---------+----------------+
mysql> desc tb_sort;
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| id | int(6) | NO | PRI | NULL | auto_increment |
| name | varchar(20) | NO | | NULL | |
+-------+-------------+------+-----+---------+----------------+
mysql> alter table tb_sort add constraint FK_sort_goods foreign key(id) references tb_goods(sort_id);
ERROR 1005 (HY000): Can't create table 'mysqldb.#sql-c54_1' (errno: 150)
看过一些资料,还是没看出来,请大家帮看看~
http://hi.baidu.com/itsuperman_/item/ecc4a478b6315e025d178930 展开
推荐于2016-09-18
展开全部
方向错了.
看上去好像是
alter table tb_goods add constraint FK_sort_goods ......
-- 创建测试主表. ID 是主键.
CREATE TABLE test_main (
id INT NOT NULL,
value VARCHAR(10),
PRIMARY KEY(id)
);
-- 创建测试子表.
CREATE TABLE test_sub (
id INT NOT NULL,
main_id INT ,
value VARCHAR(10),
PRIMARY KEY(id)
);
-- 插入测试主表数据.
INSERT INTO test_main(id, value) VALUES (1, 'ONE');
INSERT INTO test_main(id, value) VALUES (2, 'TWO');
-- 插入测试子表数据.
INSERT INTO test_sub(id, main_id, value) VALUES (1, 1, 'ONEONE');
INSERT INTO test_sub(id, main_id, value) VALUES (2, 2, 'TWOTWO');
mysql> ALTER TABLE test_sub
-> ADD CONSTRAINT main_id_cons
-> FOREIGN KEY (main_id)
-> REFERENCES test_main(id);
-> //
Query OK, 2 rows affected (0.17 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> DELETE FROM
-> test_main
-> WHERE
-> id = 1;
-> //
ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constrai
nt fails (`test`.`test_sub`, CONSTRAINT `main_id_cons` FOREIGN KEY (`main_id`) R
EFERENCES `test_main` (`id`))
也就是
ALTER TABLE 子表 ADD CONSTRAINT 约束名称 FOREIGN KEY ( 子表的列) REFERENCES 主表( 主表的列);
追问
我也是这样写的,给tb_goods的sort_id项加外键,不应该是tb_goods才算主表吗?
难道是我理解错了......这样说来,不管是给谁加主键,谁本身是主键那表就是主表?就像,tb_sort表的id是主键,所以这个才算主表吗?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询