博客
关于我
第五章数据库完整性例题
阅读量: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/

    你可能感兴趣的文章
    Node.js卸载超详细步骤(附图文讲解)
    查看>>
    Node.js基于Express框架搭建一个简单的注册登录Web功能
    查看>>
    Node.js安装与配置指南:轻松启航您的JavaScript服务器之旅
    查看>>
    Node.js安装及环境配置之Windows篇
    查看>>
    Node.js安装和入门 - 2行代码让你能够启动一个Server
    查看>>
    node.js安装方法
    查看>>
    Node.js官网无法正常访问时安装NodeJS的方法
    查看>>
    Node.js的循环与异步问题
    查看>>
    Node.js高级编程:用Javascript构建可伸缩应用(1)1.1 介绍和安装-安装Node
    查看>>
    nodejs + socket.io 同时使用http 和 https
    查看>>
    NodeJS @kubernetes/client-node连接到kubernetes集群的方法
    查看>>
    Nodejs express 获取url参数,post参数的三种方式
    查看>>
    nodejs http小爬虫
    查看>>
    nodejs libararies
    查看>>
    nodejs npm常用命令
    查看>>
    nodejs npm常用命令
    查看>>
    Nodejs process.nextTick() 使用详解
    查看>>
    nodejs 创建HTTP服务器详解
    查看>>
    nodejs 发起 GET 请求示例和 POST 请求示例
    查看>>
    NodeJS 导入导出模块的方法( 代码演示 )
    查看>>