题目:
创建班级,学生,老师,课程,以及成绩表;
其中表关系有一对多,和多对多关系.表的关系是确定使用双边分析发.比如,一个班级可以对应多个学生,但一个学生不可以对应多个班级,这就是一对多关系表.一个学生可以对应多门课程,一个课程可以对于多个学生,这就是表的多对多关系.
''' 创建班级表, 学生表, 老师表, 课程表,成绩表 # 创建班级表class create table class( id int primary key auto_increment, caption varchar(16), unique(id,caption) ); # 插入班级信息 insert into class(caption) values('三年级二班'), ('一年级三班'), ('三年级一班'); # 查看班级表信息和数据 desc class; select *from class; # 创建学生列表student create table student( id int primary key auto_increment, name varchar(16), gender enum('boy', 'girl') default 'boy', # enum前不加数据类型 class_id int, foreign key(class_id) references class(id) on update cascade on delete cascade ); # 插入学生数据 insert into student(name,gender,class_id) values('峰','boy',2), ('洋','boy',3), ('红','girl',1), ('雪','girl',3), ('林','girl',1), ('空','boy',1); # 查看学生数据 describe student; select *from student; # 创建教师列表teacher create table teacher( id int primary key auto_increment, name varchar(32) ); insert into teacher(name) values('李叔同'), ('叶平'), ('冯友兰'); # 查看表 show create table teacher; select *from teacher; # 课程表course create table course( id int primary key auto_increment, name varchar(16), teacher_id int, foreign key(teacher_id) references teacher(id) on update cascade on delete cascade ); insert into course(name,teacher_id) values('生物',2), ('体育',2), ('哲学',3), ('艺术',2); # 查看表 show table course; select *from course; 创建成绩表 create table score( id int primary key auto_increment, student_id int, foreign key(student_id) references student(id) on update cascade on delete cascade, course_id int, foreign key(course_id) references course(id) on update cascade on delete cascade, number int ); insert into score(student_id,course_id,number) values(1,2,59), (1,2,79), (1,3,89), (2,1,90), (2,2,100), (2,1,98), (3,2,88), (3,3,96), (4,3,96); 查看表: desc score; select *from score ''' ''' select *from class; select *from teacher; select *from student; select *from teacher; select *from course; select *from score; '''