数据库5关系数据库完整性实验

上传人:公**** 文档编号:563697781 上传时间:2024-02-04 格式:DOCX 页数:10 大小:25.57KB
返回 下载 相关 举报
数据库5关系数据库完整性实验_第1页
第1页 / 共10页
数据库5关系数据库完整性实验_第2页
第2页 / 共10页
数据库5关系数据库完整性实验_第3页
第3页 / 共10页
数据库5关系数据库完整性实验_第4页
第4页 / 共10页
数据库5关系数据库完整性实验_第5页
第5页 / 共10页
点击查看更多>>
资源描述

《数据库5关系数据库完整性实验》由会员分享,可在线阅读,更多相关《数据库5关系数据库完整性实验(10页珍藏版)》请在金锄头文库上搜索。

1、数据库系统原理实验名称:关系数据库完整性实验任课教师:学号:姓名:完成日期:2012.11.28实验目的通过 ORACLE 的完整性,理解关系数据库的完整性二、实验内容与步骤ORACLE 环境中通过触发器可以定义为更复杂的完整性约束例 1 :通过触发器产生主键值create sequence ql minvalue 010341416;create table student(sno number(9) primary key ,sname varchar2(20);create or replace trigger add_studentbefore insert on studentfor

2、 each rowbeginselect ql.nextval into :new.sno from dual;end;验证触发器:SQLinsert into student (sname)values( 刘辉);SQLinsert into student (sname)values( 刘俊波);例 2 :创建一个行级触发器,记录下给个用户对数据库的表进行数据操纵的次数create or replace trigger audit_studentafter delete or insert or update on studentfor each rowbeginif deleting t

3、henupdate audit_student set del =del+1where user_name=user and table_name=student;end if;if inserting thenupdate audit_student set ins=ins+1where user_name=user and table_name=student;end if;if updating thenupdate audit_student set upd=upd+1;where user_name =user and table_name=student;end if ;end;要

4、求:1)定义基本表:Audit_student(user_name,table_name,del,ins,upt)2) 输入元组,例 “SCOTT”,“student”,0,0,03) 以 SCOTT 身份登录,对 student 表进行增加删除和修改操作,之后查看 audit_student 表的内容变化。例3、为教师表Teacher定义完整性规则:“职称(pos)为教授时其工资(sal)不得低 于1000元,如果低于1000元,自动改为1000元”。create or replace trigger update_salbefore insert or update of sal,pos

5、on teacherfor each rowwhen(new.pos=S 授)beginif :new.sal create sequence ql minvalue 010341416;语句已处理。SQLWKS create table student(sno number(9) primary key,sname varchar2(20);语句已处理。SQLWKS create or replace trigger add_student2 before insert on student3 for each row4 begin5select ql.nextval into:new.sn

6、o from dual;6 end;7语句已处理。SQLWKS insert into student(sname) values(汪何媛);处理了 1 行。SQLWKS insert into student(sname) values(刘明杭);处理了 1 行。SQLWKS insert into student(sname) values(李昕);处理了 1 行。SQLWKS insert into student(sname) values(刘俊);处理了 1 行。SQLWKS insert into student(sname) values(蒋帆);处理了 1 行。验证触发器:SQ

7、LWKS select * from student2SNOSNAME10341416 汪何媛10341417 刘明杭10341418 李昕10341419 刘俊10341420 蒋帆已选择 5 行。例2、 创建一个行级触发器,记录下给个用户对数据库的表进行数据操纵的次数 定义基本表:SQLWKS create table audi_student(user_name varchar2(20) primary key,table_name varchar2(20),del number(9),ins number(9),upd number(9);语句已处理。定义触发器:SQLWKS crea

8、te or replace trigger audi_student2 after delete or insert or update on student3 for each row4 begin5 if deleting then6 update audi_student set del=del+17 where user_name=user and table_name=student;8 end if;9 if inserting then10 update audi_student set ins=ins+111 where user_name=user and table_nam

9、e=student;12 end if;13 if updating then14 update audi_student set upd=upd+115 where user_name=user and table_name=student;16 end if;17 end;18语句已处理。验证触发器:SQLWKS select * from audi_student2USER_NAME TABLE_NAME DEL INS UPD已选择0行。 输入元组:SQLWKS insert into audi_student values(SCOTT,student,0,0,0); 处理了 1 行。

10、SQLWKS insert into audi_student values(Scott,汪何媛,0,0,0);处理了 1 行。显示: SQLWKS select * from audi_student2USER_NAME TABLE_NAME UPDDELINSSCOTT student 0Scott 汪何媛 0 已选择 2 行。添加操作:SQLWKS insert into audi_student values(WHY,李昕,1,1,2);处理了 1 行。SQLWKS select * from audi_student2USER_NAMETABLE_NAMEUPDDELINSSCOTT

11、student0Scott汪何媛0WHY李昕2已选择 3 行。SQLWKS insert into student(sname) values(凯莉)2处理了 1 行。SQLWKS insert into student(sname) values(戴安娜)2处理了 1 行。SQLWKS insert into student(sname) values(维多利亚)2处理了 1 行。SQLWKS insert into student(sname) values(大 卫)2处理了 1 行。SQLWKS select * from student2SNOSNAME10341416 汪何媛1034

12、1417 刘明杭10341418 李昕10341419 刘俊10341420 蒋帆10341421 凯莉10341422 戴安娜10341423 维多利亚10341424 大卫 已选择 9 行。删除操作:SQLWKS delete from student where sname=大卫; 处理了 1 行。SQLWKS select * from student2SNOSNAME10341416 汪何媛10341417 刘明杭10341418 李昕10341419 刘俊10341420 蒋帆10341421 凯莉10341422 戴安娜10341423 维多利亚已选择 8 行。SQLWKS se

13、lect * from audi_student2USER_NAMEUPDTABLE_NAMEDELSCOTTstudent14Scott汪何媛00WHY李昕12已选择3行。INSSQLWKS update student set sname=巍然where sno=10341420; 处理了 1 行。SQLWKS select * from student2SNOSNAME10341416 汪何媛10341417 刘明杭10341418 李昕10341419 刘俊10341420 巍然10341421 凯莉10341422 戴安娜10341423 维多利亚已选择 8 行。SQLWKS select * from audi_student2USER_NAMEUPDTABLE_NAMEDELINSSCOTTstudent141Scott汪何媛000WHY李昕112 已选择 3 行。 修改操作:SQLWKS update student set sname=布丁 wher

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 学术论文 > 其它学术论文

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号