一.前言
因为考研需求,我于是将数据库系统概论王珊编写的第三章学生-课程数据库的三张表实现了一下,为了简化操作,我没有实现部分外码功能。
我用的是MySQL实现的
二.正文
/*3. 创建学生表Student(例3.5)*/
CREATE TABLE Student(
Sno CHAR(9) PRIMARY KEY,/*列级完整性约束条件,Sno是主码*/
Sname CHAR(20) UNIQUE,/*Sname取唯一值*/
Ssex CHAR(2),
Sage SMALLINT,
Sdept CHAR(20)
);
/*4.插入学生信息*/
INSERT INTO Student VALUES('201215121','李勇','男',20,'CS');
INSERT INTO Student VALUES('201215122','刘晨','女',19,'CS');
INSERT INTO Student VALUES('201215123','王敏','女',18,'MA');
INSERT INTO Student VALUES('201215125','张立','男',19,'IS');
/*6.创建课程表Course(例3.6)*/
CREATE TABLE Course(
Cno CHAR(4) PRIMARY KEY,
Cname CHAR(40) NOT NULL,
Cpno CHAR(4),/*Cpno的含义是先行课*/
Ccredit SMALLINT
);
/*6.插入课程信息*/
INSERT INTO Course VALUES('1','数据库','5',4);
INSERT INTO Course VALUES('2','数学','null',2);
INSERT INTO Course VALUES('3','信息系统','1',4);
INSERT INTO Course VALUES('4','操作系统','6',3);
INSERT INTO Course VALUES('5','数据结构','7',4);
INSERT INTO Course VALUES('6','数据处理','null',2);
INSERT INTO Course VALUES('7','PASCAL语言','6',4);
/*7.建立学生选课表SC(例3.7)*/
CREATE TABLE SC(
Sno CHAR(9),
Cno CHAR(4),
Grade SMALLINT,
PRIMARY KEY (Sno,Cno) /*主码由两个属性构成,必须作为表级完整性进行定义*/
);
/*8.插入学生选课信息*/
INSERT INTO SC VALUES('201215121','1',92);
INSERT INTO SC VALUES('201215121','2',85);
INSERT INTO SC VALUES('201215121','3',88);
INSERT INTO SC VALUES('201215122','2',90);
INSERT INTO SC VALUES('201215122','3',80);
SELECT *
FROM student;
SELECT*
FROM course;
SELECT *
FROM sc;
效果图:
学生表:
课程表:
选课表: