数据库原理实验

数据库实验

实验目的

通过理论学习与实验设计,使自己具备如下能力:

  1. 具备分析不同用户的特性,设计用户的数据需求方案并实施的能力。
    2.选用合适的数据库管理工具承担数据库系统的实施、运行、监控与维护工作的能力。

实验项目

1.为上面的三个表补充定义主码、外码(级联删除、级联修改)。
(1)给countries添加主键,外键

alter table countries 
add constraint pk_countries primary key(cid);

(2)给dept添加主键,外键

alter table dept 
add constraint pk_dept primary key(deptno);
alter table dept 
add constraint fk_dept foreign key(cid) references countries(cid);

(3)给emp添加主键,外键

alter table emp 
add constraint pk_emp primary key(empno);
alter table emp 
add constraint fk_emp foreign key(deptno) references dept1(deptno);

2.为上面的三个表补充定义check约束,请写出相应的SQL语句并执行:
a.工资介于5000到100000之间

alter table EMP
add constraint chk_EMP_SAL check(SAL between 5000 and 10000);

b.雇佣日期大于2000-01-01

alter table EMP
add constraint chk_EMP_HIREDATE check(HIREDATE>to_date('2019-05-10','yyyy-mm-dd')); 

C.工作类型的取值只能是:

clerk,salesman,analyst,manager,president 
alter table EMP 
add constraint ck_JOB 
check(JOB in('clerk','salesman','analyst','manager','president'));

3.公司与湘潭市达成协议,在湘潭市设立一个生产部门,部门编号为:50,部门名称为:PRODUCTION。请写出相应的SQL语句并执行。

insert into DEPT(CID,DEPTNO,DNAME,LOC, EMPS ,INCOME)
  values('CH',50,'PRODUCTION','湘潭',null,null);

4.2019年5月1日,公司决定聘请你担任PRODUCTION部门的经理(职工编号为’9’+你学号的最后3位数,如你的学号为201701020304,则职工编号为:9304,工资自定,没有佣金),并为你招聘了下列职工,开始创业。请写出相应的SQL语句并执行。

insert into emp values(9429,'***','manager',7839,
to_date('2019-05-01','yyyy-mm-dd'), 30000,0,50);

5.为了扩大生产,2019年5月10日,公司将与你同姓的人都招聘到你的部门工作。待聘人员名单在视图“待聘人员”中。
要求:用临时编号作为职工编号,工作类型为“clerk”,工资15000。
请写出相应的SQL语句并执行。

insert into emp
 select 临时编号,姓名,'clerk',9429,
to_date('2019-05-10','yyyy-mm-dd'),15000,0,50
 from 待聘人员  where 姓名 like '邹%';

6.DEPT表中的部门人数、部门职工收入总和两列还没有填写。请写出相应的SQL语句并执行。

update dept
set income=case 
when deptno=10 then (select sum(sal)+sum(comm) from dept left outer join emp on dept.deptno = emp.deptno where emp.deptno=10)
when deptno=20 then (select sum(sal)+sum(comm) from dept left outer join emp on dept.deptno = emp.deptno where emp.deptno=20)
when deptno=30 then (select sum(sal)+sum(comm) from dept left outer join emp on dept.deptno = emp.deptno where emp.deptno=30)
when deptno=40 then (select sum(sal)+sum(comm) from dept left outer join emp on dept.deptno = emp.deptno where emp.deptno=40)
else (select sum(sal)+sum(comm) from dept left outer join emp on dept.deptno = emp.deptno where emp.deptno=50)
end;
update dept
set emps=case 
when deptno=10 then (select count(empno) from dept left outer join emp on dept.deptno = emp.deptno where emp.deptno=10)
when deptno=20 then (select count(empno) from dept left outer join emp on dept.deptno = emp.deptno where emp.deptno=20)
when deptno=30 then (select count(empno) from dept left outer join emp on dept.deptno = emp.deptno where emp.deptno=30)
when deptno=40 then (select count(empno) from dept left outer join emp on dept.deptno = emp.deptno where emp.deptno=40)
else (select count(empno) from dept left outer join emp on dept.deptno = emp.deptno where emp.deptno=50)
end;

7.删除20号部门,请写出相应的SQL语句并执行。

delete from emp
      where deptno=20;
delete from dept
      where deptno=20;

8.删除工资低于15000的职工,请写出相应的SQL语句并执行。

delete from emp
       where sal<15000;

9.所有职工的工资增加5000,请写出相应的SQL语句并执行。

update emp
 set sal=sal+5000;

三.实验错误解决方案

问题一: ORA-00903问题
1、问题的出现

在实验项目6中在DEPT表中填写部门职工收入,SQL语句如下:

Insert into DEPT(INOCOME) 
select sum(sal)+sum(comm) as wage 
from emp;

出现错误:

ORA-01400: 无法将 null 插入(“A2107***”,“DEPT”。“DEPTNO”)

图 2实验问题一
2、问题分析

错误出现在第1行,表明插入insert 错误,插入式不能将空值插入表中,将数插入到表中,插入数据的类型必须与被插入表的类型的数据一致。

3、解决方案

解决方案如下:

update dept
set income=case 
when deptno=10 then (select sum(sal)+sum(comm) from dept left outer join emp on dept.deptno = emp.deptno where emp.deptno=10)
when deptno=20 then (select sum(sal)+sum(comm) from dept left outer join emp on dept.deptno = emp.deptno where emp.deptno=20)
when deptno=30 then (select sum(sal)+sum(comm) from dept left outer join emp on dept.deptno = emp.deptno where emp.deptno=30)
when deptno=40 then (select sum(sal)+sum(comm) from dept left outer join emp on dept.deptno = emp.deptno where emp.deptno=40)
else (select sum(sal)+sum(comm) from dept left outer join emp on dept.deptno = emp.deptno where emp.deptno=50)
end;

执行该语句后,表已经更新,问题解决。

图 3 问题一解决后图
通过SQL查询语句:

select emps,income from dept;

结果如下(全部项目完成后的结果):

图 4 问题一解决查询图
问题二: 回滚问题

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章