根据学生、课程、学生与课程之间的多对多联系,上机创建数据库表(Student,Course,Grade),具体要求如下:
1)定义表的属性,并设计合理的约束(实体完整性、参照完整性和用户自定义完整性);
2)使用SQL语句INSERT INTO…VALUES向Student,Course,Grade数据库表插入数据;
3)对各表进行简单查询(显示各表内容)。
创建Student表
Create table Student1
(
学号 char(4) primary key,
姓名 varchar(50) not null,
性别 varchar(1) default 'M',
专业号 varchar(5),
年龄 numeric(3),
constraint CHK_Student1_Sex check(性别='M' or 性别='F')
);
创建Course表
Create table Course1
(
课程号 varchar(7) primary key,
课程名 varchar(20) not null,
学分 numeric(2,1)
);
创建Grade表
Create table Grade1
(
学号 char(4) primary key,
课程号 varchar(7),
成绩 numeric(3,0),
constraint PK_Grade1_To_Student1 foreign key(学号)references Student1(学号),
constraint PK_Grade1_To_Course1 foreign key(课程号)references Course1(课程号)
);
插入数据
INSERT INTO Student1 VALUES('8056','张三','M','13137',18);
INSERT INTO Student1 VALUES('8055','李四','M','13136',18);
INSERT INTO Student1 VALUES('8054','王五','F','13135',18);
INSERT INTO Course1 VALUES('0702601','高等数学(一)',5);
INSERT INTO Course1 VALUES('0702602','高等数学(二)',6.5);
INSERT INTO Course1 VALUES('1303020','计算机科学导论',1);
INSERT INTO Course1 VALUES('1302033','数据结构',4.5);
INSERT INTO Course1 VALUES('1302036','数据库系统原理',3.5);
INSERT INTO Grade1 VALUES('8056','0702601',78);
INSERT INTO Grade1 VALUES('8055','0702602',86);
INSERT INTO Grade1 VALUES('8054','1302033',98);
简单查询各表
SELECT * FROM Student1;
SELECT * FROM Course1;
SELECT * FROM Grade1;