数据检查约束类型和语法

原创:转载请注明出处:
存储在数据库中的所有数据值均正确的状态。如果数据库中存储有不正确的数据值,则该数据库称为已丧失数据完整性。
数据完整性(Data Integrity)是指数据的精确性(Accuracy) 和可靠性(Reliability)。它是应防止数据库
中存在不符合语义规定的数据和防止因错误信息的输入输出造成无效操作或错误信息而提出的。数据完整性分为四类:
实体完整性(Entity Integrity)、域完整性(Domain Integrity)、参照完整性(Referential Integrity)、
用户定义的完整性(User-definedIntegrity)。
数据库采用多种方法来保证数据完整性,包括外键、束约、规则和触发器。系统很好地处理了这四者的关系,并针对
不同的具体情况用不同的方法进行,相互交叉使用,相补缺点。
总结了一下对数据约束的sql语法如下:
14.添加主键
alter table student
add constraint pk_student_sid
primary key(sid);

15.添加检查约束
alter table student
add constraint ck_student_sname
check(length(sname)<6);

16.添加唯一约束
alter table student
add constraint un_student_scard
unique(scard);

17.添加外键
alter table student
add constraint fk_student_cid
foreign key(cid)
references classes(cid);

18.添加检查约束
alter table student
add constraint ck_student_sex
check(sex in('男','女'));

19.添加检查约束
alter table student
add constraint ck_student_age
check(age>0 and age<120);

20:添加检查约束
alter table student
add constraint ck_student_password
check(length(password)>=6 and length(password)<=16);

21.查询约束
select table_name,constraint_name from user_constraints;

22.删除约束
alter table student drop constraint ck_student_password;
 

23.s删除表同时删除该表的所有约束
drop table student;
posted @ 2017-03-28 13:57  香吧香  阅读(955)  评论(0编辑  收藏  举报