杭电数据库张红娟编习题参考答案_第1页
杭电数据库张红娟编习题参考答案_第2页
杭电数据库张红娟编习题参考答案_第3页
杭电数据库张红娟编习题参考答案_第4页
免费预览已结束,剩余1页可下载查看

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、)/*完成表中约束的定义*/-创建course表create table course(cno char(1) primary key,cname varchar(20) not null,credit smallint check(credit=1 and credit1 and number14 and sage0 andgmark 100), primary key (sno,cno)/*针对成绩管理数据库中的表,完成以下操作:*/-(1)用户张勇对Student表和Course表有Select权力。Grant select on student to张勇Grant select on

2、course to张勇-(2)把对表Student的INSERT和Delete权限授予用户张三,并允许他再把此权限授予其 他用户。Grant insert,delete on student to张三with grant option-(3)把查询Course表和修改属性Credit的权限授给用户李四。Grant select,update(credit) on course to李四-(4)授予用户李勇敏对Student表的所有权力(读、插、删、改) ,并具有给其他用户授 权的权力。Grant all privilege on student to李勇敏with grant option-(

3、5)撤销(1)中对张勇所授予的所有权力。Revoke select on student to张勇Revoke select on course to张勇或:Revoke select on student from张勇Revoke select on course from张勇-(6)撤销(2)中对张三所授予的所有权力。revoke insert,delete on student to张三cascade或revoke insert,delete on student from张三cascade/*为成绩管理数据库中的Student表创建一触发器: 当向表中插入或删除记录时, 修改Class

4、表中相应班级的人数。*/-创建insert触发器,适用于student表的单行数据的添加create trigger stu_inserton studentafter insertasupdate classset number=number+1from class,insertedwhere =-创建delete触发器,适用于student表的单行数据的删除create trigger stu_deleteon studentafter deleteasupdate classset number=number-1from class,deletedwhere =-将insert和dele

5、te写入一个触发器内,适用于student表的单行数据的添加或删除create trigger tri_stuon studentafter insert,deleteasif update(sno)update classset number=number+1where clno = (select clno from inserted)else update classset number=number-1where clno = (select clno from deleted)-验证触发器,添加数据insert into studentvalues (2222,tom,男,20,00

6、311)-验证触发器,删除数据delete from studentwhere sno=2222-假设向student表添加或删除的多行数据都来自同一个班级create trigger tri_stu2on studentafter insert,deleteasif update(sno)update classset number=number+(select count(*) from inserted) where clno = (select clno frominserted)else update classset number=number-(select count(*) f

7、rom inserted) where clno = (select clno from deleted)-适用于student表的多行数据的添加或删除(最靠谱解决方案)create trigger tri_stu2on student after insert,delete as begindeclare sno char(7),clno char(5)if update(sno)begindeclare mycursor cursor for select sno,clno from inserted -声明游标open mycursor-打开游标fetch next from mycur

8、sor into sno,clno-获取数据while(fetch_status =0 )/* 0操作成功,-1 FETCH语句失败或此行不在结果集中,-2被提取的行不存在*/beginupdate classset number=number+1 where clno = clnofetch next from mycursor into sno,clno endclose mycursordeallocate mycursorendelsebegindeclare mycursor cursor for select sno,clno from deleted open mycursorf

9、etch next from mycursor into sno,clno while(fetch_status = 0)beginupdate class set number = number -1 where clno=clnofetch next from mycursor into sno,clno endclose mycursor deallocate mycursorend end -为class表再建一更新触发器:当更新班长学号时,检查新输入的学号是否为同一班级的学 生学号,若不是,给出适当的提示信息。create trigger stu_updateon classafte

10、r update asif update(monitor)if ( select monitor from inserted ) not in( select sno from studentwhere clno = (select clno from deleted ) ) beginprint there is not the new monitor in the classrollback transactionend-验证触发器执行update class set monitor = 2001104 where clno = 00312-创建商品表-关闭游标-释放游标create ta

11、ble product( pno char(6) primary key,pname varchar(20) not null,price decimal(7,2) )-创建仓库表create table warehouse( whno char(3) primary key,whname varchar(20) not null, whaddress varchar(20) )-创建库存商品表create table whproduct( whno char(3) references warehouse(whno) on delete no action on update cascade, pno char(6) referencesproduct(pno) on delete cascade on update cascade, number int )-设计触发器,当新增商品时,自动生成该商品在所有仓库的库存记录,库存数量为0create trigger tri_producton productafter insertasbegindeclare pno char(3)select pno=pno from insertedinsert into whproductselect whno,pno,0from warehouseend-设计触发器,当新增仓库

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论