mysql 8.0版本中添加表级约束外键失败原因之一
1 create table if not exists t_classes( 2 classes_id int(3), 3 classes_name varchar(40), 4 constraint pk_classes_id primary key(classes_id) 5 ); 6 7 -- 利用标记约束添加主键和外键 8 create table if not exists t_student( 9 student_id int(10), 10 student_name varchar(20), 11 sex char(2), 12 birthday date, 13 email varchar(30), 14 classes_id int(3), 15 constraint fk_classes_id foreign key (classes_id) references t_classes (classes_id), 16 constraint student_id_pk primary key(student_id) 17 );
create table if not exists t_student( student_id int(10) primary key, student_name varchar(20), sex char(2), birthday DATE, email varchar(30), classes_id2 int(3), -- 这里的添加顺序是先添加主键后添加外键,会失败,提示语法错误 constraint student_id_pk primary key(student_id) constraint fk_classes_id foreign key (classes_id2) references t_classes (classes_id) );
同样是添加主键和外键,顺序颠倒后会报语法错误
数据库版本号:8.0.19