当前位置:网站首页>Database - integrity constraints
Database - integrity constraints
2022-06-26 15:10:00 【EbowTang】
In database design , The data in the table has a certain range of values and connections , The data between multiple tables sometimes have certain reference relationships . When creating and modifying tables , Data integrity and consistency can be ensured by defining constraints . Constraints are rules , Inserting data 、 These rules should be verified when deleting and modifying , So as to play a restrictive role .
The naming rules recommend : Constraint type _ Constraint fields :
- Non empty constraint :NN Table name Name
- Unique constraint :UK Table name Name
- Primary key constraint :PK_ Table name
- Foreign key constraints :FK Table name Name
- Check constraint :CK Table name Name
About table level constraints and column level constraints :
- Column level constraints can only act on one column
- Table level constraints can act on multiple columns ( Of course, table level constraints can also act on a column )
- Define the way : The column constraint must follow the definition of the column , Table constraints do not work with columns , It is defined separately .
- Non empty (not null) Constraints can only be defined on columns
stay KingbaseES Tested on the database :
1, The creation of a table , And its constraint definitions :
1) Define the constraint method in the direct table
2) Define the table before defining the constraint
2, Test the primary key , Non empty , Check , Only four constraints
drop table if exists student; --oracle Without this grammar ,mysql and KES There is
----------------------------------------------
----- piecewise 1: Create and test constraint tables
----------------------------------------------
---1, Directly define constraints on tables and columns
CREATE TABLE student (
stu_nmb number(8) primary key, -- Primary key
stu_name char(8) not null, -- Non empty constraint
gender varchar2(2) check (gender in (' male ',' Woman ')), -- Check constraint
age number(2) CHECK (age BETWEEN 18 AND 30), -- Check constraint
class varchar2 (40) not null,
email varchar2 (30) UNIQUE,
sdate DATE
);
---2, Another form of defining constraints : Define columns first, then define constraints
-- doubt , Non empty constraints do not support the following ?
--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), -- Check constraint
class varchar2 (40) not null,
email varchar2 (30),
sdate DATE,
constraint con_nmb primary key (stu_nmb),
constraint con_check check ( gender in (' male ',' Woman ')),
constraint ch_age CHECK (age BETWEEN 18 AND 30), -- Check constraint
constraint uk_student_email UNIQUE (email)
);
select * from student;
--- Constraint testing
-- First insert , correct
INSERT INTO student VALUES(2314,' Zhang Detian ',' male ',19,' Senior 3 6 class ','[email protected]',SYSDATE);
-- Duplicate primary key , Report errors
INSERT INTO student VALUES(2314,' Wu Haifeng ',' male ',18,' Senior 3 1 class ','[email protected]',SYSDATE);
-- In violation of the gender constraint , Report errors
INSERT INTO student VALUES(2614,' Wudetian ',' change ',29,' Senior 3 8 class ','[email protected]',SYSDATE);
-- Violation of non empty constraints , Report errors
INSERT INTO student VALUES(2614,'',' Woman ',24,' Senior 3 3 class ','[email protected]',SYSDATE);
-- Only constraint check , Report errors
INSERT INTO student VALUES(9874,' Zhanghuale ',' Woman ',19,' Senior 3 4 class ','[email protected]',SYSDATE);
----------------------------------------------
----- piecewise 2: Define constraints after creating tables
----------------------------------------------
--3, Unconstrained tables , Now create it
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
);
-- Start creating constraints
alter table student add constraint pk_nmb primary key (stu_nmb); --oracle Need to be in add Put parentheses after
alter table student add constraint ck_gender check (gender in (' male ',' Woman '));--oracle Need to be in add Put parentheses after
-- Non null constraints and oracle Difference is very big
--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;
--- Constraint testing
-- First insert , correct
INSERT INTO student VALUES(2314,' Zhang Detian ',' male ',19,' Senior 3 6 class ','[email protected]',SYSDATE);
-- Duplicate primary key , Report errors
INSERT INTO student VALUES(2314,' Wu Haifeng ',' male ',18,' Senior 3 1 class ','[email protected]',SYSDATE);
-- Violation of inspection constraints , Report errors
INSERT INTO student VALUES(2614,' Wudetian ',' change ',29,' Senior 3 8 class ','[email protected]',SYSDATE);
-- Violation of non empty constraints , Report errors
INSERT INTO student VALUES(2614,'',' Woman ',24,' Senior 3 3 class ','[email protected]',SYSDATE);
-- Only constraint check , Report errors
INSERT INTO student VALUES(9874,' Zhanghuale ',' Woman ',19,' Senior 3 4 class ','[email protected]',SYSDATE);
-- Remove the four types of constraints for the specified column
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;
-- Disable and enable constraints
ALTER TABLE student DISABLE CONSTRAINT con_nmb
ALTER TABLE student ENABLE CONSTRAINT con_nmb
INSERT INTO student VALUES(2314,' Wangdetian ',' male ',21,' Senior 3 1 class ','[email protected]',SYSDATE);
边栏推荐
- 数据库-视图
- 【TcaplusDB知识库】TcaplusDB系统管理介绍
- Pytoch deep learning code skills
- Mark: unity3d cannot select resources in the inspector, that is, project locking
- Excel-VBA 快速上手(二、条件判断和循环)
- 聊聊几位大厂清华同学的近况
- The DOTPLOT function in the epidisplay package of R language visualizes the frequency of data points in different intervals in the form of point graphs, specifies the grouping parameters with the by p
- 设计人员拿到的工程坐标系等高线CAD图如何加载进图新地球
- Redis cluster
- 【云原生】 ”人人皆可“ 编程的无代码 iVX 编辑器
猜你喜欢
One copy ten, CVPR oral was accused of plagiarizing a lot, and it was exposed on the last day of the conference!
Talk about the RPA direction planning: stick to simple and valuable things for a long time
teamviewer显示设备数量上限解决方法
【 Native cloud】 Éditeur ivx Programmable par tout le monde
Unity 利用Skybox Panoramic着色器制作全景图预览有条缝隙问题解决办法
人力资源导出数据 excel VBA
feil_uVission4左侧工目录消失
RestCloud ETL抽取動態庫錶數據實踐
小程序:uniapp解决 vendor.js 体积过大的问题
English语法_形容词/副词3级 - 原级句型
随机推荐
The R language cartools package divides data, the scale function scales data, and the KNN function of the class package constructs a k-nearest neighbor classifier
cluster addslots建立集群
Idea shortcut key
【雲原生】 ”人人皆可“ 編程的無代碼 iVX 編輯器
Halcon C# 设置窗体字体,自适应显示图片
Kubernetes的pod调度
RestCloud ETL与Kettle对比分析
Cluster addslots establish a cluster
vsomeip3 双机通信文件配置
Unity C # e-learning (10) -- unitywebrequest (2)
Is the QR code for account opening given by the manager of the securities firm safe? Who can I open an account with?
R language uses the aggregate function of epidisplay package to split numerical variables into different subsets based on factor variables, calculate the summary statistics of each subset, and use agg
Common operation and Principle Exploration of stream
Get the intersection union difference set of two dataframes
How to load the contour CAD drawing of the engineering coordinate system obtained by the designer into the new earth
15 BS object Node name Node name String get nested node content
R语言dplyr包bind_rows函数把两个dataframe数据的行纵向(竖直)合并起来、最终行数为原来两个dataframe行数的加和(Combine Data Frames)
Attention meets geometry: geometry guided spatiotemporal attention consistency self supervised monocular depth estimation
English语法_形容词/副词3级 - 原级句型
teamviewer显示设备数量上限解决方法