当前位置:网站首页>数据库-完整性约束
数据库-完整性约束
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);边栏推荐
- View touch analysis
- 聊聊几位大厂清华同学的近况
- Excel-VBA 快速上手(二、条件判断和循环)
- R language dplyr package summary_ The at function calculates the mean and median of multiple data columns (specified by vectors) in the dataframe data, and specifies na RM parameter configuration dele
- 文献1
- vue中缓存页面 keepAlive使用
- Solution to the upper limit of TeamViewer display devices
- RestCloud ETL抽取动态库表数据实践
- What is the ranking of Guosen Securities? Is it safe to open a stock account?
- Redis cluster messages
猜你喜欢

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

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

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

Restcloud ETL extraction de données de table de base de données dynamique

The heavyweight white paper was released. Huawei continues to lead the new model of smart park construction in the future

ETL过程中数据精度不准确问题

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

Mark一下 Unity3d在Inspector中选中不了资源即Project锁定问题

710. 黑名单中的随机数

The engine "node" is inconsistent with this module
随机推荐
SAP gui 770 下载
MongoDB系列之Window环境部署配置
Transformers datacollatorwithpadding class
C语言刷题随记 —— 乒乓球比赛
What is the ranking of Guosen Securities? Is it safe to open a stock account?
R语言glm函数逻辑回归模型、使用epiDisplay包logistic.display函数获取模型汇总统计信息(自变量初始和调整后的优势比及置信区间,回归系数的Wald检验的p值)、结果保存到csv
Go变量的声明与赋值
Pytorch深度学习代码技巧
kubernetes的Controller之deployment
R language dplyr package summary_ The at function calculates the mean and median of multiple data columns (specified by vectors) in the dataframe data, and specifies na RM parameter configuration dele
R语言epiDisplay包的dotplot函数通过点图的形式可视化不同区间数据点的频率、使用by参数指定分组参数可视化不同分组的点图分布、使用cex.X.axis参数指定X轴轴刻度数值标签字体的大小
feil_uVission4左侧工目录消失
RestCloud ETL解决shell脚本参数化
Excel-vba quick start (II. Condition judgment and circulation)
Deploy the flask environment using the pagoda panel
About selenium common. exceptions. Webdriverexception: message: an unknown server side error solution (resolved)
Detailed explanation of C language programming problem: can any three sides form a triangle, output the area of the triangle and judge its type
Pod of kubernetes
Use abp Zero builds a third-party login module (I): Principles
使用 Abp.Zero 搭建第三方登录模块(一):原理篇