數據庫原理實驗

數據庫實驗

實驗目的

通過理論學習與實驗設計,使自己具備如下能力:

  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 問題一解決查詢圖
問題二: 回滾問題

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