CREATE table sms_class( id int PRIMARY KEY auto_increment, name VARCHAR(20) );
– 创建学生信息表
CREATE table sms_student( id int PRIMARY KEY auto_increment, name varchar(128), age int, class_id INT, CONSTRAINT fk_class_id FOREIGN KEY(class_id)REFERENCES sms_class(id) ON DELETE CASCADE ON UPDATE CASCADE );
– 多对多
学生表和课程表之间是多对多关系,需要一个中间表
CREATE table sms_course( id int PRIMARY KEY auto_increment, name VARCHAR(64) );
– 创建中间表
CREATE table sms_student_course( student_course_id int PRIMARY KEY auto_increment, student_id int, course_id int, CONSTRAINT fk_student_id FOREIGN KEY(student_id) REFERENCES sms_student(id) ON UPDATE CASCADE ON DELETE CASCADE, CONSTRAINT fk_course_id FOREIGN KEY(course_id) REFERENCES sms_course(id) ON UPDATE CASCADE ON DELETE CASCADE );