HelloStranger

每个人都是初学者

如何禁用和启用mysql外键约束

启用和禁用

SET foreign_key_checks = 0; 禁用外键
 SOURCE dump_file_name; 进行SQL查询
 SET foreign_key_checks = 1; 恢复外键

 

设置外键

create table teacher_table1

(

teacher_id int auto_increment,

teacher_name varchar(255),

primary key(teacher_id)

);

create table student_table1

(

student_id int auto_increment primary key,

student_name varchar(255),

java_teacher int,

foreign key(java_teacher) references teacher_table1(teacher_id)

或者使用下面的语句建立外键约束,并指定外键约束名student_teacher_fk

constraint student_teacher_fk foreign key(java_teacher) references teacher_table1(teacher_id)

);
点赞

发表评论