第五章  数据库完整性

1.什么是数据库的完整性

数据库的完整性是指数据的正确性和相容性

 

2.DBMS的完整性控制机制应具有哪些功能?

a.定义功能:即提供定义完整性约束条件的机制

b.检查功能:即检查用户发出的操作请求是否违背了完整性约束条件

c.违约反应:如果发现用户的操作请求使数据违背了完整性约束条件,则采取一定的动作来保证数据的完整性。

 

3.实体完整性

(1)定义实体完整性

a.在列级定义主码

Create table student(sno char(9) primary key,sname char(20) not null,ssex
char(2),sage smallint,sdept char(20));

b.在表级定义主码

Create table sc(sno char(9) not null,cno char(4),not null,grade
smallint,primary key(sno,cno))

(2)实体完整性检查

检查主码值是否唯一,如果不唯一则拒绝插入或修改;

检查主码的各个属性是否为空,只要有一个为空就拒绝插入或修改。

 

4.参照完整性

(1)定义参照完整性

在关系sc中一个元祖表示一个学生选修的某门课程的成绩,(sno,cno)是主码。Sno、cno分别参照引用student表的主码和course表的主码。

Create table sc( sno char(9) not null,cno char(4) not null,grade smallint,

primary key(sno,cno),foreign key(sno) references student(sno),foreign key(cno)
references course(cno))

(2)违约处理

拒绝执行;级联(CASCADE)操作;设置为空值

 

5.用户定义的完整性

(1)属性上的约束条件

a.列值非空

在定义sc表时,sno、cno、grade属性不允许空值

Create table sc(sno char(9) not null,cno char(4) not null,grade smallint not
null,primary key(sno,cno));

b.列值唯一

建立部门表DEPT,要求部门名称Dname列取值唯一,部门编号Deptno列为主码

Create table dept(deptno numberic(2),dname char(9) unique not null,location
char(10),primary key(depno))

c.用CHECK短语指定列值应该满足的条件

Student 表的ssex只允许取“男”或“女”

Create table student(sno char(9) primary key,sname char(8) not null,ssex
char(2) check(ssex in(‘男’,’女’)),sage smallint,sdept char(20));

Sc表的grade的值应该在0和100之间

Create table sc(sno char(8),cno char(4),grade smallint check(grade>=0 and
grade <= 100),primary key(sno,cno),foreign key(sno) references
student(sno),foreign key(cno) references course(cn))

(2)元组上约束条件

当学生的性别是男时,其名字不能以Ms.打头

Create table student(sno char(9),sname char(8) not null,ssex char(2),sage
smallint,sdept char(20),primary key(sno),check(ssex=’女’or sname not
like’Ms.%’));


 

友情链接
KaDraw流程图
API参考文档
OK工具箱
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:[email protected]
QQ群:637538335
关注微信