当前位置:网站首页>数据库-完整性约束
数据库-完整性约束
2022-06-26 14:52:00 【EbowTang】
在数据库设计的时候,表的数据有一定的取值范围和联系,多表之间的数据有时也有一定的参照关系。在创建表和修改表时,可通过定义约束条件来保证数据的完整性和一致性。约束条件是一些规则,在对数据进行插入、删除和修改时要对这些规则进行验证,从而起到约束作用。

命名规则推荐采用:约束类型_约束字段:
- 非空约束:NN表名列名
- 唯一约束:UK表名列名
- 主键约束:PK_表名
- 外键约束:FK表名列名
- 检查约束:CK表名列名
关于表级约束和列级约束:
- 列级约束只能作用在一个列上
- 表级约束可以作用在多个列上(当然表级约束也可以作用在一个列上)
- 定义方式:列约束必须跟在列的定义后面,表约束不与列一起,而是单独定义。
- 非空(not null) 约束只能定义在列上
在KingbaseES数据库上测试了:
1,表的创建,及其约束定义:
1)直接表中定义约束方式
2)先定义表再定义约束
2,测试主键,非空,检查,唯一四种约束
drop table if exists student; --oracle没有这个语法,mysql和KES存在
----------------------------------------------
-----分段1:创建和测试约束表
----------------------------------------------
---1,直接定义表和列的约束
CREATE TABLE student (
stu_nmb number(8) primary key, --主键
stu_name char(8) not null, --非空约束
gender varchar2(2) check (gender in ('男','女')), --检查约束
age number(2) CHECK (age BETWEEN 18 AND 30), --检查约束
class varchar2 (40) not null,
email varchar2 (30) UNIQUE,
sdate DATE
);
---2,定义约束的另一种形式:先定义列再定义约束
--疑问,非空约束不支持写成下面这个?
--constraint con_name not null (stu_name),
CREATE TABLE student (
stu_nmb number(8),
stu_name char(8) not null,
gender varchar2(2),
age number(2), --检查约束
class varchar2 (40) not null,
email varchar2 (30),
sdate DATE,
constraint con_nmb primary key (stu_nmb),
constraint con_check check ( gender in ('男','女')),
constraint ch_age CHECK (age BETWEEN 18 AND 30), --检查约束
constraint uk_student_email UNIQUE (email)
);
select * from student;
---约束测试
--首先插入,正确
INSERT INTO student VALUES(2314,'张德田','男',19,'高三第6班','[email protected]',SYSDATE);
--主键重复,报错
INSERT INTO student VALUES(2314,'吴海峰','男',18,'高三第1班','[email protected]',SYSDATE);
--违反gender约束,报错
INSERT INTO student VALUES(2614,'五德田','变',29,'高三第8班','[email protected]',SYSDATE);
--违反非空约束,报错
INSERT INTO student VALUES(2614,'','女',24,'高三第3班','[email protected]',SYSDATE);
--唯一约束检查,报错
INSERT INTO student VALUES(9874,'张华乐','女',19,'高三第4班','[email protected]',SYSDATE);
----------------------------------------------
-----分段2:创建表后再定义约束
----------------------------------------------
--3,没有约束的表,现将其进行创建
CREATE TABLE student (
stu_nmb number (8),
stu_name char (8),
gender varchar2 (2),
age number (2),
class varchar2 (40),
email varchar2 (30),
sdate DATE
);
--开始创建约束
alter table student add constraint pk_nmb primary key (stu_nmb); --oracle需要在add后打括号
alter table student add constraint ck_gender check (gender in ('男','女'));--oracle需要在add后打括号
--非空约束和oracle区别很大
--oracle:alter table student modify(stu_name constraint con_name not null);
ALTER TABLE student ALTER COLUMN stu_name SET NOT NULL;
ALTER TABLE student ADD CONSTRAINT uq_email UNIQUE (email);
select * from student;
---约束测试
--首先插入,正确
INSERT INTO student VALUES(2314,'张德田','男',19,'高三第6班','[email protected]',SYSDATE);
--主键重复,报错
INSERT INTO student VALUES(2314,'吴海峰','男',18,'高三第1班','[email protected]',SYSDATE);
--违反检查约束,报错
INSERT INTO student VALUES(2614,'五德田','变',29,'高三第8班','[email protected]',SYSDATE);
--违反非空约束,报错
INSERT INTO student VALUES(2614,'','女',24,'高三第3班','[email protected]',SYSDATE);
--唯一约束检查,报错
INSERT INTO student VALUES(9874,'张华乐','女',19,'高三第4班','[email protected]',SYSDATE);
--移除指定列的四种类型的约束
ALTER TABLE student ALTER COLUMN stu_name DROP NOT NULL;
ALTER TABLE student DROP CONSTRAINT uq_email;
ALTER TABLE student DROP CONSTRAINT ck_gender;
ALTER TABLE student DROP CONSTRAINT pk_nmb;
--禁用与启用约束
ALTER TABLE student DISABLE CONSTRAINT con_nmb
ALTER TABLE student ENABLE CONSTRAINT con_nmb
INSERT INTO student VALUES(2314,'望德田','男',21,'高三第1班','[email protected]',SYSDATE);边栏推荐
- 【雲原生】 ”人人皆可“ 編程的無代碼 iVX 編輯器
- Talk about the recent situation of several students from Tsinghua University
- Deployment of kubernetes' controller
- TCP 复位攻击原理
- 子查询的使用
- Redis-集群
- Pod scheduling of kubernetes
- R语言使用glm函数构建泊松对数线性回归模型处理三维列联表数据构建饱和模型、使用step函数基于AIC指标实现逐步回归筛选最佳模型、使用summary函数查看简单模型的汇总统计信息
- 房东拿租金去还房贷是天经地义的嘛
- Go变量的声明与赋值
猜你喜欢

使用RestCloud ETL Shell组件实现定时调度DataX离线任务

710. random numbers in the blacklist

使用卷积对数据进行平滑处理

15 BS object Node name Node name String get nested node content

【soloπ】adb连接单个多个手机

一篇抄十篇,CVPR Oral被指大量抄袭,大会最后一天曝光!

About selenium common. exceptions. Webdriverexception: message: an unknown server side error solution (resolved)

Use abp Zero builds a third-party login module (II): server development

feil_uVission4左侧工目录消失

【雲原生】 ”人人皆可“ 編程的無代碼 iVX 編輯器
随机推荐
Unity 利用Skybox Panoramic着色器制作全景图预览有条缝隙问题解决办法
Execution of commands in the cluster
Attention meets Geometry:几何引导的时空注意一致性自监督单目深度估计
It's natural for the landlord to take the rent to repay the mortgage
Pod scheduling of kubernetes
Datasets dataset class (2)
TS常用数据类型总结
Numpy基本使用
【雲原生】 ”人人皆可“ 編程的無代碼 iVX 編輯器
Where do people get their top energy?
R language uses GLM function to build Poisson logarithm linear regression model, processes three-dimensional contingency table data to build saturation model, uses step function to realize stepwise re
TS common data types summary
Use abp Zero builds a third-party login module (I): Principles
View触摸分析
[cloud native] codeless IVX editor programmable by "everyone"
php文件上传00截断
Is it safe to open an account by digging money? Is there any risk?
IP certificate application process of sectigo
Is it safe for flush to register and open an account? Is there any risk?
券商经理给的开户二维码安全吗?找谁可以开户啊?