共六个表,其中两个表 要引用外键,第一个引用过的,在第二个里面就不能引用了,否则就会报错
最后一个,有table,没粘贴上
这俩一设外键就报错
foreign key (Mno) reference Manager(Mno),
foreign key (LabNo) reference Lab(LabNo)
我是在完整性约束哪里出的错,谢谢大家的帮助
mysql创建外键 报错
答案:2 悬赏:30 手机版
解决时间 2021-01-27 11:11
- 提问者网友:泪痣哥哥
- 2021-01-26 13:25
最佳答案
- 五星知识达人网友:撞了怀
- 2021-01-26 13:42
重新装
全部回答
- 1楼网友:轮獄道
- 2021-01-26 14:14
方向错了. 看上去好像是 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 主表( 主表的列);
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯