数据库的完整性是指数据的正确性和相容性
[例5.1] 将Student表中的Sno属性定义为码
create table Student
(Sno char(9)primary key,
Sname char(20) not null,
Ssex char(2),
Sage smallint,
Sdept char(20)
);
[例5.2] 将SC表中的Sno,Cno属性组定义为码
create table SC
(Sno char(9) not null,
Cno char(4) not null,
Grade smallint,
primary key (Sno,Cno)
);
[例5.3] 定义SC中的参照完整性
create table SC
(Sno char(9) not null,
Cno char(4) not null,
Grade smallint,
primary key (Sno,Cno),
foreign key(Sno)references Student(Sno),
foreign key(Cno)references Course(Cno)
)
[例5.4] 显示说明参照完整性的违约处理示例
create table SC
(Sno char(9) not null,
Cno char(4) not null,
Grade smallint,
primary key (Sno,Cno),
foreign key(Sno)references Student(Sno)
on delete cascade
on update cascade
foreign key(Cno)references Course(Cno)
on delete no action
on update cascade
)
[例5.5] 在定义SC表时,说明Sno,Cno,Grade属性不允许为空值
create table SC
(Sno char(9) not null,
Cno char(4) not null,
Grade smallint not null,
primary key (Sno,Cno)
);
[例5.6] 建立部门表DEPT,要求部门名称Dname列取值唯一,部门编号Deptno列为主码
create table Dept
(Deptno numeric(2)
Dname char(9) unique not null,
location char(10),
primary lkey(Deptno)
);
[例5.7]Student表的Ssex只允许取“男”或“女”
create table Student
(char(9)primary key
Sname char(8) not null,
Ssex char(2) check(Ssex in('男','女')),
Sage smallint,
Sdept char(20)
);
[例5.8] SC表的Grade的值应该在0到100之间
create table SC
(Sno char(9) not null,
Cno char(4) not null,
Grade smallint check (Grade>=10 and Grade <=100),
primary key (Sno,Cno),
foreign key(Sno)references Student(Sno),
foreign key(Cno)references Course(Cno)
);
[例5.9]当学生的性别是男时,其名字不能以Ms.打头。
create table Student4
( Sno char(9),
Sname char(8) not null,
Ssex char(2),
Sage smallint,
Sdept char(20),
primary key(Sno),
check (Ssex='女'or Sname not like 'Ms.%')
);
[例5.10]建立学生登记表Student,要求学号在90000~99999之间,姓名不能取空值,年龄小于30,性别只能是“男”或“女”
create table Student
( Sno numeric(6)
constraint C1 check(Sno between 90000 and 99999),
Sname char(20)
constraint C2 not null,
Sage numeric C3 check (Sage<30),
Ssex char(2)
constraint C4 check(Ssex in('男','女')),
constraint StudentKey primary e=key(Sno)
);
[例5.11]建立教师表TEACHER,要求每个教师的应发工资不低于3000元。应发工资是工资列Sal与扣除项Deduct之和。
create table Teacher
( Eno numeric(4) primary key
Ename char(10)
Job char(8)
Sal numeric(7,2),
Deduct numeric(7,2),
Deptno numeric(2),
constraint Teacherfkey foreign key(Deptno)
references Dept(Deptno),
constraint C1 check(Sal+Deduct>=3000)
);
[例5.12]去掉例5.10 Student表中对性别的限制。
alter table Student
drop constraint C4;
使用ALTER TABLE语句修改表中的完整性限制
[例5.13] 修改表Student中的约束条件,要求学号改为在900000~999999之间,年龄由小于30改为小于40
alter table Student
drop constraint C1;
alter table Student
add constraint C1 check (Sno between 900000 and 99999);
alter table Student
drop constraint C3;
alter table Student
add constraint C3 check (Sage<40);
[例5.18] 限制数据库课程最多60名学生选修
标准Sql
create assertion ASSE_SC_DB_NUM
CHECK (60 >=
(
select count(*)
from Course,SC
where SC.Cno=Course.Cno and Course.Cname ='数据库')
);
[例5.19]限制每一门课程最多60名学生选修
CREATE ASSERTION ASSE_SC_CNUM1
CHECK(
60 >= ALL
(SELECT count(*)
FROM SC
GROUP BY cno
)
);
[5.20]限制每个学期每一门课程最多60名学生选修
alter table SC add term date;
create (60>=all(
select count(*)
from SC group by Cno,term)
);
[例5.21] 当对表SC的Grade属性进行修改时,若分数增加了10%则将此次操作记录到下面表中:SC_U(Sno,Cno,Oldgrade,Newgrade)
其中Oldgrade是修改前的分数,Newgrade是修改后的分数。
create trigger SC_T
after update of Grade on SC
referencing
old row as OldTuple,
new row as NewTuple
for each row
when(NewTuple.Grade >= 1.1 * OldTuple.Grade)
insert into SC_U(Sno,Cno,OldGrade,NewGrade)
values(OldTuple.Sno,OldTuple.Cno,OldTuple.Grade,NewTuple.Grade);
触发器:
定义触发器:
create trigger <触发器名>
{before | after} <触发事件> on <表名>
referencing new|old row as<变量>
for each {row | statement}
[when<触发条件>]<触发动作体>
删除触发器:
drop trigger <触发器名> on <表名>;
[例5.22] 将每次对表Student的插入操作所增加的学生个数记录到表StudentInsertLog中。
create trigger Student_Count
after update on Student
referencing
new table as DELTA
for each statement
insert into StudentInsertLog (Numbers)
select count(*) from DELTA
[例5.23] 定义一个BEFORE行级触发器,为教师表Teacher定义完整性规则“教授的工资不得低于4000元,如果低于4000元,自动改为4000元”。
create trigger Insert_Or_Update_Sal
before insert or update on Teacher --触发事件是插入或更新操作
for each row --行级触发器
begin --定义触发动作体,是PL/SQL过程块
if(new.Job = '教授') and (new.Sal < 4000)
then new.Sal := 4000;
end if;
end;
[例8.9]利用存储过程实现下面的应用:从账户1转指定数额的款项到账户2中。假设账户关系表为Account(Accountnum,Total)
create or replace procedure TRANSFER(inAccount int,outAccount int,amount float) /*定义存储过程TRANSFER,参数为转入账户、转出账户、转账额度*/
as declare /*定义变量*/
totalDepositOut float;
totalDepositIn float;
inAccountnum int;
begin /*检查转出账户的余额 */
select Total into totalDepositOut from Accout
where accountnum=outAccount;
if totalDepositOut is null then /*如果转出账户不存在或账户中没有存款*/
rollback; /*回滚事务*/
return
end if;
if totalDepositOut < amount then /*如果账户存款不足*/
rollback; /*回滚事务*/
return
end if
select Accountnum into inAccountnum from Account
where accountnum = inAccount;
if inAccount is null then /*如果转入账户不存在*/
rollback; /*回滚事务*/
return;
end if;
update Account set total = total - amount
where accountnum = outAccount; /* 修改转出账户余额,减去转出额 */
update Account set total = total + amount
where accountnum = inAccount; /* 修改转入账户余额,增加转入额 */
commit; /* 提交转账事务 */
end;
[例8.10] 从账户01003815868转10000元到01003813828账户中。
call procedure transfer(01003813828,01003815868,10000);