对于以下关系模式:
Teacher(tno , tname ,tage,tsex,title)
project(pno,pname, tno,classify)
work(tno,pno,pt)
其中teacher为教师关系,tno为教师编号,tname为教师名,tage为年龄tsex为性别,title为教师的职称;
project为项目关系,pno为项目号pname为项目名,tno为负责人教师编号,classify为项目分类;
work为参与关系,pt为某个教师在参与某个项目的工作量。
1.查询年龄在30岁以上的教师和性别为男的教师的并集
SELECT *
FROM teacher
WHERE tage>=30
UNION
SELECT *
FROM teacher
WHERE tsex=’男’;
2.查询年龄在40岁以上的教师和性别为男的教师的交集
SELECT *
FROM teacher
WHERE tage>=40
INTERSECT
SELECT *
FROM teacher
WHERE tsex=’男’;
3.查询全体教师和职称为讲师的教师的差集
SELECT *
FROM teacher
EXCEPT
SELECT *
FROM teacher
WHERE title=’讲师’;
4.查询没有工作量的教师号
SELECT tno
FROM teacher
EXCEPT
SELECT tno
FROM work
WHERE pt IS NOT NULL;
5.在参与表中增加一条记录(‘p1’,’t2’,20)
INSERT
INTO work(pno,tno,pt)
VALUES(‘p1’,’t2’,20);
6.将教师参与工作量在90以上的项目的分类修改为省级
UPDATE project
SET classify=’省级’
WHERE pno IN (SELECT pno
FROM work
GROUP BY pno
HAVING SUM(pt)>90);
7.删除职称为讲师的教师的相关记录
DELETE
FROM work
WHERE tno IN(SELECT tno
FROM teacher
WHERE title=’讲师’);
DELETE
FROM project
WHERE tno IN(SELECT tno
FROM teacher
WHERE title=’讲师’);
DELETE
FROM teacher
WHERE title=’讲师’;
8.删除分类为校级项目的相关记录
DELETE
FROM work
WHERE pno IN(SELECT pno
FROM project
WHERE classify=’校级’);
DELETE
FROM project
WHERE classify=’校级’;
9.将教师号为t201参与的项目改为教师t110
UPDATE work
SET tno=’t110’
WHERE tno=’t201’;