当前位置:网站首页>SQL related knowledge - constraints
SQL related knowledge - constraints
2022-06-26 04:05:00 【Code Xiaoyou】
The concept of constraints : Limit the data in the table , Make sure the data is correct , Effectiveness and integrity .
classification :
1. Primary key constraint :primary key
2. Non empty constraint :not Null
3. Unique constraint :unique
4. Foreign key constraints :foreign key
Non empty constraint :not null The value of a column cannot be empty
1. Fields with constraints are required , Cannot save to table without filling
1. Add constraints when creating tables
CREATE TABLE stu(
id INT,
NAME VARCHAR(20) NOT NULL
);
2. After creating the table , Adding constraints
alter table stu change name name varchar(20) not null;
3. Delete non empty constraints
ALTER TABLE stu CHANGE NAME NAME VARCHAR(20);
Unique constraint :unique The value of a column cannot be repeated
1. Be careful :
The only constraint can have NULL value , But there can only be one record for NULL
2. When the table is created , Add unique constraints
CREATE TABLE stu(
id INT,
NAME VARCHAR(20) UNIQUE
);
3. Delete unique constraint
alter table stu drop index name;
4. After the table is created , Add unique constraints
alter table stu modify name varchar(20) unique;
Primary key constraint :primary key Non empty and unique
1. A table can only have one field as the primary key .
2. The primary key is the unique identification of the record in the table
1. When the table is created , Add primary key
CREATE TABLE stu(
id INT PRIMARY KEY,
NAME VARCHAR(20)
);
CREATE TABLE stu1(
id INT,
NAME VARCHAR(20),
PRIMARY KEY(NAME)
);
2. Delete primary key
alter table stu drop primary key;
3. After creating the table , Add primary key
alter table stu chane name name varchar(20) primary key;
4. Automatic growth
1. Concept : If a column is of numeric type , Use auto_increment Can be used to achieve automatic value growth
2. When the table is created , Add primary key constraint , And complete the primary key self growth
CREATE TABLE stu(
id INT PRIMARY KEY auto_increment,
NAME VARCHAR(20)
);
3. Delete auto growth
alter table stu change id id int;
4. Add autogrow
alter table stu change id id int auto_increment;
Foreign key constraints :foreign key, Let tables relate to tables , So as to ensure the correctness of the data .
1. When the table is created , Add foreign keys
grammar :create table Table name (
''''''
Foreign key column ,
constraint Foreign key name foreign key ( Foreign key field name )references Main table name ( Main table column name );
);
CREATE TABLE dep(
department_id INT PRIMARY KEY,
department_name VARCHAR(20)
);
CREATE TABLE stu(
id INT PRIMARY KEY,
NAME VARCHAR(20),
department_id INT,
CONSTRAINT stu_dep_fk FOREIGN KEY (department_id) REFERENCES dep(department_id)
);
2. Delete foreign key
alter table Table name drop foreign key Name of the foreign key ;
3. After creating the table , Add foreign keys
alter table Table name add constraint Foreign key name foreign key ( Foreign key field name )references Main table name ( Main table column name );
4. Cascade operation
1. Add cascade operation
grammar :alter table Table name add constraint Foreign key name foreign key ( Foreign key field name )references Main table name ( Main table column name ) on update cascade on delete cascade;
2. classification
1. update cascade :on update cascade
2. cascading deletion :on delete cascade
边栏推荐
- Yolov5 improvements: replace the backbone
- Spark - 一文搞懂 parquet
- Quanergy welcomes Lori sundberg as chief human resources officer
- matplotlib多条折线图,点散图
- 开源!ViTAE模型再刷世界第一:COCO人体姿态估计新模型取得最高精度81.1AP
- Camera-CreateCaptureSession
- 用eclipse连mysql数据库出错然后出现图中的话是咋回事呀
- Three level menu applet
- 【掘金运营套路揭露】真心被掘金的套路....
- MySQL est livré avec l'outil de test de performance MySQL lap pour effectuer des tests de résistance
猜你喜欢
捕获数据包(Wireshark)
EF core Basics
How does virtual box virtual machine software accelerate the network speed in the virtual system?
MapReduce execution principle record
【掘金运营套路揭露】真心被掘金的套路....
如何解决 Iterative 半监督训练 在 ASR 训练中难以落地的问题丨RTC Dev Meetup
【Flink】Flink 批处理模式Map端数据聚合 NormalizedKeySorter
力扣 515. 在每个树行中找最大值
Detr3d multi 2D picture 3D detection framework
Using jsup to extract images from interfaces
随机推荐
Nailing open platform - applet development practice (nailing applet client)
商城风格也可以很多变,DIY 了解一下
Machine learning notes - trend components of time series
Wechat applet is bound to a dynamic array to implement a custom radio box (after clicking the button, disable the button and enable other buttons)
[QT] resource file import
Contains an object field at offset position
Lua语法讲解
線程同步之讀寫鎖
Analysis of camera memory memory leakage (II)
What should I do if the 51 SCM board cannot find the device in keil
Detailed explanation of widget construction process of fluent
Quanergy welcomes Lori sundberg as chief human resources officer
Mybatis的引入问题invalid
【MySQL】 MySQL 导出数据库
2020 summary: industrial software development under Internet thinking
Small record of neural network learning 71 - tensorflow2 deep learning with Google Lab
MapReduce执行原理记录
ipvs之ipvs0网卡
Alibaba cloud function computing service one click to build Z-blog personal blog
XML parsing bean tool class