当前位置:网站首页>MySQL constraints
MySQL constraints
2022-07-03 13:15:00 【Levi Bebe】
1.MySQL constraint
Concept :
Constrained English :constraint
Constraints are actually constraints on the data in the table
effect :
The purpose of adding constraints in the table design is to ensure the records in the table Integrity and effectiveness , For example, the value of some columns in the user table cannot be empty , Some column values cannot be repeated .
classification :
- Primary key constraint (primary key)
- Self increasing constraint (auto_increment)
- Non empty constraint (not null)
- Uniqueness constraint (unique)
- Default constraint (default)
- Zero fill constraint (zerofill)
- Foreign key constraints (foreign key)
1.1 Primary key constraint
Concept :
- MySQL The primary key constraint is what one or more columns do , Its value uniquely identifies each row in the table , Convenient in RDBMS Find a line as soon as possible .
- A primary key constraint is equivalent to a unique constraint + A combination of nonnull constraints , Primary key constraint columns are not allowed to be duplicate , Null values are not allowed .
- Only one primary key is allowed per table
- Keywords are :primary key
Add single column primary key :
CREATE TABLE bianbian.emp1(
eid int PRIMARY KEY,
name VARCHAR(20),
deptId INT,
salary DOUBLE
);
CREATE TABLE bianbian.emp2(
eid INT,
name VARCHAR(20),
deptId INT,
salary DOUBLE,
CONSTRAINT pk1 PRIMARY KEY(eid) --CONSTRAINT pk1 It can be omitted
);
-- The function of primary key
-- Primary key constraint , The primary key column is not empty and duplicate
INSERT into emp2(eid,name,deptId,salary) VALUES(1001,' Zhang San ',10,5000);
-- Repeat validation
INSERT into emp2(eid,name,deptId,salary) VALUES(1001,' Li Si ',10,6000);
-- Can't be empty
INSERT into emp2(eid,name,deptId,salary) VALUES(NULL,' Li Si ',10,6000);
Combined the primary key :
The so-called federated primary key , The primary key is composed of multiple fields in a table .
Be careful :
1. When the primary key is composed of multiple fields , You cannot declare a primary key constraint directly after a field name
2. A table can only have one primary key , A federated primary key is also a primary key
Code :
CREATE TABLE bianbian.emp3(
name VARCHAR(20),
deptId INT,
salary DOUBLE,
PRIMARY KEY(name,deptId)
);
Running results :
The primary key function :
-- The marked column of the union primary key cannot be duplicate or empty
INSERT INTO emp3 VALUES(' Zhang San ',10,5000);
INSERT INTO emp3 VALUES(' Zhang San ',20,5000);
INSERT INTO emp3 VALUES(' Wang Wu ',20,5000);
-- name and deptId It is the same as the original content , Will report a mistake
INSERT INTO emp3 VALUES(' Zhang San ',20,5000);
-- An error is also reported if it is empty
INSERT INTO emp3 VALUES(NULL,20,5000);
INSERT INTO emp3 VALUES(' Extrajudicial ',5000);
Code :
CREATE TABLE bianbian.emp4(
eid INT,
name VARCHAR(20),
deptId INT,
salary DOUBLE
);
ALTER TABLE emp4 ADD PRIMARY key(eid);
Delete primary key :
-- There is no single primary key or joint primary key
ALTER table emp4 DROP PRIMARY KEY
1.2 Self growth constraints
Code :
-- Self growth constraints
CREATE TABLE bianbian.t_user1(
id int PRIMARY KEY auto_increment,
name VARCHAR(20)
);
INSERT INTO t_user1 VALUES(NULL,' Zhang San ');
INSERT INTO t_user1(name) VALUES(' Li Si ');
Code :
-- Self growth constraints
-- Mode one : Specify... When creating a table
CREATE TABLE bianbian.t_user2(
id int PRIMARY KEY auto_increment,
name VARCHAR(20)
)auto_increment = 100; -- id from 100 Start
-- Mode two : After creating the table, specify
CREATE TABLE bianbian.t_user3(
id int PRIMARY KEY auto_increment,
name VARCHAR(20)
);
ALTER TABLE t_user3 auto_increment = 200 -- id from 200 Start
DELETE FROM t_user1; -- DELETE After deleting data , Self growth is still the last value plus 1
TRUNCATE t_user1; -- TRUNCATE After deleting data , Start with the default assignment
1.3 Non empty constraint
CREATE TABLE bianbian.t_user7(
id INT,
name VARCHAR(20) ,
address VARCHAR(20)
);
ALTER TABLE t_user7 MODIFY name VARCHAR(20) NOT NULL; -- Specify a non NULL constraint
ALTER TABLE t_user7 MODIFY address VARCHAR(20) NOT NULL;-- Specify a non NULL constraint
INSERT INTO t_user7(id) VALUES(1001); -- Report errors
ALTER TABLE t_user7 MODIFY address VARCHAR(20);-- Delete non empty constraints
1.4 Unique constraint
Code :
-- 1. Add unique constraints - The way 1- Specify... When creating a table
CREATE TABLE bianbian.t_user8(
id INT,
name VARCHAR(20),
phone_number INT UNIQUE -- Specify a unique constraint
);
-- 2. Add unique constraints - The way 2 Specify... When creating a table
CREATE TABLE bianbian.t_user9(
id INT,
name VARCHAR(20),
phone_number INT -- Specify a unique constraint
);
ALTER TABLE t_user9 ADD CONSTRAINT phone_unique UNIQUE(phone_number);
-- 3. Delete unique constraint
-- Format : ALTER TABLE Table name DROP INDEX Unique constraint name
ALTER TABLE t_user9 DROP INDEX phone_unique;
1.5 Default constraint
Code :
CREATE TABLE bianbian.t_user11(
id INT,
name VARCHAR(20),
address VARCHAR(20)
);
ALTER TABLE t_user11 MODIFY address VARCHAR(20) DEFAULT ' Shenzhen '; -- Add default constraint
ALTER TABLE t_user11 MODIFY address VARCHAR(20) DEFAULT NULL; -- Delete default constraint
Reference resources
- https://www.bilibili.com/video/BV1iF411z7Pu?p=39
边栏推荐
- regular expression
- SSH登录服务器发送提醒
- Servlet
- Sword finger offer14 the easiest way to cut rope
- [Exercice 5] [principe de la base de données]
- Elk note 24 -- replace logstash consumption log with gohangout
- 2022-01-27 use liquibase to manage MySQL execution version
- [Database Principle and Application Tutorial (4th Edition | wechat Edition) Chen Zhibo] [Chapter III exercises]
- Huffman coding experiment report
- Detailed explanation of multithreading
猜你喜欢
Flink SQL knows why (19): the transformation between table and datastream (with source code)
February 14, 2022, incluxdb survey - mind map
[Database Principle and Application Tutorial (4th Edition | wechat Edition) Chen Zhibo] [sqlserver2012 comprehensive exercise]
[Database Principle and Application Tutorial (4th Edition | wechat Edition) Chen Zhibo] [Chapter III exercises]
Dojo tutorials:getting started with deferrals source code and example execution summary
Grid connection - Analysis of low voltage ride through and island coexistence
[problem exploration and solution of one or more filters or listeners failing to start]
Solve system has not been booted with SYSTEMd as init system (PID 1) Can‘t operate.
Leetcode234 palindrome linked list
[Database Principle and Application Tutorial (4th Edition | wechat Edition) Chen Zhibo] [Chapter 6 exercises]
随机推荐
Understanding of CPU buffer line
Fabric. JS three methods of changing pictures (including changing pictures in the group and caching)
Flink SQL knows why (7): haven't you even seen the ETL and group AGG scenarios that are most suitable for Flink SQL?
Cadre de logback
[review questions of database principles]
71 articles on Flink practice and principle analysis (necessary for interview)
php:  The document cannot be displayed in Chinese
Logback 日志框架
Flink SQL knows why (13): is it difficult to join streams? (next)
Detailed explanation of multithreading
When the R language output rmarkdown is in other formats (such as PDF), an error is reported, latex failed to compile stocks Tex. solution
[Database Principle and Application Tutorial (4th Edition | wechat Edition) Chen Zhibo] [Chapter 7 exercises]
Differences and connections between final and static
Node. Js: use of express + MySQL
C graphical tutorial (Fourth Edition)_ Chapter 13 entrustment: what is entrustment? P238
2022-01-27 research on the minimum number of redis partitions
Sword finger offer 12 Path in matrix
Image component in ETS development mode of openharmony application development
Flink SQL knows why (17): Zeppelin, a sharp tool for developing Flink SQL
剑指 Offer 17. 打印从1到最大的n位数