已知有三个教学管理数据库中的基本表S,SC,C分别为
S(SNO, SNAME, AGE, SEX, SDEPT)
SC(SNO,CNO,GRADE)
C(CNO,CNAME,CDEPT,TNAME)
SNO(学号),SNAME(姓名),AGE(年龄),SEX(性别),SDEPT(所在系),CNO(课程号),GRADE(分数),CNAME(课程名),CDEPT(承担课程系),TNAME(任课教师).
1、建表,在定义中要求声明
1) 每个表的主外码;
2) 雇员的年龄介于12到65之间 ;
3) 姓名和课程名不能为空 ;
4) 分数取0到100之间整数;
2、往表中插入数据 :
3、请用SQL语句完成下列查询
(1) 检索年龄大于20岁学生的姓名、性别和年龄;
(2) 统计每名学生选修课程的门数,要求输出学号和选课门;
(3) 检索所有不及格同学的学号、姓名、课程名、成绩;
(4) 计算各门课程的平均分;
操作代码如下:
create database zxl
----------------------------------
create table S
(
SNO char(12) primary key,
SNAME char(6) not null,
SEX char(2) check (SEX in('男','女')),
SDEFT char(15) NOT NULL,
AGE int check(AGE between 12 and 65)
)
----------------------------------
CREATE TABLE C
(
CNO CHAR(12) PRIMARY KEY,
CNAME CHAR(6) NOT NULL,
CDEPT CHAR(15) NOT NULL,
TNAME CHAR(5) NOT NULL
)
----------------------------------
create table SC
(
SNO char(12) references S(SNO),
CNO char(12) references C(CNO),
GREADE INT check (GREADE between 0 and 100),
PRIMARY KEY(SNO,CNO)
)
---------------------------------
insert into S values('201190609111','姓名1','男','电子系','20')
insert into S values('201190609112','姓名2','女','电子系','22')
insert into S values('201190609113','姓名3','男','建筑系','20')
insert into S values('201190609114','姓名4','男','电子系','21')
insert into S values('201190609115','姓名5','女','文法系','20')
insert into S values('201190609116','姓名6','男','电子系','25')
---------------------------------
insert into C values('001','sql','文2教','刘培')
insert into C values('002','vb','文1教','封伟')
insert into C values('003','c','文2教','张振')
---------------------------------
insert into SC values('201190609111','001','100')
insert into SC values('201190609112','001','80')
insert into SC values('201190609113','001','76')
insert into SC values('201190609114','001','58')
insert into SC values('201190609115','001','49')
insert into SC values('201190609116','001','35')
insert into SC values('201190609111','002','77')
insert into SC values('201190609112','002','80')
insert into SC values('201190609115','002','65')
insert into SC values('201190609116','002','88')
insert into SC values('201190609111','003','50')
insert into SC values('201190609112','003','80')
insert into SC values('201190609113','003','43')
insert into SC values('201190609114','003','99')
--------------------------------
select SNAME,SEX,AGE
FROM S
where AGE>20
-------------------------------
SELECT SNO,count(SC.CNO)
FROM SC,C
WHERE SC.CNO=C.CNO
GROUP BY SNO
-------------------------------
SELECT SC.SNO,SNAME,CNAME,GREADE
FROM SC,C,S
WHERE SC.SNO=S.SNO AND SC.CNO=C.CNO AND SC.GREADE<60
------------------------------
SELECT CNAME,AVG(GREADE) AS 平均
FROM SC,C
WHERE SC.CNO=C.CNO
GROUP BY CNAME
-----------------------------