一、Student 表

image.png

二、Course 表

image.png

三、SC 表

image.png

四、初始化 SQL 语句

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

/*学生表*/
CREATE TABLE Student
(
Sno INT(9) PRIMARY KEY,
Sname VARCHAR(10),
Ssex CHAR(2),
Sage TINYINT(3),
Sdept VARCHAR(20)
);
INSERT INTO Student
VALUES (201215121, '李勇', '男', 20, 'CS'),
(201215122, '刘晨', '女', 19, 'CS'),
(201215123, '王敏', '女', 18, 'MA'),
(201215125, '张立', '男', 19, 'IS'),
(201215133, '张三', '男', 21, 'TE'),
(201215137, '赵四', '男', 23, 'TE'),
(201215139, '田二', '女', 24, 'CS'),
(201215140, '李四', '男', 21, 'CS'),
(201215141, '郑五', '女', 22, 'IS');

/*课程表*/
CREATE TABLE Course
(
Cno INT(4) PRIMARY KEY,
Cname VARCHAR(40),
Cpno INT(4),
Ccredit TINYINT(3),
FOREIGN KEY (Cpno) REFERENCES Course (Cno)
);
INSERT INTO Course
VALUES (1, '数据库', 5, 4),
(2, '数学', NULL, 2),
(3, '信息系统', 1, 4),
(4, '操作系统', 6, 3),
(5, '数据结构', 7, 4),
(6, '数据处理', NULL, 2),
(7, 'PASCAL语言', 6, 4);

/*选课表*/
CREATE TABLE SC
(
Sno INT(9),
Cno INT(4),
Grade SMALLINT(3),
PRIMARY KEY (Sno, Cno),
/* 主码由两个属性构成,必须作为表级完整性进行定义*/
FOREIGN KEY (Sno) REFERENCES Student (Sno),
/* 表级完整性约束条件,Sno是外码,被参照表是Student */
FOREIGN KEY (Cno) REFERENCES Course (Cno)
/* 表级完整性约束条件, Cno是外码,被参照表是Course*/
);
INSERT INTO SC
VALUES (201215121, 1, 92),
truetruetrue(201215121, 2, 85),
truetruetrue(201215121, 3, 88),
truetruetrue(201215122, 2, 90),
(201215122, 3, 80),
truetruetrue(201215122, 6, 59),
(201215123, 1, 84),
(201215125, 1, 60),
(201215125, 3, 90),
(201215133, 4, 87),
(201215137, 2, 79),
(201215139, 2, 80),
(201215140, 2, 81);

SET FOREIGN_KEY_CHECKS = 1;