CDA持证人阿涛哥

2022-04-28   阅读量: 901

Mysql

mysql报错1822 :Failed to add the foreign key constraint. Missing index for constraint empo_ibfk_1

扫码加入数据分析学习群

第一句代码: create table dept(deptno int,dname varchar(15),loc varchar(10));

第二句代码:

create table empo(empno int, ename varchar(15) unicode, job varchar(10) not null, mgr int, hiredate date, sal float default 4000, comn decimal,
deptno int, foreign key (deptno) references dept (deptno));

执行第一句, 然后执行第二句后报错如下

7E}X7LG$R`Y9`X```6I%KW7.jpg

解决方法:

作为外键,dept表中的deptno必须是唯一值,因此得添加约束unique,或者primary key;


给第一句代码加上unique,改为:

create table dept(deptno int unique ,dname varchar(15),loc varchar(10));

然后再执行第二句就没问题了


学习更多数据分析知识欢迎访问 https://www.cda.cn/member.html?utm_source=weitao



510.7184 4 0 关注作者 收藏

评论(0)


暂无数据

推荐课程

推荐帖子