一对一
一个人拥有一张身份证,一张身份证只被一个人拥有
一对多
一个班级拥有多个学生,一个学生只属于一个班级
多对多
学生 和 课程的关系: 1个学生 可以学习多门课程。 1个课程 可以被多个学生学习。
有一个问题:不管在那张表里面写另一张表的ID 都是不合适的。 所以有了中间表。来处理这个对应关系。
//创建学生表
drop table if exists student;
create table sttudent(
id int primary key auto_increment,
sn int,
name varchar(20)
);
//创建课程表
drop table if exists course;
create table course(
id int primary key auto_increment,
name varchar(20)
);
//创建学生课程中间表,考试成绩表
drop table if exists socre;
create table score(
id int primary key quto_increment,
score decimal(3,1),
student_id int,
course_id int,
foreign key(student_id)references student(id),
foreign key(course_id)references course(id)
);