1.题目:
职工表EMP(E#,ENAME,AGE,SEX,ECITY),其属性分别表示职工工号、姓名、年龄、性别和籍贯。
工作表WORKS(E#,C#,SALARY),其属性分别表示职工工号、工作的公司编号和工资。
公司表COMP(C#,CNAME,CITY),其属性分别表示公司编号、公司名称和公司所在城市。
emp(eno.ename,age,sex,ecity)
works(eno,cno,salary)
comp(cno,cname,city)
1./用CREATE TABLE语句创建上述3个表,需指出主键和外键。/
create table emp(
eno char(4) not null,
ename char(8) not null,
age smallint,
sex char(1),
ecity char(20),
primary key(eno));
create table comp(
cno char(4) not null,
cname char(20) not null,
city char(20),
primary key(cno));
create table works(
eno char(4) not null,
cno char(4) not null,
salary smallint,
primary key(eno,cno),
foreign key(eno) references emp(eno),
foreign key(cno) references comp(cno));
2./检索超过50岁的男职工的工号和姓名。/
select eno,ename from emp
where sex="m" and age >50
3./假设每个职工只能在一个公司工作,检索工资超过1000元的男性职工工号和姓名。/
select eno,ename from emp
where eno in(
select eno from works
where salary>1000)
4./假设每个职工可在多个公司工作,检索至少在编号为C4和C8的公司兼职的职工工号和姓名。/
select emp.eno,ename from emp,works w1,works w2
where w1.cno="c4" and w2.cno="c8" and emp.eno=w1.eno and w1.eno=w2.eno
5./检索在联华公司工作、工资超过1000元的男性职工的工号和姓名。/
select a.eno,a.ename from emp a,works b,comp c
where a.eno=b.eno and b.cno=c.cno and cname="联华" and a.sex="m" and b.salary>1000
6./假设每个职工可在多个公司工作,检索每个职工的兼职公司数目和工资总数,显示(E#,NUM,SUM_SALARY),分别表示工号、公司数目和工资总数。/
select eno,count(eno) as num,sum(salary) as sum_salary from works
group by eno
7./工号为E1的职工在多个公司工作,试检索至少在E1 职工兼职的所有公司工作的职工工号。/
有问题还没解决
select x.eno from works x
where not exists (
select * from works y
where eno="e1" and not exists(
select * from works z
where z.eno=x.eno and z.cno=y.cno))
8./检索联华公司中低于本公司平均工资的职工工号和姓名。/
select DISTINCT emp.eno,emp.ename from emp,works,comp
where emp.eno=works.eno and works.cno =comp.cno and salary<(
select avg(salary) from works
where works.cno in(
select works.cno from comp
where comp.cname="联华"))
9./在每一公司中为50岁以上的职工加薪100元(若职工为多个公司工作,可重复加)。/
update works
set salary = salary+100
where eno in(
select eno from emp
where age>50)
10./在EMP表和WORKS表中删除年龄大于60岁的职工有关元组。/
delete from emp
where age>60;
delete from works
where eno in (
select eno from emp
where age>60);
11./建立一个有关女职工信息的视图EMP_WOMAN,属性包括(E#,ENAME,C#,CNAME,SALARY)。然后对视图EMP_WOMAN进行操作,检索每一位女职工的工资总数(假设每个职工可在多个公司兼职)。/
create view emp_woman as
select eno,ename, from emp a,works b,comp c