要求
現需要構建一個簡易的成績管理系統的數據庫,來記錄幾門課程的學生成績。數據庫中有三張表分別用於記錄學生信息、課程信息和成績信息。
- 學生表(student):學生 id 、學生姓名和性別
sid(主鍵) |
sname |
gender |
1 |
Tom |
male |
2 |
Jack |
female |
- 成績表:成績 id 、學生 id 、課程 id 和分數
mid(主鍵) |
sid(外鍵) |
cid(外鍵) |
score |
1 |
1 |
1 |
50 |
2 |
2 |
1 |
90 |
create database gradesystem;
use gradesystem;
create table student
(
sid int NOT NULL AUTO_INCREMENT,
sname varchar(21) NOT NULL,
gender enum('male', 'female') NOT NULL,
PRIMARY KEY(sid)
);
create table course
(
cid int(10) NOT NULL AUTO_INCREMENT,
cname varchar(21) NOT NULL,
PRIMARY KEY(cid)
);
create table mark
(
mid int NOT NULL AUTO_INCREMENT,
sid int NOT NULL,
cid int NOT NULL,
score int DEFAULT 0,
PRIMARY KEY(mid),
FOREIGN KEY(sid) references student(sid),
FOREIGN KEY(cid) references course(cid)
);
insert into student values(1, 'Tom', 'male'),(2, 'Jack', 'female');
insert into course values(1, 'math');
insert into mark values(1, 1, 1, 50),(2, 2, 1, 90);