mysql, error reported: 1822-failed to add the foreign key constraint. Missing index for constraint ‘tb_emp_ibfk_1’ in the referenced table ‘tb_dept’
reason: if table A has no primary key set, the fields in table A cannot be used as foreign keys for table B.
solution, on the premise of not deleting table A, add the following SQL text:
ALTER TABLE tb_dept ADD PRIMARY KEY(deptno);
SQL:
p>
DROP DATABASE IF EXISTS tb_test;
CREATE DATABASE tb_test DEFAULT charset utf8;
USE tb_test;
# 部门表
CREATE TABLE tb_dept (
deptno INT NOT NULL COMMENT '编号',
deptname VARCHAR ( 10 ) NOT NULL COMMENT '名称',
deptaddr VARCHAR ( 20 ) NOT NULL COMMENT '地址'
);
INSERT INTO tb_dept VALUES
( 101, '研发部门', '武汉' ),
( 127, '会计部门', '上海' );
# 员工表
DROP TABLE IF EXISTS tb_emp;
CREATE TABLE tb_emp (
empno INT NOT NULL COMMENT '编号',
empname VARCHAR ( 10 ) NOT NULL COMMENT '姓名',
empjob VARCHAR ( 10 ) NOT NULL COMMENT '职位',
empsal INT NOT NULL DEFAULT 3000 COMMENT '薪酬',
deptno INT NOT NULL COMMENT '所属部门',
PRIMARY KEY ( empno ),
FOREIGN KEY ( deptno ) REFERENCES tb_dept ( deptno )
);
INSERT INTO tb_emp VALUES
(10001,'杨过','研发主管',2000000,101),
(40001,'张三丰','总裁',90000000,127);
operation result:
DROP DATABASE IF EXISTS tb_test
> OK
> 时间: 0.32s
CREATE DATABASE tb_test DEFAULT charset utf8
> OK
> 时间: 0.991s
USE tb_test
> OK
> 时间: 0.008s
# 部门表
CREATE TABLE tb_dept (
deptno INT NOT NULL COMMENT '编号',
deptname VARCHAR ( 10 ) NOT NULL COMMENT '名称',
deptaddr VARCHAR ( 20 ) NOT NULL COMMENT '地址'
)
> OK
> 时间: 1.74s
INSERT INTO tb_dept VALUES
( 101, '研发部门', '武汉' ),
( 127, '会计部门', '上海' )
> Affected rows: 2
> 时间: 0.532s
# 员工表
DROP TABLE IF EXISTS tb_emp
> OK
> 时间: 0.393s
CREATE TABLE tb_emp (
empno INT NOT NULL COMMENT '编号',
empname VARCHAR ( 10 ) NOT NULL COMMENT '姓名',
empjob VARCHAR ( 10 ) NOT NULL COMMENT '职位',
empsal INT NOT NULL DEFAULT 3000 COMMENT '薪酬',
deptno INT NOT NULL COMMENT '所属部门',
PRIMARY KEY ( empno ),
FOREIGN KEY ( deptno ) REFERENCES tb_dept ( deptno )
)
> 1822 - Failed to add the foreign key constraint. Missing index for constraint 'tb_emp_ibfk_1' in the referenced table 'tb_dept'
> 时间: 0.034s
p>
div>
Read More:
- Failed to add the foreign key constraint. Missing index for constraint ‘stu_ibfk_1’ in the reference
- MySQL failed to add foreign key: SQL 1452 cannot add or update a child row:a foreign key constraint fails
- MySql ERROR CODE:1215 Cannot add the foreign key constraint
- Mysql error 1452 – Cannot add or update a child row: a foreign key constraint fails
- Solution to error 1452: cannot add or update a child row: a foreign key constraint failures in MySQL
- MYSQL ERROR 1217 (23000): Cannot delete or update a parent row: a foreign key constraint fails
- Ora-02292: integrity constraint (XX) violated – child record found foreign key Association, record cannot be deleted
- Mysql error when deleting the table structure: Error Code: 1217. Cannot delete or update a parent row: a foreign key constraint fails
- MySQL failed to add foreign key error 1452
- [Oracle] when inserting data, “ora-00001: unique constraint” appears
- python sqlite Error IntegrityError: UNIQUE constraint failed: table_area1.user_name
- MySQL error 1451 23000 foreign key exception handling
- django.db.utils .IntegrityError: NOT NULL constraint failed: blog_ blog.author_ ID error resolution
- django.db.utils.IntegrityError: UNIQUE constraint failed: auth_user.username
- greendao insert data UNIQUE constraint failed: PURCHASE_ORDER_TABLE._id (code 1555)
- IntegrityError at ** NOT NULL constraint failed: learning_logs_topic.owner_id
- Error report of. XML header file constraint in idea
- MySQL:Foreign key fails with error Error Code: 1452.
- MySQL error code 1217 (ER_ROW_IS_REFERENCED): Cannot delete or update a parent row: a foreign key co
- Flutter solves the problem of incorrect use of parent datawidget and boundary constraint