当前位置:网站首页>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);边栏推荐
- Halcon C# 设置窗体字体,自适应显示图片
- 设计人员拿到的工程坐标系等高线CAD图如何加载进图新地球
- Minister of investment of Indonesia: Hon Hai is considering establishing electric bus system and urban Internet of things in its new capital
- Principle of TCP reset attack
- View触摸分析
- Unity uses skybox panoramic shader to make panorama preview. There is a gap. Solution
- 整理了一批脚本标准的函数模块(2021版)
- Common operation and Principle Exploration of stream
- SAP 销售数据 实际发货数据导出 销量
- Use abp Zero builds a third-party login module (II): server development
猜你喜欢

Smoothing data using convolution

RestCloud ETL与Kettle对比分析

English语法_形容词/副词3级 - 原级句型

文献1

使用 Abp.Zero 搭建第三方登录模块(一):原理篇

功能:crypto-js加密解密

10 minutes to understand bim+gis fusion, common BIM data formats and characteristics

Halcon C # sets the form font and adaptively displays pictures

Redis cluster

重磅白皮书发布,华为持续引领未来智慧园区建设新模式
随机推荐
Authoritative announcement on the recruitment of teachers in Yan'an University in 2022
Excel-vba quick start (II. Condition judgment and circulation)
Pytorch深度学习代码技巧
The tablestack function of the epidisplay package of R language makes a statistical summary table (descriptive statistics of groups, hypothesis test, etc.), does not set the by parameter to calculate
整理了一批脚本标准的函数模块(2021版)
Redis事务与watch指令
券商经理给的开户二维码安全吗?找谁可以开户啊?
Deployment of kubernetes' controller
Unity C # e-learning (10) -- unitywebrequest (1)
Is the QR code for account opening given by the manager of the securities firm safe? Who can I open an account with?
Execution of commands in the cluster
R language dplyr package bind_ The rows function merges the rows of the two dataframes vertically. The final number of rows is the sum of the rows of the original two dataframes (combine data frames)
集群中命令的执行过程
Unity C# 网络学习(九)——WWWFrom
[async/await] - the final solution of asynchronous programming
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
Is it safe to open a stock account through the account opening link given by the broker manager? I want to open an account
Redis cluster
Declaration and assignment of go variables
Numpy基本使用