当前位置:网站首页>[introduction to database system (Wang Shan)] Chapter 5 - database integrity
[introduction to database system (Wang Shan)] Chapter 5 - database integrity
2022-07-27 17:55:00 【tsunaa】
1、 The integrity of database refers to the correctness and compatibility of data .
2、 Integrity control mechanism :
(1) Provide a mechanism for defining integrity month conditions
(2) Provide methods for integrity checking
(3) Default treatment
3、 Entity integrity definition :
PRIMARY KEY Definition , Table level integrity constraints or column level integrity constraints .
Entity integrity : The primary property cannot be empty .
example :CREATE TABLE Student
(Sno CHAR(5) PRIMARY KEY, /* Define the main code at the column level */
Sname CHAR(20) NOT NULL,
Ssex CHAR(1) ,
Sage INT,
Sdept CHAR(15));
CREATE TABLE Student
(Sno CHAR(5) ,
Sname CHAR(20) NOT NULL,
Ssex CHAR(1) ,
Sage INT,
Sdept CHAR(15),
PRIMARY KEY (Sno) /* Define the main code at the table level */
);
CREATE TABLE SC
(Sno CHAR(9) NOT NULL,
Cno CHAR(4) NOT NULL,
Grade SMALLINT,
PRIMARY KEY (Sno,Cno) /* A set of attributes is the main code , The main code can only be defined at the table level */
);
4、 Entity integrity check and default handling
According to the entity integrity rules , The system checks automatically :
1) Whether the primary code value is unique .
2) Whether the main code attributes are empty .
5、 Refer to integrity definition : Create table time ,FOREIGN KEY Definition .
Referential integrity : The outer code value is null or equal to the value of a Yuanzu of the corresponding main code .
example : CREATE TABLE SC(
Sno CHAR(5) ,
Cno CHAR(3) ,
Grade int,
Primary key (Sno, Cno)
foreign key (Sno) references Student(Sno),
foreign key (Cno) references Course(Cno)
);
6、 Several issues to consider in realizing referential integrity :
1) Whether the outer code can accept null value
2) The problem of deleting tuples in the referenced relationship
3) Problems in inserting tuples into reference relationships
4) Modify the master code in the relationship
1) Whether the outer code can accept null value
When implementing referential integrity , The system should provide a mechanism for defining foreign codes , A mechanism should also be provided to define whether the outer code column allows null values .
example 1:
Student ( Student number , full name , Gender , Major number , Age )
major ( Major number , Professional name )
The major number column in student relations can be null .
example 2:
Student ( Student number , full name , Gender , Major number , Age )
Course ( Course no. , Course name , credits )
Elective ( Student number , Course no. , achievement )
The student number column in the elective relationship cannot be null .
2) The problem of deleting tuples in the referenced relationship
In a general way , When deleting a tuple of the referenced relationship , There are several tuples in the reference relationship , Its outer code value is the same as the main code value of the deleted tuple of the referenced relationship , Can there be at this time Three Different Strategy :
(1) cascading deletion (CASCADES)
(2) Restricted deletion (RESTRICTED)
Only if the outer code value of no tuple in the reference relationship is the same as the main code value of the tuple to be deleted in the referenced relationship , The system only performs the deletion operation , Otherwise, reject the deletion .
(3) Set null to delete (NULLIFIES)
Delete the tuple of the referenced relationship , And set the outer code value of the corresponding tuple in the reference relationship to null .
These three methods , Which one is right , It depends on the semantics of the application environment
3) Problems in inserting tuples into reference relationships
In a general way , When a tuple is inserted into a reference relationship , There is no corresponding tuple in the referenced relationship , Its main code value is the same as the outer code value of the tuple inserted by the reference relationship , Here are some strategies :
(1) Restricted insertion
Only if there are corresponding tuples in the referenced relationship , Its main code value is the same as the outer code value of the tuple inserted by the reference relationship , The system performs the insertion operation , Otherwise, reject this operation .
(2) Recursive insert
First, insert the corresponding tuple into the referenced relationship , Its main code value is equal to the outer code value of the tuple inserted by the reference relationship , Then insert tuples into the reference relationship .
4) Modify the master code in the relationship
(1) It is not allowed to modify the master code
In some RDBMS in , Modifying the relationship master code is not allowed .
(2) It is allowed to modify the master code
In some RDBMS in , It is allowed to modify the relationship master code , However, the uniqueness and non emptiness of the main code must be guaranteed , Otherwise, the modification is rejected .
If the referenced relationship is modified , Is similar to deleting , Yes Three strategies :
Cascade modification : While modifying the primary code value of the referenced relationship , Modify the corresponding foreign code value in the reference relationship .
Restricted modification : Reject this modification . Only when the outer code value of no tuple in the reference relationship is equal to the main code value of a tuple in the reference relationship , Can be modified .
Set null value to modify : While modifying the primary code value of the referenced relationship , Set the corresponding foreign code value in the reference relationship to null .
example : CREATE TABLE SC(
Sno CHAR(5) NOT NULL,
Cno CHAR(3) NOT NULL,
Grade SMALLINT,
Primary key (Sno, Cno)
foreign key (Sno) references Student(Sno),
ON DELETE CASCADE Delete level company
ON UPDATE CASCADE, Company update
foreign key (Cno) references Course(Cno)
ON DELETE CASCADE
ON UPDATE CASCADE);
7、 User defined integrity : Definition of constraints on tuples .
Column value is not empty (NOT NULL) Column value uniqueness (UNIQUE) CHECK The phrase
example : CREATE TABLE Student
(Sno CHAR(5) ,
Sname CHAR(20) NOT NULL,
Ssex CHAR(1) ,
Sage INT,
Sdept CHAR(15),
PRIMARY KEY (Sno)
CHECK (Ssex=‘ Woman ’ OR Sname NOT LIKE ‘Ms.%’)
);
8、 Integrity constraint named clause
CREATE TABLE Student
(Sno NUMERIC(6)
CONSTRAINT C1 CHECK(Sno BETWEEN 90000 AND 99999),
Sname CHAR(20)
CONSTRAINT C2 NOT NULL,
Ssex CHAR(2)
CONSTRAINT C3 CHECK(Ssex IN (’ male ‘,’ Woman ‘)),
CONSTRAINT C4 PRIMARY KEY (Sno)
);
notes :C1、C2、C3、C4 Is the constraint name constraint Define the keyword for the constraint name
Modify the integrity restrictions in the table
ALTER TABLE Student DROP CONSTRAINT C4;
ALTER TABLE Student ADD CONSTRAINT C1 CHECK(Sno BETWEEN 900000 AND 999999);
边栏推荐
- Initial polymorphism
- How difficult the interview is! I was forced to survive the six rounds of interview of ant financial! Almost out (interview resumption)
- 阿里巴巴鹰眼系统简介
- 【数据库系统概论(王珊)】第11章 并发控制
- KMP template - string matching
- JS to realize the right-click menu bar function
- Maximum number less than n
- Lichuang EDA - PCB layout (IV)
- Oracle 11g database installation tutorial
- numpy数组矩阵操作
猜你喜欢

numpy数组矩阵操作

机器学习之评价指标(一)——回归评价指标

微信小程序 实现拨打电话

二舅的外甥和他的学生们

写好技术原创文章的一点建议

Fast parsing combined with Huatu document encryption software

Database hyperphone (I)

High cost, difficult to implement, slow to take effect, what about open source security?

Explain the pile of binary trees in detail

面试官:什么是脚手架?为什么需要脚手架?常用的脚手架有哪些?
随机推荐
面试官:什么是脚手架?为什么需要脚手架?常用的脚手架有哪些?
微信小程序 实现拨打电话
Database hyperphone (4)
Lichuang EDA - PCB layout (IV)
With the arrival of large displacement hard core products, can the tank brand break through the ceiling of its own brand?
What should we pay attention to when choosing the LED display screen of the stadium
Hegong sky team vision training Day7 - vision, Jetson naon and d435i
Bit band operation of semaphore protection
【数据库系统概论(王珊)】第4章——数据库安全性
Summer Challenge [FFH] real time chat room websocket practice
记一次 .NET 某智慧物流 WCS系统 CPU 爆高分析
Run loam_ Velodyne real-time mapping
How far can invisible orthodontics go under the tuyere?
【数据库系统概论(王珊)】第1章——绪论
The 7-year-old boy broke his finger by AI robot just because he played chess too fast?
Database hyperphone (III)
Array of C language
Following the example of IE, is the decline of Firefox inevitable?
可口可乐的首要挑战,不是元气森林
Microsoft silently donated $10000 to curl, which was not notified until half a year later