当前位置:网站首页>Detailed explanation of constraints of Oracle table
Detailed explanation of constraints of Oracle table
2022-04-23 13:40:00 【Wangcai 2】
Constraint Constraint name
If you do not specify a name for the constraint , that oracle The constraint is automatically named
Table level constraints and column level constraints
Scope of action :
① 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
NOT NULL Non empty constraint
Can only be defined at the column level :
CREATE TABLE test(
employee_id NUMBER(6),
last_name VARCHAR2(25) NOT NULL,
hire_date DATE CONSTRAINT test_last_name NOT NULL
)
UNIQUE Data uniqueness constraint
Be careful : Unique constraint , Multiple null values are allowed :NULL.
It can be defined at table level or column level :
( Pay attention to the separation of commas )
CREATE TABLE test(
employee_id NUMBER(6),
last_name VARCHAR2(25) UNIQUE,
email VARCHAR2(25),
hire_date DATE NOT NULL,
CONSTRAINT test_email_uk UNIQUE(email)
);
PRIMARY KEY Primary key constraint
It can be defined at table level or column level :
(1) Data cannot be empty
(2) Data should be unique
CREATE TABLE test(
employee_id NUMBER(6) PRIMARY KEY,
last_name VARCHAR2(25) UNIQUE,
email VARCHAR2(25),
salary NUMBER(8,2),
commission_pct NUMBER(2,2),
hire_date DATE NOT NULL,
CONSTRAINT test_email_uk UNIQUE(email)
);
-- Or, CONSTRAINT test_id_pk PRIMARY KEY(department_id)
FOREIGN KEY Foreign key constraints
It can be defined at table level or column level :
Only the primary key or unique key in the parent table can be referenced as a foreign key by other tables
CREATE TABLE test(
employee_id NUMBER(6) PRIMARY KEY,
last_name VARCHAR2(25) UNIQUE,
email VARCHAR2(25),
salary NUMBER(8,2),
department_id NUMBER(4) ,
hire_date DATE NOT NULL,
CONSTRAINT test_fk FOREIGN KEY (department_id) REFERENCES departments(department_id),
CONSTRAINT test_email_uk UNIQUE(email)
);
FOREIGN KEY Keywords for constraints
FOREIGN KEY: Specify the columns in the child table at the table level
REFERENCES: Columns marked in the parent table
ON DELETE CASCADE( cascading deletion ): When the columns in the parent table are deleted , The corresponding columns in the sub table are also deleted
ON DELETE SET NULL( Cascade empty ): The corresponding column in the sub table is empty
1.ON DELETE CASCADE
If you remove departments Primary Key department_id, So as test Foreign key department_id The data in the column is deleted together
2.ON DELETE SET NULL
If you remove departments Primary Key department_id, So as test Foreign key department_id The location is empty
CREATE TABLE test(
employee_id NUMBER(6) PRIMARY KEY,
last_name VARCHAR2(25) UNIQUE,
email VARCHAR2(25),
salary NUMBER(8,2),
department_id NUMBER(4) ,
hire_date DATE NOT NULL,
CONSTRAINT test_fk FOREIGN KEY (department_id) REFERENCES departments(department_id) ON DELETE CASCADE CONSTRAINT test_email_uk UNIQUE(email)
);
CHECK Check each row constraint
Define the conditions that each line must meet
CREATE TABLE test(
employee_id NUMBER(6) PRIMARY KEY,
last_name VARCHAR2(25) UNIQUE,
email VARCHAR2(25),
salary NUMBER(8,2),
CONSTRAINT test_salary_min CHECK (salary > 0)
);
Syntax for adding constraints
Use ALTER TABLE sentence : Add or remove constraints , But you can't modify the constraint , Valid or invalid constraints ;
Use MODIFY sentence : add to NOT NULL constraint ;
ADD CONSTRAINT Adding constraints
ALTER TABLE employees
ADD CONSTRAINT emp_manager_fk FOREIGN KEY(manager_id) REFERENCES employees(employee_id);
DROP CONSTRAINT Delete constraints
ALTER TABLE employees DROP CONSTRAINT emp_manager_fk;
DISABLE CONSTRAINT Invalidate constraints
ALTER TABLE employees DISABLE CONSTRAINT emp_emp_id_pk;
ENABLE CONSTRAINT Activate constraint
When defining or activating UNIQUE or PRIMARY KEY Constraints are created automatically UNIQUE or PRIMARY KEY Indexes
ALTER TABLE employees ENABLE CONSTRAINT emp_emp_id_pk;
USER_CONSTRAINTS Query constraints
SELECT constraint_name, constraint_type,search_condition
FROM user_constraints
WHERE table_name = 'EMPLOYEES';

USER_CONS_COLUMNS Query the columns that define constraints
SELECT constraint_name, column_name
FROM user_cons_columns
WHERE table_name = 'EMPLOYEES';
Such as ,

————————————————
Link to the original text :https://blog.csdn.net/weixin_42193004/article/details/89043148
版权声明
本文为[Wangcai 2]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230604301397.html
边栏推荐
- Solve tp6 download error course not find package topthink / think with stability stable
- [point cloud series] so net: self organizing network for point cloud analysis
- FatFs FAT32 learning notes
- TCP reset Gongji principle and actual combat reproduction
- 9419页最新一线互联网Android面试题解析大全
- TCP 复位gongji原理和实战复现
- Use of GDB
- 数据仓库—什么是OLAP
- Example interview | sun Guanghao: College Club grows and starts a business with me
- Common types and basic usage of input plug-in of logstash data processing service
猜你喜欢

100000 college students have become ape powder. What are you waiting for?

SAP ui5 application development tutorial 72 - animation effect setting of SAP ui5 page routing
![[point cloud series] learning representations and generative models for 3D point clouds](/img/c5/712bd448fa6c0bffc09ce57f6e56b5.png)
[point cloud series] learning representations and generative models for 3D point clouds

顶级元宇宙游戏Plato Farm,近期动作不断利好频频

kettle庖丁解牛第16篇之输入组件周边讲解

Zero copy technology

Opening: identification of double pointer instrument panel

You and the 42W bonus pool are one short of the "Changsha bank Cup" Tencent yunqi innovation competition!
![[point cloud series] summary of papers related to implicit expression of point cloud](/img/71/2ea1e8a0d505577c9057670bd06046.png)
[point cloud series] summary of papers related to implicit expression of point cloud

Isparta is a tool that generates webp, GIF and apng from PNG and supports the transformation of webp, GIF and apng
随机推荐
Migrating your native/mobile application to Unified Plan/WebRTC 1.0 API
MySQL 8.0.11 download, install and connect tutorials using visualization tools
软考系统集成项目管理工程师全真模拟题(含答案、解析)
5 tricky activity life cycle interview questions. After learning, go and hang the interviewer!
Use of GDB
Data warehouse - what is OLAP
Super 40W bonus pool waiting for you to fight! The second "Changsha bank Cup" Tencent yunqi innovation competition is hot!
SAP UI5 应用开发教程之七十二 - SAP UI5 页面路由的动画效果设置
Solve the problem that Oracle needs to set IP every time in the virtual machine
2020年最新字节跳动Android开发者常见面试题及详细解析
Imx6ull QEMU bare metal tutorial 2: usdhc SD card
GDB的使用
SHA512 / 384 principle and C language implementation (with source code)
[dynamic programming] 221 Largest Square
Launcher hides app icons that do not need to be displayed
Exemple de méthode de réalisation de l'action d'usinage à point fixe basée sur l'interruption de déclenchement du compteur à grande vitesse ob40 pendant le voyage de tia Expo
9419页最新一线互联网Android面试题解析大全
Filter and listener of three web components
torch. Where can transfer gradient
@优秀的你!CSDN高校俱乐部主席招募!