Introduction to Database Systems - Integrity

I. Overview of integrity

It refers to the correctness of the integrity of the database data in the database, consistency, compatibility

  • Correctness: ensure access to database data is in line with semantic constraints of legal data
  • Consistency: to ensure the logical relationships between the data is correct, update of the database, the database from one consistent state to another consistent state
  • Compatibility: the fact that two of the same data should be consistent

1.1 Bound Categories

Categories integrity constraint
miGzkR.png
from consideration of angular constraint state

  • Static constraints: constraints on the correct state of the database
  • Dynamic constraints: database properly transfer from one state to another state transition constraints

1.2 DBMS support for integrity

  • It provides a mechanism for the definition of integrity constraints
  1. Integrity constraints also called integrity rules, semantic constraint data in the database must be met
  2. SQL standard concepts used to describe a series of integrity, including relational model entity integrity, referential integrity and user-defined integrity
  3. Generally implemented by SQL data definition language statements
  • Provide integrity checking method
  1. Mechanism to check whether the database management system to meet the data integrity constraints is called integrity checks
  2. Usually start checking after insert, update, delete statement is executed, you can also check at the time of the transaction commits
  • Breach of Contract
  1. Database management system if found to user's actions violated the integrity constraints, to take certain actions
  2. Rejection (no action) to perform the operation
  3. Cascade (cascade) to perform other operations

1.3 integrity and security of difference

  • Data integrity
  1. Prevent exists in the database does not comply with the semantics of data, that is, to prevent incorrect data exist in the database
  2. Prevention Object: substandard semantics, incorrect data
  • Data security
  1. Database protection against malicious damage and illegal access
  2. Prevention Object: Illegal user and illegal operation

1.4 integrity constraint name

constraint <完整性约束条件名><完整性约束条件>
--<完整性约束条件>包括not null、unique、primary key短语、foreign key短语、check短语等

--建立教师表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)
    );
--使用 alter table语句修改表中的完整性限制

--修改表Student中的约束条件,要求学号改为在900000~999999之间,年龄由小于30改为小于40
--可以先删除原来的约束条件,再增加新的约束条件
alter table Student
drop constraint c1;

alter table Student
add constraint c1 check (Sno between 900000 and 999999),
alter table Student
drop constraint c3;

alter table Student
add constraint c3 check(Sage < 40);

Second, the entity integrity

Entity integrity rules: relation R values of all tuples in the main code must be unique, and any property on the primary code can not be null
deletion does not destroy the physical integrity of the insert and edit a new tuple (s ) main symbols of group entities could undermine the integrity of
SQL support entity integrity, users only need to explain the relationship between the primary key when you create a basic table, the system can automatically ensure the integrity of the entity

Entity integrity of the 2.1 SQL

Single property code composed of two methods described

  • It is defined as a column-level constraint
  • Define constraints for the table level

A plurality of configuration attributes code is only one method description

  • Define constraints for the table level
--将Student表中的Sno属性定义为码,primary key表示主码
--在列级定义主码
create table Student(
    Sno char(9) primary key,
    Sname  char(20) not null,
    Ssex  char(2),
    Sage  int,
    Sdept  char(20)
    );
--在表级定义主码
    create table Student(
    Sno char(9),
    Sname char(20) not null,
    Ssex char(2),
    Sage int,
    Sdept char(20),
    primary key (Sno)
    );

--将SC表中的Sno,Cno属性组定义为码
create table SC(
    Sno char(9) not null,
    Cno char(4) not null,
    Grade int,
    primary key (Sno,Cno)  /*只能在表级定义主码*/
    );

2.2 Inspection and Breach of Contract

When inserting or main column update operation code to

  • Checking whether the unique primary key values, if they are not inserted or modified reject
  • Checking whether the primary key of each attribute is empty, as long as there is a blank reject inserted or modified

Inspection records whether the unique primary key value, for the avoidance of substantially full table scan table, the RDBMS automatically establishing a generally core index on the primary code
B + tree index
miJSt1.png
primary key value of the new record is inserted 25

  1. The main code index, the B + tree from the root node Find
  2. Read three nodes: the root (51), an intermediate node (1230), leaf nodes (15 20 25)
  3. The primary key value already exists, this record can not be inserted

Third, referential integrity

Referential integrity rules: If the attribute is set FK relationship between the outer code of the R, S and the reference relationship of the primary key Ks, the value of any tuple in the FK R or S is equal to a tuple in a main code Ks of value, or a null value
if the pattern obtained from the relational database ER FIG conversion, converted by the contact set each referential integrity constraint relation exists

3.1 update violate referential integrity

Referential integrity violation and breach of contract deal
miYSbQ.png
possible measures to update breach of referential integrity

  • Rejected: This operation is not allowed to perform. This policy is generally set as the default policy
  • Cascade: update, update and result in a violation of referential integrity referential tuples updated accordingly
  • Blanking value: update and update cause referential integrity violation reference blanking outer code value tuples. Only when the outer code allows to use null value
  • Default is set: to update, and the update cause referential integrity violation reference blanking outer code value tuples; wherein the default values ​​shall be a reference to the relationship between the master key tuple

Referential integrity in the 3.2 SQL

which is defined as a foreign key phrases outer code, the primary code table which references the outer code with reference to the phrase specified

foreign key (A1,...,An) references <外表名> (<外表主码>)
[<参照触发动作>]
--指出修改和删除违反参照完整性约束时触发的动作;缺省时,违反参照完整性的修改和删除将被拒绝
--参照触发动作可以是以下两种
on update <参照动作> [on delete <参照动作>]
on delete <参照动作> [on update <参照动作>]
--参照动作可以是拒绝、级联、置空值、置缺省值之一

--关系SC中(Sno,Cno)是主码。Sno,Cno分别参照Student表的主码和Course表的主码定义SC中的参照完整性
create table SC(
    Sno char(9) not null,
    Cno char(4) not null,
    Grade int,
    primary key (Sno, Cno),  /*在表级定义实体完整性*/
    foreign key (Sno) references Student(SNO),  /*在表级定义参照完整性*/
    foreign key (Cno) references Course(Cno)    /*在表级定义参照完整性*/
    );

Fourth, the user-defined integrity

4.1 domain constraints

Each property must evaluate on a range
type is similar to programming language variables in principle, just as different variables can have the same data type, different attributes can have the same domain
declare a domain include: field value type, the default value, the format field value, or constraints on the value set in the range of
values can not be compared on different domains

--创建域
create domain <域名> [as] <数据类型>
[default <缺省值>]
[<域约束>,...,<域约束>]

--修改域
alter domain <域名> <修改动作>
/*
修改动作
    set default <缺省值>:设置缺省值
    drop default:删除缺省值
    add <域约束>:添加域约束,其中<域约束>与 create domain相同
    drop constraint <约束名>:删除<约束名>命名的域约束
*/

--删除域
drop domain <域名> {cascade | restrict}

4.2 affirm

Assertions are a naming constraint, expressed the database state must meet the logical conditions

--创建断言
create assretion <断言名>
check (<条件>) [<约束性质>]
--每个断言都被赋予一个名字,<check 子句>中的约束条件与 where子句的条件表达式类似

--限制数据库课程最多60名学生选修
create assertion ASSE_SC_DB_NUM
check ( 60 >= (
        select count(*)
        /*此断言的谓词涉及聚集操作count的SQL语句*/
        from Course,SC
        where SC.Cno=Course.Cno and Course.Cname ='数据库'
        ));

--删除断言
drop assertion <断言名>

Fifth, the flip-flop

A trigger is a user-defined relation in the form of a special kind of event-driven type of stored procedure

  • Specify what events and what conditions trigger execution
  • What kind of action specified trigger execution
  • Stored in a database system
  • Any user table add, delete, change operations by the server automatically activates the corresponding triggers

5.1 Definitions trigger

create trigger <触发器名>
    {before | after} <触发事件> on <表名>
    referencing new|old row as<变量>
    for each  {row | statement}
    [when <触发条件>]<触发动作体>

/*
触发器又叫做事件-条件-动作规则
当特定的系统事件发生时,对规则的条件进行检查,如果条件成立则执行规则中的动作,否则不执行该动作。规则中的动作体可以很复杂,通常是一段SQL存储过程
*/

Syntax Description

  • Table owner can create a trigger on table
  • Trigger name
  1. Trigger name can contain schema name may not contain the schema name
  2. The same mode, the trigger name must be unique
  3. Triggers and table names must be under the same mode
  • Table Name
  1. The trigger can only be defined in the base table, the view can not be defined in
  2. When the data base table is changed, the trigger will be activated is defined in the table corresponding triggering event
  • trigger event
  1. The triggering event may be INSERT, DELETE, or UPDATE, may be a combination of these events
  2. You can also activate the trigger when UPDATE OF <column triggers, ...>, which further specify which columns to modify
  3. AFTER / BEFORE timing is triggered:
    an AFTER trigger activation represents operations performed after the trigger event
    BEFORE trigger is activated before the operation represents the execution of a triggering event
  • Trigger Type
  1. Row-level triggers
  2. Statement-level trigger
  • Triggering conditions
  1. When the trigger is activated, it is executed only when the true body trigger action trigger condition; otherwise trigger the body does not perform the action
  2. If omitted WHEN trigger conditions, trigger the operation member performed immediately after activation of the trigger
  • Trigger action body
  1. Trigger action body can be an anonymous PL / SQL block process can also be a call to the stored procedure has been created
  2. If a row-level trigger, users can use the old values ​​and new values ​​before the event following the NEW and OLD references to events in the body of the procedure
  3. If a statement-level triggers, you can not use the NEW or OLD referenced in the body trigger action
  4. Other objects event trigger actions if the body fails, it will terminate the execution of the activation of the trigger, the trigger or triggers that may affect the target table does not change any

5.2 activate the trigger

Execution of a trigger is activated by a trigger event , the server automatically executed by the database
on a data table may define multiple triggers , follow the following order of execution

  1. Execution BEFORE trigger on the table
  2. Activation of the trigger SQL statement
  3. AFTER trigger execution on the table

5.3 Delete Trigger

drop trigger <触发器名> on <表名>

Guess you like

Origin www.cnblogs.com/xxwang1018/p/11546732.html