博客
关于我
第五章数据库完整性例题
阅读量:249 次
发布时间:2019-03-01

本文共 4007 字,大约阅读时间需要 13 分钟。

数据库系统完整性

一、实体完整性

1. 实体完整性定义

实体完整性是数据库系统中最基本的完整性概念,它确保数据库中的数据满足实体的语义规则。例如,在学生表中,学号应作为主键,确保每个学生的学号唯一且不为空。

通过SQL实现实体完整性

-- 在列级定义主码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));

2. 实体完整性检查和违约处理

数据库管理系统在插入或更新操作时,会自动检查实体完整性规则,确保数据符合预定义的约束。

检查规则

  • 主码值必须唯一。
  • 主码的所有属性不能为空。
  • 借助实验数据,填充Student表时,确保学号唯一性。
  • 示例

    -- 插入新学生时,检查学号唯一性insert into Student values('201215121','李强','男',20,'CS');

    二、参照完整性

    1. 参照完整性定义

    参照完整性确保数据库中的外码正确引用主表中的主码,防止数据孤立。

    通过SQL实现参照完整性

    -- 在表级定义参照完整性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));

    2. 参照完整性检查和违约处理

    数据库系统会自动检查外码的正确性,确保外码值存在于主表中。

    示例

    -- 插入SC表时,检查外码是否存在insert into SC values ('201215121','2',95);

    三、用户定义的完整性

    1. 属性上的约束条件

    通过在CREATE TABLE时定义属性上的约束条件,确保数据符合特定业务规则。

    通过SQL实现属性约束

    -- 在定义表时,确保部门名称不允许为空create table DEPT(Deptno numeric(2),                 Dname char(9) unique not null,                 Location char(10),                 primary key(Deptno));

    2. 元组上的约束条件

    通过在CREATE TABLE时使用CHECK约束,确保元组符合特定业务规则。

    通过SQL实现元组约束

    -- 检查性别属性是否为男或女create table Student(Sno char(20) primary key,                    Sname char(10) not null,                    Ssex char(2) check (Ssex in('男','女')),                   Sage int,                    Sdept char(5));

    四、完整性约束命名子句

    1. 完整性约束命名子句

    通过为约束命名,便于在建立表后对约束进行增删改。

    示例

    -- 创建命名约束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));

    2. 修改表中的完整性限制

    通过ALTER TABLE命令动态修改约束。

    示例

    -- 删除并重新添加约束alter table Student drop constraint C4;alter table Student add constraint C4 check(Ssex in('男','女'));

    五、断言

    1. 创建断言

    通过断言定义更具一般性的约束,确保数据库中的数据符合特定规则。

    示例

    -- 限制数据库中课程人数create assertion ASSE_SC_DB_NUM check (60 >= (select count(*) from Course, SC where SC.Cno=Course.Cno and Course.Cname = '数据库'));

    2. 删除断言

    通过DROP ASSERTION命令取消断言。

    示例

    drop assertion ASSE_SC_DB_NUM;

    六、触发器

    1. 定义触发器

    通过触发器实现数据库中的自动化操作,确保数据符合业务规则。

    示例

    -- 在更新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;

    2. 执行触发器

    通过触发条件激活触发器。

    示例

    -- 更新Grade属性时,触发器自动记录变更update SC set Grade=50 where Sno='201215121' and Cno='2';select * from SC_U;

    七、存储过程和函数

    1. 创建存储过程

    通过存储过程实现复杂的数据库操作,提高效率。

    示例

    -- 转账存储过程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;

    2. 执行存储过程

    通过调用存储过程实现业务逻辑。

    示例

    -- 调用存储过程执行转账call Proc_TRANSFER('01003813828', '01003815868', 10000);

    3. 删除存储过程

    通过DROP PROCEDURE命令取消存储过程。

    示例

    drop procedure Proc_TRANSFER();

    转载地址:http://ffvx.baihongyu.com/

    你可能感兴趣的文章
    python unittest高级特性!
    查看>>
    Python unittest:如何将标准输出消息临时重定向到缓冲区并测试其内容?
    查看>>
    Python urllib/Requests下载文件失败,但浏览器下载失败
    查看>>
    Python urllib2 文件上传问题
    查看>>
    Python urllib2.open 连接由对等错误重置
    查看>>
    python urllib2详解及实例
    查看>>
    Python url请求提示certificate verify failed unable to get local issuer certificate
    查看>>
    Python UTC 日期时间对象的 ISO 格式不包括 Z(祖鲁语或零偏移)
    查看>>
    python valueerror object2_python遇到错误记录
    查看>>
    python vars的作用
    查看>>
    Python vcrpy库:HTTP请求记录和重放
    查看>>
    Python virtualenv
    查看>>
    python vue3实现大文件分段续传(断点续传)--带暂停和继续功能
    查看>>
    Python WebDriver如何打印整个页面源(html)
    查看>>
    Python WebSocket自动化测试:构建高效接口测试框架
    查看>>
    Python Web开发
    查看>>
    Redis 配置文件杂项。
    查看>>
    Python web自动化测试 —— 文件上传
    查看>>
    Python web自动化测试 —— 文件上传!
    查看>>
    Python Web自动化测试开发环境搭建(附安装包与虚拟机环境)
    查看>>