资源描述
第七章 实现数据完整性
一规则
规则Rule就是数据库中对存储在表的列或用户自定义数据类型中的值的规定和限制。规则是单独存储的独立的数据库对象。规则与其作用的表或用户自定义数据类型是相互独立的,即表或用户自定义对象的删除修改不会对与之相连的规则产生影响。规则和约束可以则时使用。
分为两种情况:(1)绑定到表
创建规则语法:create rule 规则名 例:create rule ru_sex
As 表达式 as @sex in(‘男’,’女’)
绑定规则到表的字段语法: sp_bindrule 规则名,[表名.字段名]
例:sp_bindrule ru_sex,[stu.sex]
从表的字段解绑语法:sp_unbindrule [表名.字段名]
删除规则语法:drop rule 规则名 例 :drop rule ru_sex
注意:只有对所有的绑定到某表的字段的规则松绑,才能删除规则。
(2)绑定到自定义数据类型
创建自定义数据类型语法: sp_addtype 自定义数据类型名,系统数据类型
例: sp_addtype varchar,’varchar(20)’
创建规则:create rule ru_address
As @like ‘%长春市%’
绑定规则语法:sp_bindrule 规则名,自定义数据类型名,futureonly
例 sp_bindrule ru_address,varchar
只要在建表的时候用到自定义数据类型,那么表中的字段就都被绑定了。要注意futureonly
它只对以后的起作用。
例:当你用futureonly绑定规则时,以后用此自定义类型的所建表,都将使用此规则。
如你用futrueonly松绑规则时,以后用此自定义类型所建表,将不使用此规则。
注意:如果不用futureonly的绑定或松绑都是对所有表起作用。
二默认值
默认值defaut就是数据库中对存储在表的列或用户自定义数据类型中的值的规定和限制。默认值是单独存储的独立的数据库对象。默认值与其作用的表或用户自定义数据类型是相互独立的,即表或用户自定义对象的删除修改不会对与之相连的默认值产生影响。默认值和约束可以同时使用。
分为两种情况:(1)绑定到表
创建默认值语法:create default 默认值名 例:create default df_address
As 常量表达式 as ‘长春市’
绑定默认值到表的字段语法: sp_bindefault 默认值,[表名.字段名]
例:sp_bindefault df_address,[stu.address]
从表的字段解绑语法:sp_unbindefault [表名.字段名]
删除默认值语法:drop default 默认值 例 :drop default df_address
注意:只有对所有的绑定到某表的字段的默认值松绑,才能删除默认值。
(2)绑定到自定义数据类型
创建自定义数据类型语法: sp_addtype 自定义数据类型名,系统数据类型
例: sp_addtype age,int
创建默认值:create default df_age
As
23
绑定默认值语法:sp_bindefault 默认值,自定义数据类型名,future only
例 sp_bindefault df_age,age
只要在建表的时候用到自定义数据类型,那么表中的字段就都被绑定了。要注意future only
它只对以后的起作用。
例:当你用futureonly绑定默认值时,以后用此自定义类型的所建表,都将使用此默认值。如你用futrueonly松绑默认值时,以后用此自定义类型所建表,将不使用此默认值。
注意:如果不用futureonly的绑定或松绑都是对所有表起作用。
三触发器
1. 触发器的概述
触发器是一类特殊的存储过程。当用户对表或视图发出Update、Insert或Delete语句时触发器自动执行。
在SQLServer中自动为触发器维护两个隐含表:
A.Inserted表 :存放用户对表插入或更新的但尚未提交到数据库中的记录
B.Deleted 表 :存放用户对表即将删除的记录
2. 创建触发器
create trigger 触发器名
on 表名
[after|instead of ]
[insert、update、delete]
as
sql 语句
3.例题
1)、创建替代触发器:
create table student(
stuid int,
stuname varchar(10),
class int,
address varchar(20))
go
create table score(
stuid int,
score numeric)
go
insert into student values(1000,'tom',2,'吉林省长春市')
insert into student values(1001,'mary',1,'黑龙江省哈尔滨市')
insert into student values(1002,'jack',3,'辽宁省沈阳市')
insert into student values(1003,'smith',4,'山东省潍坊市')
go
insert into score values(1000,98)
insert into score values(1001,76)
insert into score values(1002,82)
insert into score values(1003,56)
go
select * from student
select * from score
go
create view vstu_score as select s.stuid ,stuname,score from student s,score sc where s.stuid=sc.stuid
go
insert into vstu_score values(1004,'rose',99)
go
create trigger trg_vstu_score
on vstu_score
instead of insert
as
declare @stuid int,@stuname varchar(10),@score numeric
select @stuid=stuid,@stuname=stuname,@score=score from inserted
insert into student(stuid,stuname) values(@stuid,@stuname)
insert into score(stuid,score) values(@stuid,@score)
go
insert into vstu_score values(1004,'rose',99)
go
select * from student
select * from score
2)、创建after触发器:
select * from student
go
select * into newstudent from student where stuid is null
go
select * from newstudent
go
create trigger trg_newstudent
on student
after delete
as
declare @stuid int,@stuname varchar(10),@class int,@address varchar(20)
select @stuid=stuid,@stuname=stuname,@class=class,@address=address from deleted
insert into newstudent values(@stuid,@stuname,@class,@address)
go
delete from student where stuid=1003
go
select * from newstudent
go
4.修改触发器
格式:Alter trigger trigger_name
例:略
5.删除触发器
格式:Drop Trigger trigger_name
例:Drop Trigger tgOutStoctInsert
展开阅读全文