第六章 数据库保护习题(徐玲)

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

【7.1】假定DMA要求customers可能的discnt值在0.00和10.00之间,而且数值之间的差距只能为0.02,所以可接受的值为0.00,0.02,0.04,......,9.96,9.98,10.00。请用适当的Create Table语句实现这样的约束。注意,因为可能的值很多,所以用CHE子句是不合适的;需要另外定义一个表来实现这一约束。

Use sales

create table discnts

([discnt] float(2) not null,

primary key([discnt]),

check ([discnt] <= 10.00),

)

declare @i float

set @i=0.00

while @i<10.00

begin

insert into discnts

values(@i)

set @i=@i+0.02

continue

end

create table customers

(cid char(4) not null,cname varchar(13) ,

city varchar(20),[discnt] float(2) not null,

primary key (cid),

foreign key ([discnt] )references discnts);

【7.2】根据X/Open中更新视图的约束,下面哪一条SQL语句是合法的?(括一号中说明了视图是在哪个例子中创建的。)

(1)update agentorders set month='jun';

(2)update agentorders set month='jun' where pid='c001';

(3)update agentorders set month='axx' where aid='a03';

(1)合法。

(2)不合法。

(3)合法

【7.3】创建一个触发器,当向表orders中插入一个新订单时被触发,自动地更新表products的quantity列。触发器必须把在orders指定的qty从products相应行的quantity中减去。

use sales

go

create trigger BT on orders for insert

as

declare@new_qty float,@new_pid char(4)

select@new_qty=qty,@new_pid=pid from inserted

update products set quantity=quantity-@new_qty where pid=@new_pid

【7.4】

雇员EMP(雇员号Eno,姓名Ename,年龄Eage,工资Esalary,部门号Edno),其中雇员号为主码。

部门DEPT(部门号Dno,部门预算Dbudget,经理雇员号Emno),其中部门号为主码。

用SQL语言定义这两个关系模式,要求在模式中完成以下定义:

1)定义每个模式的主码;

2)定义参照完整性;

3)定义每个雇员的年龄不得超过60岁;

4)定义每个雇员的工资不得小于1000;

5)将EMP表DEPT表的所有权限授予用户王平;

6)将EMP表的SELECT权和Esalary列的UPDATE权授予用户李丽,并允许

她传播此权限;

create table DEPT

(Dno char (4)not null,

Dbudget varchar (255),

Emno char (4)not null,

constraint PK_Dno primary key (Dno));

go

create table EMP

(Eno char(4)not null,

Ename varchar (255),

age integer not null check(age<=60),

Esalary integer not null check (Esalary>=1000),

constraint pk_Eno primary key (Eno),/*主键约束*/

constraint FK_Dno foreign key (Dno)references DEPT);

go

grant all privileges on DEPT to王平

grant select on EMP to李丽

with grant option;

grant update on EMP(Esalary)to李丽

with grant option;

go

相关文档
最新文档