狠狠干影院/欧美午夜电影在线观看/高黄文/国产精品一区二区在线观看完整版

《數據庫概論》實驗報告

| 瀏覽次數:

 《數據庫系統概論》

 實驗報告書

 專業班級

 學

 號

 姓

 名

 指導教師

 實驗一:數據定義語言

 [ 實驗日期

 1 ]

  2011 年

 10 月

 22

 日

 [ 實驗目的

 ] ]

 熟悉 Oracle 上機環境及 Oracle 客戶端的配置;熟練掌握和使用 DDL 語言,建立、修改和刪除數據庫表、主鍵、外鍵約束關系和索引。

 [ 實驗內容

 ] ]

 1 1 .

 L SQL 數據定義語句:

 例 1-1:

 (建立數據庫表)

 建立教學數據庫的四個數據庫表,其中 Student 表中不包含 SSEX(C,2) 字段,Sname 字段為 Sname(C,8)且可為空。

 create table Student (Sno char(5),

 Sname char(8),

 Sdept char(2) not null unique,

 Sclass char(2) not null unique,

 Sage number(2),

 constraint Sno_pk PRIMARY KEY(Sno)) ;

 create table Course (Cno char(3) PRIMARY KEY,

 Cname char(16),

  Ctime number(3)) ;

  create table SC (Sno char(5),

 Cno char(2),

 Grade numble(3),

 constraint SC_pk PRIMARY KEY(Sno,Cno),

 constraint Sno_fk foreign KEY(Sno) references Student(sno), constraint Sno_pk PRIMARY KEY(Sno)) ;)

 create table Teach (Tname char(8),

 Tsex char(2),

  Cno char(3),

 Tdate date,

 Tdept char(2),

 constraint Teach_pk PRIMARY KEY(Tname,Cno,Tdept),

 constraint Cno_fk foreign KEY(Cno) references Course(Cno));

 create table Score (Sno char(5),

 Cno char(3),

 Score number(5,2),

 constraint Score_pk PRIMARY KEY(Sno,Cno),

 constraint Sno_fk foreign KEY(Sno) references Student(Sno),

 constraint Cno_cfk foreign KEY(Cno) references Course(Cno));

  例 1-2:

 (修改數據庫表)

 在 Student 表中增加 SEX(C,2) 字段。

 alter table Student add Sex char(2) ;

  例 1-3:

 (修改列名)

 將 Student 表中列名 SEX 修改為 SSEX。

 alter table Student rename column Sex to Ssex ;

  例 1-4:

 (修改數據庫表)

 將 Student 表中把 Sname 字段修改為 Sname(C,10)且為非空。

 alter table student modify(sname not null);

  例 1-5:

 (建立索引)

 為 Score 表按課程號升序、分數降序建立索引,索引名為 SC_GRADE。

 create unique index SC_GRADE on Score(Cno asc,Score desc) ;

  例 1-6:

 (刪除索引)

 刪除索引 SC_GRADE。drop index SC_GRADE;

  例 1-7:

 (建立數據庫表)

 建立數據庫表 S1(SNO,SNAME,SD,SA),其字段類型定義與 Student 表中的相應字段(SNO,SNAME,SDEPT,SAGE)的數據類型定義相同。

 create table S1

 (Sno char(5),

 Sname char(10),

 Sd char(2),

 Sage number(2));

  例 1-8:

 (修改數據庫表)

 刪除成績表 Score 的參照完整性約束關系。

 alter table score drop constraint cno_cfk;

  例 1-9:

 (修改數據庫表)

 添加成績表 Score 的參照完整性約束關系。

 alter table score add constraint Sno_fk foreign KEY(sno) references Student(Sno); alter table score add constraint cno_cfk foreign KEY(cno) references Course(cno);

  例 1-10:

 (修改數據庫表名)

 將數據庫表 S1 改名為 Student_Temp。

 rename s1 to Student_Temp;

 [ 實驗要求

 ] ]

 ①

 熟悉 Oracle 上機環境,掌握 Oracle 客戶端的配置; ②

 建立數據庫表,修改數據庫表結構,建立、刪除索引;

 [ 實驗方法

 ] ]

 ①

 按照附錄中的操作步驟進行客戶端的配置;

  ②

 將實驗需求用 SQL 語句表示;

 ③

 執行 SQL 語句;

  ④

 查看執行結果,如果結果不正確,進行修改,直到正確為止。

 [ [

  實驗總結

 ] ]

 經過這次實驗使我對 sql 語句對表的創建,修改和刪除等操作更加熟練。

  實驗二:數據操縱語言

 [ 實驗日期

 ]

 2011

  年

 10

 月

 22

 日

 [ 實驗目的

 ] ]

 在實驗一的基礎上,熟練掌握和使用 DML 語言,對數據進行增加、修改和刪除操作。

 [ 實驗內容

 ] ]

 2 2 .

 L SQL 數據操縱語句:

 例 2-1:(插入數據)

 按前面各表中的數據分別插入到教學數據庫的四個數據庫表中。

 insert into Student values("96001","馬小燕","cs","01",21,"女"); insert into Student values("96002","黎明","cs","01",18,"男"); insert into Student values("96003","劉東明","ma","01",18,"男"); insert into Student values("96004","趙志勇","is","02",20,"男"); insert into Student values("97001","馬蓉","ma","02",19,"女"); insert into Student values("97002","李成功","cs","01",20,"男"); insert into Student values("97003","黎明","is","03",19,"女"); insert into Student values("97004","李麗","cs","02",19,"女"); insert into Student values("96005","司馬志明","cs","02",18,"男");

  insert into course values("001","數學分析",144);

 insert into course values("002","普通物理",144);

 insert into course values("003","微機原理",72);

 insert into course values("004","數據結構",72);

 insert into course values("005","操作系統",64);

 insert into course values("006","數據庫原理",64);

 insert into course values("007","DB_Design",48); insert into course values("008","程序設計",56);

  insert into teach values("王成剛","男","004","05-9 月-1999","cs"); insert into teach values("李正科","男","003","05-9 月-1999","cs"); insert into teach values("嚴敏","女","001","05-9 月-1999","ma"); insert into teach values("趙高","男","004","05-9 月-1999","is"); insert into teach values("李正科","男","003","23-2 月-2000","ma"); insert into teach values("劉玉蘭","女","006","23-2 月-2000","cs"); insert into teach values("王成剛","男","004","23-2 月-2000","is"); insert into teach values("馬悅","女","008","06-9 月-2000","cs"); insert into teach values("王成剛","男","007","05-9 月-1999","cs");

 insert into score values("96001","001",77.5); insert into score values("96001","003",89); insert into score values("96001","004",86); insert into score values("96001","005",82); insert into score values("96002","001",88); insert into score values("96002","003",92.5); insert into score values("96002","006",90); insert into score values("96005","004",92); insert into score values("96005","005",90); insert into score values("96005","006",89); insert into score values("96005","007",76); insert into score values("96003","001",69); insert into score values("97001","001",96); insert into score values("97001","008",95);

 insert into score values("96004","001",87); insert into score values("96003","003",91); insert into score values("97002","003",91); insert into score(sno,cno) values("97002","004"); insert into score values("97002","006",92); insert into score values("97004","005",90); insert into score values("97004","006",85); insert into score values("97004","008",75); insert into score values("97003","001",59); insert into score values("97003","003",58);

  例 2-2:(多行插入)

 將表 Student 中在計算機系(‘CS’)的學生數據插入到表 Student_Temp 中。

 insert into Student_Temp values("96001","馬小燕","cs",21); insert into Student_Temp values("96002","黎明","cs",18); insert into Student_Temp values("97002","李成功","cs",20); insert into Student_Temp values("97004","李麗","cs",19); insert into Student_Temp values("96005","司馬志明","cs",18); 例 2-3:(利用查詢來實現表的定義與數據插入)

 求每一個學生的平均成績,把結果存入數據庫表Student_Gr 中。

 create table Student_Gr (sno char(5)not null,

 avgscore number(5,2)); insert into Student_Gr(sno,avgscore) select sno,avg(score) from score group by sno; 例 2-4:

 (修改數據)

 將 Student_Temp 表中所有學生的年齡加 2。

 update Student_Temp set sage =sage+2; 例 2-5:

 (修改數據)

 將 Course 表中‘程序設計’課時數修改成與‘數據結構’的課時數相同。

 update course set ctime=(select ctime from course where cname="數據結構"where cname="程序設計"; 例 2-6:

 (插入數據)

 向 Score 表中插入數據(‘98001’, ‘001’, 95),根據返回信息解釋其原因。

 insert into score values("98001","001",95); 因為引入了外鍵 sno,來源于表 Student,而在 Student中沒有 98001 這個學號,故無法插入。

  例 2-7:

 (插入數據)

 向 Score 表中插入數據(‘97001’, ‘010’, 80),根據返回信息解釋其原因。

 insert into score values("97001","010",80); 因為引入了外鍵 cno,來源于表 course,而在 course中沒有 010 這個課程號,故無法插入。

 例 2-8:

 (刪除數據)

 刪除 Student 表中學號為‘96001’的學生信息,根據返回信息解釋其原因。

 delete from student where sno="96001";違反完整性約束條件,已找到子記錄日志 例 2-9:

 (刪除數據)

 刪除 Course 表中課程號為‘003’ 的課程信息,根據返回信息解釋其原因。

 delete from course where cno="003"; 違反完整性約束條件,已找到子記錄日志

  例 2-10:

 (刪除數據)

 刪除學生表 Student_Temp 中學號以 96 打頭的學生信息。

 delete from Student_Temp where sno like"96___"; ( 此 操作 后 ,注意用 K ROLLBACK 回退可能更新的 數據)

 例 2-11:

 (刪除數據)

 刪除數據庫表 Student_Temp 中所有學生的數據。

 delete from student_temp;

  例 2-12:(刪除表)

 刪除數據庫表 Student_Temp 和 Student_Gr。

 drop table student_temp; drop table student_gr; [ 實驗要求

 ] ]

 對數據庫表進行插入、修改和刪除數據的操作。

 [ 實驗方法

 ] ]

 ①

 按照附錄中的操作步驟進行客戶端的配置;

  ②

 將實驗需求用 SQL 語句表示;

 ③

 執行 SQL 語句;

  ④

 查看執行結果,如果結果不正確,進行修改,直到正確為止。

 [ [

  實驗總結

 ] ]

  通過這次實驗使我對插入數據更加熟練,對約束條件的作用也有了更深的理解。

 實驗三:數據查詢語言

 [ 實驗日期

 ]

 2 011

  年

 10

  月

 22

  日

 [ 實驗目的

 ] ]

 體會 SQL 語言數據查詢功能的豐富和復雜。

 [ 實驗內容

 ] ] 3 3 .

 L SQL 數據查詢語句:

 例 3-1:

 (選擇表中的若干列)

 求全體學生的學號、姓名、性別和年齡。

 select sno,sname,ssex,sage from student;

  例 3-2:

 (不選擇重復行)

 求選修了課程的學生學號。

 select distinct sno from score;

  例 3-3:

 (選擇表中的所有列)

 求全體學生的詳細信息。

 select *from student;

 例 3-4:

 (使用表達式)

 求全體學生的學號、姓名和出生年份。

 select sno,sname,2011-sage as "birth" from student;

  例 3-5:

 (使用列的別名)

 求學生的學號和出生年份,顯示時使用別名“學號”和“出生年份”。

 select sno as "學號",2011-sage as"出生年份" from student; 例 3-6:

 (比較大小條件)

 求年齡大于 19 歲的學生的姓名和年齡。

 select sname,sage from student where sage>19;

  例 3-7:

 (比較大小條件)

 求計算機系或信息系年齡大于 18 歲的學生的姓名、系和年齡。

 select sname,sdept,sage from student where sage>19 and sdept in ("cs","is");

 例 3-8:

 (確定范圍條件)

 求年齡在 19 歲與 22 歲(含 20 歲和 22 歲)之間的學生的學號和年齡。

  select sno,sage from student where sage between 19 and 22; 例 3-9:

 (確定范圍條件)

 求年齡不在 19 歲與 22 歲之間的學生的學號和年齡。

 select sno,sage from student where sage not between 19 and 22; 例 3-10:(確定集合條件)

 求在下列各系的學生信息:數學系、計算機系。

 select *from student where sdept in ("ma","cs");

  例 3-11:(確定集合條件)

 求不是數學系、計算機系的學生信息。

 select *from student where sdept not in ("ma","cs");

 例 3-12:(匹配查詢)

 求姓名是以“李”打頭的學生。

 select sname from student where sname like "李%";

  例 3-13:(匹配查詢)

 求姓名中含有“志”的學生。

 select sname from student where sname like "%志%";

  例 3-14:(匹配查詢)

 求姓名長度至少是三個漢字且倒數第三個漢字必須是“馬”的學生。

 select sname from student where sname like "%馬__"; 例 3-15:(匹配查詢)

 求選修課程 001 或 003,成績在 80 至 90 之間,學號為 96xxx 的學生的學號、課程號和成績。select sno,cno,score from score where cno in ("001","003") and score between 80 and 90 and sno like"96___";

  例 3-16:(匹配查詢)

 求課程名中包含 ’_’ 字符的課程號、課程名和學時數。

 select cno,cname,ctime from course where Cname like"%/_%"escape"/";

  例 3-17:(涉及空值查詢)

 求缺少學習成績的學生的學號和課程號。

 select sno,cno from score where score is null;

  例 3-18:(控制行的顯示順序)

 求選修 003 課程或 004 課程的學生的學號、課程號和分數,要求按課程號升序、分數降序的順序顯示結果。

 select sno,cno,score from score where cno in ("003","004") order by cno asc,score desc;

  例 3-19:(組函數)

 求學生總人數。select count(sno) from student;

  例 3-20:(組函數)

 求選修了課程的學生人數。select count(distinct sno) from score;

  例 3-21:(組函數)

 求計算機系學生的平均年齡。select avg(sage) from student where sdept="cs";

 例 3-22 :

 ( 組 函 數 )

 求 選 修 了 課 程 001 的 最 高 、 最 低 與 平 均 成 績 。

 select max(score),min(score),avg(score)from score where cno="001"; 例 3-23:(分組查詢)

 求各門課程的平均成績與總成績。

 select avg(score),sum(score)from score group by cno;

 例 3-24:(分組查詢)

 求各系、各班級的人數和平均年齡。

 select sdept,sclass,count(sno),avg(sage)from student group by sdept,sclass;

  例 3-25:(分組查詢)

 輸入以下查詢語句并執行,觀察出現的其結果并分析其原因。

  SELECT SNAME,SDEPT,COUNT(*)FROM STUDENT WHERE SDEPT=’CS’ GROUP BY SDEPT;

  例 3-26:(分組查詢)

 分析以下語句為什么會出現錯誤。并給出正確的查詢語句。

 SELECT SAGE FROM STUDENT GROUP BY SNO; 不是 group by 表達式;SELECT SAGE FROM STUDENT order by SNO;

 例 3-27:(分組查詢)

 求學生人數不足 3 人的系及其相應的學生數。

 select sdept,count(sno) from student group by sdept having count(sno)<3;

  例 3-28:(分組查詢)

 求各系中除 01 班之外的各班的學生人數。

 select sdept,sclass,count(sno) from student where sclass<>"01" group by sdept,sclass ;

  例 3-29:(涉及空值的查詢)

 分別觀察各組函數、行的顯示順序以及分組查詢與空值的關系。

 例 3-30:(連接查詢)

 求選修了課程 001 且成績在 70 分以下或成績在 90 分以上的學生的姓名、課程名稱和成績。

 select sname,cname,score from student,course,score

  where

 student.sno=score.sno

 and course.cno=score.cno

  and score.cno="001"

  and score.score not between 70 and 90;

 例 3-31:(連接查詢與表的別名)

 求選修了課程的學生的學生姓名、課程號和成績。

 select sname,cno,score from student,score where student.sno=score.sno;

  例 3-32:(自身連接查詢)

 求年齡大于 ’李麗’ 的所有學生的姓名、系和年齡。

 select X.sname,X.sdept,X.sage from student X,student Y where x.sage>y.sage and y.sname="李麗";

  例 3-33:(外部連接查詢)

 求選修了課程 002 或 003 的學生的學號、課程號、課程名和成績,要求必須將 002 和 003 課程的相關信息顯示出來。

 select course.cno,course.cname,course.ctime,score.sno,score.score from course,score

 where score.cno in("002","003");

 例 3-34:(子查詢)

 求與 ‘黎明’ 年齡相同的學生的姓名和系。

 select sname,sdept from student where sname<>"黎明" and sage in(select sage from student where sname="黎明");

 例 3-35:(子查詢)

 求選修了課程名為 ’數據結構’ 的學生的學號和姓名。

 select sname,student.sno from student,course,score where student.sno=score.sno

 and course.cno=score.cno and course.cname="數據結構";

 例 3-36:(子查詢 ANY)

 求比數學系中某一學生年齡大的學生的姓名和系。

 select sname,sdept from student

  where sage>any(select sage from student where sdept="ma")

  and sdept<>"ma";

 例 3-37:(子查詢 ALL)

 求比數學系中全體學生年齡大的學生的姓名和系。

 select sname,sdept from student

  where sage>all(select sage from student where sdept="ma")

  and sdept<>"ma";

 例 3-38:(子查詢 EXISTS)

 求選修了課程 004 的學生的姓名和系。

 select sname,sdept from student where exists

 (select * from score where sno=student.sno

  and cno="004"); 例 3-39:(返回多列的子查詢)

 求與 ‘李麗’ 同系且同齡的學生的姓名和系。

 select sname,sdept from student where sdept=(

 select sdept from student where sname="李麗")

 and sage=(select sage from student where sname="李麗");

 例 3-40:(多個子查詢)

 求與 ‘‘黎明’ 同系,且年齡大于 ‘李麗’ 的學生的信息。

 select sdept from student where sname="黎明")

 and sage>(select sage from student where sname="李麗")

 and sname<>"李麗" and sname<>"黎明";

 例 3-41:(子查詢中使用表連接)

 求數學系中年齡相同的學生的姓名和年齡。

 select sname,sage from student x where x.sdept="ma" and sage in(select sage from student

 y where y.sdept="ma" and x.sno!=y.sno);

  例 3-42:(連接或嵌套查詢)

 檢索至少選修王成剛老師所授課程中一門課程的女學生姓名。

  select sname from student where ssex="女" and sno in(select sno from score,teach where

 score.cno=teach.cno and teach.tname="王成剛");

 例 3-43:(嵌套與分組查詢)

 檢索選修某課程的學生人數多于 3 人的教師姓名。

 select tname from Teach x where count(select score.cno from teach y,score

  where y.cno=score.cno and y.tname=x.tname)>3;

 例 3-44:(集合查詢)

 列出所有教師和同學的姓名和性別。

 select distinct sname,ssex,tname,tsex from student s1, teach s2 where s1.sdept=s2.tdept;

 例 3-45:(相關子查詢)

 求未選修課程 004 的學生的姓名。

 select sname from student where

 exists(select * from score where cno<>"004");

 例 3-46:(相關子查詢)

 求選修了全部課程的學生的姓名。

  select sname from student

  where

 not exists (select * from course

 where not exists

  (select * from score

 where sno=student.sno

  and cno=course.cno)); 例 3-47:(相關子查詢)

 求至少選修了學生 ‘96002’ 所選修的全部課程的學生的學號。

 select distinct sno from score s1

 where not exists

  ( select * from

 score s2

 where s2.sno="96002"

  and not exists

 ( select * from score s3

 where sno=s1.sno

  and cno=s2.cno))and sno!="96002";

 例 3-48:(相關子查詢)

 求成績比所選修課程平均成績高的學生的學號、課程號、和成績。

 select sno,cno,score from score s1

 where not exists (select

 s2.cno,avg(s2.score) from score s2 group by cno having s1.score<avg(s2.score));

 例 3-49:(相關子查詢)

 查詢被一個以上的學生選修的課程號。

 select cno,count(cno) from score group by cno having count(cno)>1; 例 3-50:(相關子查詢)

 查詢所有未選課程的學生姓名和所在系。

 select sname from student where sno not in (select sno from score where cno="004");

 [ 實驗要求

 ] ]

 對數據庫表進行各種查詢操作。

 [ 實驗方法

 ] ]

 ①

 將實驗需求用 SQL 語句表示;

  ②

 執行 SQL 語句;

  ③

 查看執行結果,如果結果不正確,進行修改,直到正確為止。

 [ [

  實驗總結

 ] ]

 這次實驗使我對 SQL 語句查詢方面得到了很多的鍛煉,很多我不熟的,不會弄的查詢這次通過慢慢摸索總算知道了查詢方法。

 實驗四:視圖、授權控制與事務處理

 [ 實驗日期

 ]

 2011

  年

 10

 月

 22 日

 [ 實驗目的

 ] ]

 通過實驗進一步理解視圖的建立和更新、數據庫的權限管理和事務處理功能。

 [ 實驗內容

 ] ]

 4 4 .

 L SQL 視圖 的定義與操縱 :

 例 4-1:

 (建立視圖)

 建立計算機系的學生的視圖 STUDENT_CS。

 create view student_cs with check option;

  例 4-2:

 (建立視圖)

 建立由學號和平均成績兩個字段的視圖 STUDENT_GR。

 create view student_gr(sno,avg(score));

 例 4-3:

 (視圖查詢)

 利用視圖 STUDENT_CS,求年齡大于 19 歲的學生的全部信息。

 select *from student_cs where sage>19; 例 4-4:

 (視圖查詢)

 利用視圖 STUDENT_GR,求平均成績為 88 分以上的學生的學號和平均成績。

 select sno,avg(score) as "平均成績"from student_gr where avg(score)>88; 例 4-5:

 (視圖更新)

 利用視圖 STUDENT_CS,增加學生( ‘96006’,‘張然’,‘CS’,‘02’,‘男’,19 )。

  insert into student_cs values("96006","張然","cs","02","男",19); 例 4-6:

 (視圖更新)

 利用視圖 STUDENT_CS,將學生年齡增加 1 歲。觀察其運行結果并分析原因。

  update student_cs set sage=sage+1; 例 4-7:

 (視圖更新)

 利用視圖 STUDENT_GR,將平均成績增加 2 分。觀察其運行結果并分析原因。

 update student_gr set avg(score)=avg(score)+2; 例 4-8:

 (視圖更新)

 刪除視圖 STUDENT_CS 中學號為 ‘96006’ 的學生的全部數據。

 delete from student_cs where sno="96006"; 例 4-9:

 (視圖更新)

 刪除視圖 STUDENT_GR 的全部數據。

 delete student_gr;

  例 4-10:(刪除視圖)

 刪除視圖 STUDENT_CS 和 STUDENT_GR。

 drop view student_cs; drop view student_gr; 5 5 .

 L SQL 數據控制語句:

  例 5-1:

 (授權)

 給左右鄰近同學(用戶)授予在表 Student 上的 SELECT 權限,并使這兩個用戶具有給其他用戶授予相同權限的權限。

 grant select on student to u74003,u74024 with grant option;

  例 5-2:

 (授權)

 給鄰近同學(用戶)授予 Teach 表上的所有權限。

 grant all on teach to u74003;

 例 5-3:

 (授權)

 給所有用戶授予 Score 表上的 SELECT 權限。

 grant select on score to public; 例 5-4:

 (授權驗證)

 觀察左右鄰近同學查詢你所授權的表中的內容。

  例 5-5:

 (收回授權)

 收回上面例子中的所有授予的權限。

 revoke all privileges from public; revoke select on student from u74003,u74024; revoke all on teach from u74003; revoke select on score from public; 6 6 .

 L SQL 事務處理:

  例 6-1:

 (事務回退)

 將課程名稱表中的 ‘程序設計’ 課程學時數修改為 80、‘微機原理’ 課程學時數修改為 70 學時,查詢全部課程的總學時數后,取消所有修改(ROLLBACK)。再次查詢全部課程的總學時數。注意比較分析兩次查詢的結果。

 例 6-2:

 (事務提交)

 將課程名稱表中的 ‘程序設計’ 課程學時數修改為 80、‘微機原理’ 課程學時數修改為 70 學時,查詢全部課程的總學時數后,確認所有修改(COMMIT)。再次查詢全部課程的總學時數。注意比較分析兩次查詢的結果。

 6-1 圖

 6-2 圖

  [ 實驗要求

 ] ]

 ① 建立視圖,視圖查詢,視圖更新; ② 給某一或全部用戶授權和收回授權; ③ 事務回退,事務提交。

  [ 實驗方法

 ] ]

 ①

 將實驗需求用 SQL 語句表示;

  ②

 執行 SQL 語句;

  ③

 查看執行結果,如果結果不正確,進行修改,直到正確為止。

 [ [

  實驗總結

 ] ]

 通過實驗我加深了對視圖的理解,并且懂得了如何授權,還知道了事務回退和提交語句的作用。

  實驗五:e Oracle 存儲過程與觸發器

 [ 實驗日期

 ]

  2010

 年

 10

 月

 22

 日

 [ 實驗目的

 ] ]

 通過實驗進一步理解和掌握 Oracle 數據庫的存儲過程和觸發器。

 [ 實驗內容

 ] ]

 4 4 .

 存儲過程與觸發器 :

 例 7-1:

 (存儲過程) 創建一個顯示學生總人數的存儲過程。

 set serveroutput on create or replace

 procedure student_cnt

  as

 a number;

 begin

 select count(sno) into a from student;

 dbms_output.put_line("學生總人數:");

 dbms_output.put_line(a);

 end; 例 7-2:

 (存儲過程) 創建顯示學生信息的存儲過程 STUDENT_LIST,并引用 STU_COUNT 存儲過程。

 create or replace procedure student_list as

  begin

  student_cnt;

  end; 例 7-3:

 (存儲過程) 創建一個顯示學生平均成績的存儲過程。

 set serveroutput on

 create or replace procedure stu_avg(

 p1 in student.sno%type)

 as a number;

 begin

 select avg(score) into a from score where sno=p1 group by(sno);

 dbms_output.put_line(a);

 end; 例 7-4:

 (存儲過程) 創建顯示所有學生平均成績的存儲過程。

 set serveroutput on create or replace procedure all_avg as

 cursor stu_avg is select sno,avg(score) as a from score

 group by(sno); data stu_avg%rowtype; begin

 open stu_avg; loop fetch stu_avg into data; exit when stu_avg%notfound; dbms_output.put_line(data.sno||"

 "||data.a);

 end loop; close stu_avg; end; 例 7-5:

 (修改數據庫表) 在 Student 表中增加 SAVG(N,6,2) 字段。

 alter table student add savg number(6,2);; 例 7-6:

 (存儲過程) 創建存儲過程,計算每個學生的平均成績保存到學生表 SAVG 字段中。

 set serveroutput on

  create or replace procedure cal_avg as

 cursor c_avg is select sno,avg(score) as a from score

 group by(sno);

  data c_avg%rowtype;

 begin

  open c_avg;

  loop

 fetch c_avg into data;

 exit when c_avg%notfound;

  update student set savg=data.a

 where sno=data.sno;

  end loop;

  close c_avg;

  end; 例 7-7:

 (觸發器) 當更新學生成績表 SCORE 中的學生成績時,自動計算該學生的平均成績保存到學生表 SAVG 字段中。

 set serveroutput on

 create or replace trigger sc_avg before insert or update on score

 referencing new as new old as old

 for each row

 declare

 pscore number;

  n number;

 news number;

 begin

  select sum(score) into pscore from score where sno=:new.sno;

  select count(*) into n from score where sno=:new.sno;

  update student set savg=(pscore+:new.score)/(n+1) where sno=:new.sno;

  end; 例 7-8:

 (觸發器) 創建包含插入、刪除、修改多種觸發事件的觸發器 DML_LOG,對 SCORE 表的操作進行記錄。用 INSERTING、DELETING、UPDATING 謂詞來區別不同的 DML 操作。

 先創建事件記錄表 LOGS,該表用來對操作進行記錄。該表的字段含義解釋如下:

  LOG_ID:操作記錄的編號,數值型,它是該表的主鍵,自動增 1,可由序列自動生成。

  LOG_TABLE:進行操作的表名,字符型,非空,該表設計成可以由多個觸發器共享使用。比如我們可以為 Student 表創建類似的觸發器,同樣將操作記錄到該表。

 LOG_DML:操作的動作,即 INSERT、DELETE 或 UPDATE 三種之一。

 LOG_KEY_ID:操作時表的主鍵值,數值型。之所以記錄表的主鍵,是因為主鍵是表的記錄的惟一標識,可以識別是對哪一條記錄進行了操作。對于 Score 表,主鍵是由 SNO_CNO 構成。

  LOG_DATE:操作的日期,日期型,取當前的系統時間。

 LOG_USER:操作者,字符型,取當時的操作者賬戶名。比如登錄 SCOTT 賬戶進行操作,在該字段中,記錄賬戶名為 SCOTT。

 [ 實驗要求

 ] ]

 ① 創建、調試和修改、調用和執行 Oracle 存儲過程; ② 創建、調試和修改、測試 Oracle 觸發器。

  [ 實驗方法

 ] ]

 ①

 將實驗需求用 SQL 語句表示;

  ②

 執行 SQL 語句;

  ③

 查看執行結果,如果結果不正確,進行修改,直到正確為止。

 [ [

  實驗總結

 ] ]

 這次實驗使我加深了對觸發器的認識。開始編寫觸發器時覺得很困難,都不知道怎么顯示,而且發現“/”符號通過復制弄到 SQL plus 中時不能用,必須自己輸進去。

推薦訪問: 概論 實驗 數據庫

【《數據庫概論》實驗報告】相關推薦

工作總結最新推薦

NEW