知识点
外键是指引用另外一个表中的一列或多列数据,被引用的列应该具有主键约束或者唯一性约束(简单来说外键是另一个表的主键或者唯一约束)。外键可以有重复的, 可以是空值,用来建立和加强两个表数据之间的连接。
实验
表一:
create table stuInfo(
Scode int primary key, --学生的学号
Sname char(10), --学生的姓名
Saddress varchar(50), --学生的住址
Sgrade int, --学生所在班级
Semail varchar(50), --学生的邮箱地址
Sbrith date
)DEFAULT CHARSET='utf8';
表二:
create table score(
studentID int,
coureseID int,
score int,
scoreID int primary key,
foreign key(studentID) references stuInfo(Scode) --添加外键 )
DEFAULT charset='utf8';
添加外键
alter table stuInfo add foreign key (scode) references score(studentID);
进行查看外键名称
show create table stuInfo;
删除外键
alter table score drop foreign key score_ibQk_1;