创建一个年级表:
create table if not exists grade(
gradeid int(10) not null auto_increment comment '年级id',
gradename varchar(50) not null comment '年级名称',
primary key (gradeid)
);
创建一个学生表:
create table student1(
id int(10) not null auto_increment comment '学号',
name varchar(10) not null comment '姓名',
gradeid int(10) not null comment '学生的年级',
primary key (id),
key FK_gradeid (gradeid),
constraint FK_gradeid foreign key (gradeid) references grade (gradeid)
);
注意:删除有外键的表时,要先删除引用别人的表(student),再删除被引用的表(grade)