1. 程式人生 > 其它 >資料庫實驗1-SQL語言

資料庫實驗1-SQL語言

技術標籤:資料庫實驗報告

實驗專案名稱 SQL語言 指導老師 xxx
(1)實驗目的
通過本次實驗,使學生能夠熟練運用SQL語言進行資料查詢和資料更新,以及對基本表、檢視、索引的管理。
(2)實驗環境
熟悉實驗室實驗環境,閱讀本次實驗預備知識,熟悉基本表、檢視、索引的基本概念,瞭解基本表、檢視、索引的基本管理語法,熟悉查詢語句和更新語句的基本語法。實驗中根據實驗步驟要求書寫相應的SQL程式碼並執行,記錄和分析執行結果,使用程式碼驗證SQL程式碼執行後是否滿足步驟要求,並獨立完成實驗報告。
(3)實驗內容

實驗內容第一部分
(建議先把實驗內容和步驟拷貝到SQL Developer工作區,然後按步驟進行實驗,後同)

  1. 建立學生選課關係資料庫中的STUDENT表(特別提示:表結構見1.3節,使用指導書上的表結構是錯誤的,後同);
  2. 建立學生選課關係資料庫中的COURSE表;
  3. 建立學生選課關係資料庫中的SC表;
  4. 執行下列語句,為基本表新增資料;

–以下為學生表的初始資料

insert into Student(sname,ssex,sno, sage, sdept) values('李勇','男','200215121',20,'CS');
insert into Student(sname,ssex,sno, sage, sdept) values('劉晨','女','200215122',19,'CS');
insert into
Student(sname,ssex,sno, sage, sdept) values('王敏','女','200215123',18,'MA'); insert into Student(sname,ssex,sno, sage, sdept) values('張立','男','200215125',19,'IS');

–以下為課程表的初始資料

insert into course(cno,cname,cpno,ccredit) values('6','資料處理',null,2);
insert into course(cno,cname,cpno,ccredit) values('2','數學'
,null,2); insert into course(cno,cname,cpno,ccredit) values('7','PASCAL語言','6',4); insert into course(cno,cname,cpno,ccredit) values('5','資料結構','7',4); insert into course(cno,cname,cpno,ccredit) values('1','資料庫','5',4); insert into course(cno,cname,cpno,ccredit) values('3','資訊系統','1',4); insert into course(cno,cname,cpno,ccredit) values('4','作業系統','6',3);

–以下為選修表的初始資料

insert into sc(sno,cno,grade) values('200215121','1',92);
insert into sc(sno,cno,grade) values('200215121','2',85);
insert into sc(sno,cno,grade) values('200215121','3',88);
insert into sc(sno,cno,grade) values('200215122','2',90);
insert into sc(sno,cno,grade) values('200215122','3',80);

5.修改Student表結構,為Student表格新增一個“入學時間”屬性,屬性名為Senrollment,各元組在屬性Senrollment的值是多少;

alter table student add Senrollment date;

6.修改Student表結構,把Ssex列的寬度設定為6個位元組;

alter table student modify ssex varchar(6);	

7.修改Student表結構,刪除Senrollment列(注:刪除SYS模式下表的列將被拒絕);

alter table student drop column  Senrollment;

8.建立檢視ds,該檢視包含所有選修了“資料庫”的學生資訊(如果提示沒有許可權,請使用管理員授予當前使用者Create view許可權);

//grant Create view to dblesson;  //sys授權
create view ds as select * from student where sno in 
(select sno from sc where cno in(select cno from course where cname= '資料庫' ));

9.建立檢視maleStudent,該檢視包含男學生所有資訊,通過檢視maleStudent更新基本表資料時必須保證學生性別為男;

create view maleStudent as select * from Student where ssex='男';

10.刪除檢視maleStudent;

drop view malestudent;

11.為Course表的CName列建立唯一索引,索引名稱為uniqueCname;

create UNIQUE index uniqueCname on course(CName);

12.試著為Course表的Cpno列建立唯一索引,索引名為indexCpno1,如果發生錯誤,請說明普通索引和唯一索引有何區別;

create UNIQUE index indexcpno1 on course(CPNO);
--ORA-01452: 無法 CREATE UNIQUE INDEX; 找到重複的關鍵字
--唯一索引只能對應唯一資料記錄.

13.為Cource表的Cpno列建立普通索引,索引名稱為indexCpno2;

create  index indexcpno2 on course(CPNO);

14.刪除索引indexCpno2;

drop index indexcpno2;

15.刪除基本表Student,如果發生錯誤,請分析原因;

drop table student;
--ORA-02449: 表中的唯一/主鍵被外來鍵引用
--因為主鍵被course引用.

16.刪除基本表SC;

drop table sc;

17.參考1.3節學生選課關係資料庫的表結構,列出各個關係表應有的主碼和外碼約束(文字回答即可);
表student的主碼為sno
表course的主碼為cno
表sc的主碼為sno,cno

18.檢視已建立的Student、SC、Course表的約束,如果某個表缺少應有的主碼或外碼約束,為該表新增缺失的主碼或外碼約束。
NULL

實驗內容第二部分
本部分實驗採用專案資訊管理關係資料庫,實驗前請執行附錄中專案資訊管理關係資料庫的DDL程式碼和“專案資訊管理關係資料庫初始化資料程式碼”建立基本表和插入初始化資料,後續實驗也都採用專案資訊管理關係資料庫。
1.查詢系號為“d001”的所有教師的教工號、名稱和工資;

select tno,tname,tsalary from teacher where dno='d001';

2.查詢工資在3000到5000之間的教師姓名、年齡(提示:可使用當前年份減去教師的出生年份,教師的出生年份可以使用函式extract(year from tbirthday)獲取);

select tname,2020-extract(year from tbirthday) as age from teacher where tsalary  between 3000 and 5000;

3.查詢參加了專案的教工的編號,排除相同的元素;

select UNIQUE tno from myproject;

4.查詢名字中包含字“小”的教工姓名、出生日期;

select tname,tbirthday from teacher where tname like'%小%';

5.查詢名字中第二個字為“小”的教工姓名、出生日期;

select tname,tbirthday from teacher where tname like'_小%';

6.查詢所有不姓“李”、並且姓名為三個字的教工姓名、性別;

select tname,tsex from teacher where tname like '___' and tname not like '李%';

7.查詢Department表有系主任的系號、系名稱;

select DNO,Dname from department;

8.查詢工資在4000以上或者性別為女的教師詳細資訊,按性別降序排列輸出;

select * from teacher where tsex='女' or tsalary>4000 order by tsex desc; 

9.查詢參與了專案的教工總人數;

select count(unique TNO) from myproject;

10.查詢“張三”負責的專案數量;

select count (*) from TM where tno in (select tno from teacher where tname='張三');

11.查詢所有教師的平均工資、工資總和、最高工資、最低工資;

select sum(tsalary),avg(tsalary),max(tsalary),min(tsalary) from teacher;

12.建立檢視departmentSalary,查詢各個系的教師的平均工資、工資總和、最高工資、最低工資;

create view departmentSalary as  select dno,avg(tsalary) as 平均工資,sum(tsalary) as 工資總和,
max(tsalary) as 最高工資,min(tsalary)as 最低工資 from teacher GROUP by dno ;

13.查詢各個系的詳細資訊,包括各個系的教師的平均工資、工資總和、最高工資、最低工資(提示:可以使用department表與檢視departmentSalary進行連線運算完成);

select * from departmentsalary;

14.查詢教師平均工資大於4500的系號、系名稱、平均工資(提示:要求不能使用檢視departmentSalary,可把department與teacher連線後再進行分組,然後使用having子句對分組進行篩選);

select department.dno,dname,avg(tsalary) as 平均工資 from department,teacher 
where department.dno=teacher.dno having avg(tsalary)>4500 group by department.dno,dname;

15.查詢教師參與專案的情況,列出教工號、姓名和專案名稱,沒有參與專案的教師也列出來(提示:用左外連線);

select teacher.tno,teacher.tname,myProject.pname from teacher left outer 
join myProject on(myProject.tno=teacher.tno);

16.查詢與“李小龍”工資相同的教師詳細資訊(要求分別使用自身連線、子查詢兩種查詢方法完成);
自身連線

select B.* from teacher A,teacher B where a.tsalary=b.tsalary and a.tname='李小龍'; 

子查詢

select * from teacher where tsalary in(select tsalary from teacher where tname='李小龍');

17.查詢參與了“雲端計算研究”並且工資在4000以上的教師詳細資訊;

select * from teacher where tsalary>4000 and tno in(select tno from myproject where pname='雲端計算研究' );

18.查詢小於或等於“同一系中教師平均工資”的教工號、姓名、年齡(提示:請參閱書本的“相關子查詢”示例);

select tno,tname,202-extract(year from tbirthday) as age from teacher x 
where tsalary<=(select avg(tsalary) from teacher y where x.dno=y.dno);

19.查詢比“計算機科學系”教師工資都高、並且不是“網路工程系”的教師資訊;

select * from teacher where dno in(select dno from department where not dname='“網路工程系' );

20.查詢沒有參與專案“p0001”的教工號、姓名;

select tno,tname from teacher where not tno in(select tno from department where dno='p001' );

21.查詢參與了所有專案的教師姓名;

select tname from teacher where not exists(select * from myproject where not exists
(select * from tm where tno=teacher.tno and pno=myproject.pno ));

22.查詢工資大於3500或者在計算機科學系工作的教師詳細資訊(要求使用關鍵字UNION);

select * from teacher where tsalary>3500 union select * from teacher 
where dno in(select dno from department where dname like '計算機科學系'); 
  1. 查詢工資大於3500並且不在計算機科學系工作的教師詳細資訊(要求使用關鍵字MINUS);
select * from teacher where tsalary>3500 minus select *from teacher where dno in 
(select dno from department where dname ='計算機科學系' )

實驗內容第三部分
1.列出Teacher表的所有約束,並說明每個約束的具體含義及其對錶列取值的影響;

Select table_name,constraint_name,constraint_type from user_constraints where table_name='TEACHER';

2.書寫SQL語句,在Teacher表中插入2條元組,元組內容任意設定,要求能取空值的列均設定為空(提示:如果插入失敗,則檢視是否滿足基本表的約束條件);

insert into teacher(tno, tname, tsex, tbirthday, dno) values('t111', '張一', '男',
To_date('7-7-1977', 'DD-mm-yyyy'), 'd001');

insert into teacher(tno, tname, tsex, tbirthday, dno) values('t222', '張二', '男',
To_date('7-7-1977', 'DD-mm-yyyy'), 'd001');

3.利用“create table teacher2 as select * from teacher”語句建立表teacher2,並列出Teacher2表的所有約束,比較Teacher2表與Teacher表的約束差異;

create table teacher2 as select * from teacher;

select table_name,constraint_name,constraint_type from user_constraints where table_name='TEACHER';

select table_name,constraint_name,constraint_type from user_constraints where table_name='TEACHER2';

4.任取teacher表中的一條元組,把這條元組分別插入到teacher2和Teacher表中,比較兩次插入操作的執行結果並分析原因(要求插入失敗時必須指出違反了哪類完整性約束條件);

insert into teacher(tno, tname, tsex, tsalary, tbirthday, dno) values('t444', '張四', '男',
5000, To_date('7-7-1977', 'DD-mm-yyyy'), 'd001');

insert into teacher2(tno, tname, tsex, tsalary, tbirthday, dno) values('t444', '張四', '男',
5000, To_date('7-7-1977', 'DD-mm-yyyy'), 'd001');
--錯誤違反唯一約束條件 實體完整性
insert into teacher2(tno,tname,tsex,tsalary,tbirthday,dno) 

5.使用帶子查詢的插入語句把teacher表中的所有男教師插入到teacher2表中;

select tno,tname,tsex,tsalary,tbirthday,dno from teacher where tsex like '男';

6.為表Teacher新增check約束,使性別的取值只能為“男”或者“女”;

alter table teacher add check(tsex='男' or tsex='女');

7.刪除teacher2表中工資等於6000的教師資訊;

delete from teacher2 where tsalary=6000;

8.刪除teacher2表中“計算機科學系”的所有教師;

delete from teacher2 where dno in(select dnofrom department where dname='計算機科學系');

9.刪除teacher2表中的所有教師;

delete  from teacher2;

10.修改teacher2表,使列tno為主碼,主碼約束名字為PK_teacher2;

alter table teacher2 add  primary key(tno) ;

11.為teacher2表新增唯一約束,使tname的取值不能重複;

alter table teacher2 add unique(tname);

12.修改teacher2表,使列dno成為外碼,引用department表的主碼dno,當刪除department表中的元組時,級聯刪除Teacher2表中的元組(提示:刪除並重新建立外碼約束,使用ON DELETE CASCADE選項);

alter table teacher2 add foreign key(dno) references department(dno) on delete cascade;

13.在department表中插入一個新系,系號為“xyz”,在Teacher2表中為該新系新增兩個教師資訊;

insert into department(dno,dname) values('xyz','xyz');

insert into teacher2(tno, tname, tsex, tsalary, tbirthday, dno) values('t001', 'Boy', '男', 6000, To_date('1-1月-1990', 'DD-mon-yyyy'), 'xyz');

insert into teacher2(tno, tname, tsex, tsalary, tbirthday, dno) values('t002', 'girl', '女', 5000, To_date('1-1月-1990', 'DD-mon-yyyy'), 'xyz');

14.分別寫出刪除department表中系號為d001和xyz的記錄的SQL語句並執行,比較並分析執行結果(提示:在Teacher表和Teacher2表中的外碼定義是不同的);

delete from department where dno='d001';
--違反完整約束條件
delete from department where dno='xyz';
--成功刪除

15.在tm中插入一條元組,只設置tno、pno的值;

insert into tm(tno,pno) values('t006','p0001');

16.給teacher表中的所有教師的工資增加100;

update teacher set tsalary =tsalary+100 where dno in( select dno from department);

17.給teacher表中的“計算機科學系”教師的工資增加100;

update teacher set tsalary =tsalary+100 where dno in( select dno from department where dname='計算機工程系');

18.建立兩個檢視VT、VT2,兩個檢視均為包含所有teacher表的男教師的資訊,但檢視VT2的定義帶有with check option選項,設定一條女教師資訊記錄,指出通過哪個檢視可以成功插入記錄,並說明with check option選項的作用;

create view VT as select * from teacher where tsex='男';

create view VT2 as select * from teacher where tsex='男' with check option;

insert into VT(tno, tname, tsex, tsalary, tbirthday, dno) values('t010', '冰冰', '女', 3000, To_date('1-1月-1990', 'DD-mon-yyyy'), 'd001'); 
--插入時需要注意不違反唯一約束條件 (DBLESSON.PK_TEACHER)
insert into VT2(tno, tname, tsex, tsalary, tbirthday, dno) values('t010', '冰冰', '女', 3000, To_date('1-1月-1990', 'DD-mon-yyyy'), 'd001'); 
--檢視 WITH CHECK OPTION where 子句違規

實驗總結
總結實驗過程中涉及到的知識點、實驗過程中遇到的問題及解決方法。
(1)基礎語法學習select選擇,alter table對錶進行搞作,insert插入,drop刪除,delect刪除,update更新,create table建立表,create view建立檢視
(2)在create 新表時,需要區分select * from有關聯引用 和select 所有列from 沒關聯傳引數
(3)需要提前注意限制詞,插入時需要注意不違反唯一約束條件
(4)在多個表中,Select選擇指定關鍵詞選擇資訊,需要邏輯清晰意識到各個表之間的邏輯關係巧妙運用交併補運算
(5)存在級聯關係的時候,不可以直接簡單的刪除基表的資訊,需要從其他表逐個刪除,也可以用級聯刪除強制刪除。
(6)對已存在表插入資訊時需要注意values後面跟隨的關鍵字對齊
(7)總和sum(),平均值avg(),最大值max(),最小值min(),差集minus
(8)自身查詢時可以對同個表引用兩個物件,子查詢可以在where中再對自身進行選擇