【正文】
均成績+2。不能對含有聚合函數的視圖進行更新操作,因為更新操作的結果是會反映到基本表上的,而更新聚合函數無法得知更新基本表的那個元件 例48: (視圖更新) 刪除視圖STUDENT_CS中學號為 ‘96006’ 的學生的全部數據。delete from student_cs where sno=39。9600639。 例49: (視圖更新) 刪除視圖STUDENT_GR的全部數據。delete from student_gr。 例410:(刪除視圖) 刪除視圖STUDENT_CS和STUDENT_GR。drop view student_cs。drop view student_gr。5. SQL數據控制語句: 例51: (授權) 給左右鄰近同學(用戶)授予在表Student上的SELECT權限,并使這兩個用戶具有給其他用戶授予相同權限的權限。create user user1 identified by user1。grant select on student to user1 with grant option。 例52: (授權) 給鄰近同學(用戶)授予Teach表上的所有權限。grant all on teach to user1。 例53: (授權) 給所有用戶授予Score表上的SELECT權限。grant select on score to public。 例54: (收回授權) 收回上面例子中的所有授予的權限。revoke all on teach from user1。revoke select on score from public。revoke select on student from user1。6. SQL事務處理: 例61: (事務回退) 將Course表中的 ‘程序設計’ 課程學時數修改為80、 ‘編譯原理’ 課程學時數修改為70學時,查詢全部課程的總學時數后,取消所有修改(ROLLBACK)。再次查詢全部課程的總學時數。注意比較分析兩次查詢的結果。update course set ctime=80 where ame=39。程序設計39。update course set ctime=70 where ame=39。編譯原理39。select ame as 課程,ctime as 課時 from course 。rollback。select ame as 課程,ctime as 課時 from course 。 例62: (事務提交) 將Course表中的 ‘程序設計’ 課程學時數修改為80、 ‘編譯原理’ 課程學時數修改為70學時,查詢全部課程的總學時數后,確認所有修改(COMMIT)。再次查詢全部課程的總學時數。注意比較分析兩次查詢的結果。update course set ctime=80 where ame=39。程序設計39。update course set ctime=70 where ame=39。編譯原理39。select ame as 課程,ctime as 課時 from course 。mit。select ame as 課程,ctime as 課時 from course 。實驗四:存儲過程與觸發(fā)器[ 實驗日期 ] 年 月 日[ 實驗目的 ]通過實驗進一步理解和掌握數據庫的存儲過程和觸發(fā)器。[ 實驗內容 ]7.索引與數據庫完整性 例71: (建立索引) 為Score表按課程號升序、分數降序建立索引,索引名為SC_GRADE。create unique index sc_grade on score(sno asc,score desc)。 例72: (刪除索引) 刪除索引SC_GRADE。drop index sc_grade。例73: (修改數據庫表) 添加成績表Score的參照完整性約束關系。alter table score add constraint fk_sno foreign key(sno) referencesstudent(sno) on delete cascade。 例74: (修改數據庫表) 刪除成績表Score的參照完整性約束關系。alter table score drop constraint fk_sno。8. 存儲過程與觸發(fā)器:例81:(存儲過程) 創(chuàng)建顯示學生信息的存儲過程Student_List。 create or replace procedure Student_Listasstu Student%rowtype。cursor CUR_VAR is select * from Student。begin open CUR_VAR。loopfetch CUR_VAR INTO stu。exit when CUR_VAR%notfound。(||||||||||)。end loop。end。END。例82:(存儲過程) 創(chuàng)建一個顯示學生平均成績的存儲過程Student_Avg。create or replace procedure Student_AvgasAvgGrade number(5,2)。cursor CUR_AVG is select avg(score) from score group by sno。begin open CUR_AVG。loopfetch CUR_AVG INTO AvgGrade。exit when CUR_AVG%notfound。(AvgGrade)。end loop。close CUR_AVG。END。例83:(存儲過程) 創(chuàng)建一個對學生姓名進行模糊查找的帶參存儲過程Student_Name。create or replace procedure Student_Listasstu Student%rowtype。cursor CUR_VAR is select * from Student。begin open CUR_VAR。loopfetch CUR_VAR INTO stu。exit when CUR_VAR%notfound。(||||||||||)。end loop。end。例84: (觸發(fā)器) 創(chuàng)建滿足下列要求的觸發(fā)器,對Student、Score和Course表進行模仿參照完整性關系的維護。(1) 在Student上創(chuàng)建一個刪除TR_Student_Del,對Student表進行刪除操作,則刪除Score表中對應學號的所有記錄;create or replace trigger TR_Student_Del after delete on student referencing new as new old as old for each rowbeginif deleting thendelete from score where sno=:。end if。end。(2) 在Score上創(chuàng)建一個插入觸發(fā)器TR_Score_insert,當在Score表中插入一條記錄時,要檢查其學號在Student表和課程號在Course表中是否存在,若不存在則不允許插入; create or replace trigger TR_Score_insert before insert ON scorereferencing old as new old as old for each rowdeclarepsno %type。po %type。beginselect sno into psno from student where sno=:。select o into po from course where o=:。if(psno is null and po is null)thenrollback。end if。end。(3) 在Course上創(chuàng)建一個更新觸發(fā)器TR_Course_Upd,對Course表的課程號進行更新操作,則更新Score表中對應的課程號。 create or replace trigger TR_Course_Upd after update on COURSEreferencing new as new old as old for each rowbeginif updating thenupdate SCORE set o=: where o=:。end if。end。 16/1