添加外键约束
alter table 从表 add foreign key(外键字段) references主表 (主键字段)
为学生表的c_id字段添加外键约束
alter table students add foreign key(c_id) references classes(id);
创建school表
create table school(id int unsigned not null primary key auto_increment,name varchar(30));
创建teacher表
create table teacher(id int unsigned not null primary key auto_increment,name varchar(30),s_id int unsigned,foreign key(s_id) references school(id));
foreign key(s_id) references school(id)
删除外键约束
alter table 表名 drop foreign key 外键名
alter table teacher drop foreign key teacher_ibfk_1;