本文共 4007 字,大约阅读时间需要 13 分钟。
实体完整性是数据库系统中最基本的完整性概念,它确保数据库中的数据满足实体的语义规则。例如,在学生表中,学号应作为主键,确保每个学生的学号唯一且不为空。
-- 在列级定义主码create table Student(Sno char(20) primary key, Sname char(10) not null, Ssex char(2), Sage int, Sdept char(5));-- 在表级定义主码create table Student(Sno char(20), Sname char(10) not null, Ssex char(2), Sage int, Sdept char(5), primary key(Sno));
数据库管理系统在插入或更新操作时,会自动检查实体完整性规则,确保数据符合预定义的约束。
-- 插入新学生时,检查学号唯一性insert into Student values('201215121','李强','男',20,'CS'); 参照完整性确保数据库中的外码正确引用主表中的主码,防止数据孤立。
-- 在表级定义参照完整性create table SC(Sno char(20), Cno char(4), Grade int, primary key(Sno, Cno), foreign key(Sno) references Student(Sno), foreign key(Cno) references Course(Cno));
数据库系统会自动检查外码的正确性,确保外码值存在于主表中。
-- 插入SC表时,检查外码是否存在insert into SC values ('201215121','2',95); 通过在CREATE TABLE时定义属性上的约束条件,确保数据符合特定业务规则。
-- 在定义表时,确保部门名称不允许为空create table DEPT(Deptno numeric(2), Dname char(9) unique not null, Location char(10), primary key(Deptno));
通过在CREATE TABLE时使用CHECK约束,确保元组符合特定业务规则。
-- 检查性别属性是否为男或女create table Student(Sno char(20) primary key, Sname char(10) not null, Ssex char(2) check (Ssex in('男','女')), Sage int, Sdept char(5)); 通过为约束命名,便于在建立表后对约束进行增删改。
-- 创建命名约束create table Student( Sno numeric(6) constraint C1 check (Sno between 90000 and 99999), Sname char(20) constraint C2 NOT NULL, Sage numeric(3) constraint C3 check (Sage < 30), Ssex char(2) constraint C4 check(Ssex in('男','女')), constraint StudentKey primary key(Sno)); 通过ALTER TABLE命令动态修改约束。
-- 删除并重新添加约束alter table Student drop constraint C4;alter table Student add constraint C4 check(Ssex in('男','女')); 通过断言定义更具一般性的约束,确保数据库中的数据符合特定规则。
-- 限制数据库中课程人数create assertion ASSE_SC_DB_NUM check (60 >= (select count(*) from Course, SC where SC.Cno=Course.Cno and Course.Cname = '数据库'));
通过DROP ASSERTION命令取消断言。
drop assertion ASSE_SC_DB_NUM;
通过触发器实现数据库中的自动化操作,确保数据符合业务规则。
-- 在更新Grade属性时,记录变更create trigger SC_Ton SCafter update of Grade asbegin declare @old int, @new int, @sno char(15), @cno char(10); if(update(Grade)) begin select @old = Grade from deleted, @new = Grade from inserted, @sno = Sno from inserted, @cno = Cno from inserted; if(@new >= 1.1 * @old) begin insert into SC_U(Sno, Cno, Old, New) values(@sno, @cno, @old, @new); end; end;end;
通过触发条件激活触发器。
-- 更新Grade属性时,触发器自动记录变更update SC set Grade=50 where Sno='201215121' and Cno='2';select * from SC_U;
通过存储过程实现复杂的数据库操作,提高效率。
-- 转账存储过程create or replace procedure Proc_TRANSFER( @inAccount int, @outAccount int, @amount float) asbegin declare @totalDepositOut float, @totalDepositIn float, @inAccountum int; select @totalDepositOut = total from Account where accountnum = @outAccount; if(@totalDepositOut is null) rollback transaction; if(@totalDepositOut < @amount) rollback transaction; select @inAccount = accountnum from Account where accountnum = @inAccount; if(@inAccount is null) rollback transaction; update Account set total = total - @amount where accountnum = @outAccount; update Account set total = total + @amount where accountnum = @inAccount; commit transaction;end;
通过调用存储过程实现业务逻辑。
-- 调用存储过程执行转账call Proc_TRANSFER('01003813828', '01003815868', 10000); 通过DROP PROCEDURE命令取消存储过程。
drop procedure Proc_TRANSFER();
转载地址:http://ffvx.baihongyu.com/