当前位置:网站首页>Classification, function and usage of MySQL constraints
Classification, function and usage of MySQL constraints
2022-07-06 22:24:00 【Dark horse programmer official】
MySQL Strong performance , It is one of the most widely used databases at present , With MySQL For learning the prototype, it is also convenient to master other databases later , Now let's give you Give a comprehensive explanation MySQL8.0 New features , From zero foundation to high-level one-stop learning , Combined with actual cases, we can gain something !
▼ MySQL8.0 introduction - Advanced learning notes :( Summary )
- The first 1 speak :SQL Overview and database system introduction
- The first 2 speak :MySQL brief introduction 、 Detailed installation steps and use
- The first 3 speak :MySQL Common graphics management tools
- The first 4 speak :MySQL Basic operation of database -DDL
- The first 5 speak :MySQL Basic operation of database -DML
Catalog
Two 、MySQL constraint - Primary key constraint
operation - Add single column primary key
operation - Add multi column primary key ( Combined the primary key )
Add a primary key by modifying the table structure
3、 ... and 、MySQL constraint - Self growth constraints (auto_increment)
Specify the initial value of the auto increment field
delete and truncate Change of auto increment column after deletion
Four 、MySQL constraint - Non empty constraint (not null
Add non empty constraints - The way 1
Add non empty constraints - The way
Four 、MySQL constraint - Unique constraint (unique)
Add unique constraints - The way 1
Add unique constraints - The way 2
5、 ... and 、MySQL constraint - Default constraint (default)
Add default constraint - The way 1
Add default constraint - The way 2
6、 ... and 、MySQL constraint - Zero fill constraint (zerofill)
One 、MySQL constraint
Concept
Constrained English :constraint
Constraints are actually constraints on the data in the table
effect
The purpose of adding constraints in table design is to ensure the integrity and effectiveness of records in the table , For example, the values of some columns in the user table ( cell-phone number ) Can't be empty , Some column values ( ID number ) Can't repeat .
classification
- Primary key constraint (primary key) PK
- Self growth constraints (auto_increment)
- Non empty constraint (not null)
- Uniqueness constraint (unique)
- Default constraint (default)
- Zero fill constraint (zerofill)
- Foreign key constraints (foreign key) FK
Two 、MySQL constraint - Primary key constraint
Concept
- MySQL A primary key constraint is a column or a combination of columns , Its value uniquely identifies each row in the table , Convenient in RDBMS Find a line as soon as possible .
- The primary key constraint is equivalent to Unique constraint + Non empty constraint The combination of , Primary key constraint columns are not allowed to be duplicate , Null values are not allowed .
- Only one primary key is allowed per table
- The key of the primary key constraint is :primary key
- When creating constraints on primary keys , By default, the system will establish a unique index on the column and column combination .
operation
- Add single column primary key
- Add multi column joint primary key
- Delete primary key
operation - Add single column primary key
There are two ways to create a single column primary key , One is to specify the primary key while defining the field , One is to specify the primary key after defining the field .
Method 1- grammar :
-- stay create table In the sentence , adopt PRIMARY KEY Keyword to specify the primary key .
-- Specify the primary key while defining the field , The syntax is as follows :
create table Table name (
...
< Field name > < data type > primary key
...
)
Method 1- Realization :
create table emp1(
eid int primay key,
name VARCHAR(20),
deptId int,
salary double
);
The way 2- grammar :
-- Specify the primary key after defining the field , The syntax is as follows :
create table Table name (
...
[constraint < Constraint name >] primary key [ Field name ]
);
The way 2- Realization :
create table emp2(
eid INT,
name VARCHAR(20),
deptId INT,
salary double,
constraint pk1 primary key(id)
);
operation - Add multi column primary key ( Combined the primary key )
The so-called federated primary key , The primary key is composed of multiple fields in a table .
Be careful :
- When the primary key is composed of multiple fields , You cannot declare a primary key constraint directly after a field name .
- A table can only have one primary key , A federated primary key is also a primary key
grammar :
create table Table name (
...
primary key ( Field 1, Field 2,…, Field n)
);
Realization :
create table emp3(
name varchar(20),
deptId int,
salary double,
primary key(name,deptId)
);
Add a primary key by modifying the table structure
Primary key constraints can not only be created at the same time as the table is created , You can also add... When modifying the table .
grammar :
create table Table name (
...
);
alter table < Table name > add primary key( Field list );
Realization :
-- Add single column primary key
create table emp4(
eid int,
name varchar(20),
deptId int,
salary double,
);
alter table emp4 add primary key(eid);
Delete primary key constraint
When a primary key constraint is not required in a table , You need to remove it from the table . Deleting a primary key constraint is much easier than creating a primary key constraint .
Format :
alter table < Data table name > drop primary key;
Realization :
-- Delete single column primary key
alter table emp1 drop primary key;
-- Delete federated primary key
alter table emp5 drop primary key;
3、 ... and 、MySQL constraint - Self growth constraints (auto_increment)
Concept
stay MySQL in , When the primary key is defined as self growing , The value of the primary key no longer requires user input , The database system automatically assigns values according to the definition . Every time you add a record , The primary key will automatically grow in the same step size .
By adding auto_increment Property to realize the primary key self growth
grammar
Field name data type auto_increment
operation
create table t_user1(
id int primary key auto_increment,
name varchar(20)
);
characteristic
- By default ,auto_increment The initial value of 1, Every new record , The field value is automatically added 1.
- Only one field can be used in a table auto_increment constraint , And the field must have a unique index , To avoid repetition of serial numbers ( It is the primary key or part of the primary key ).
- auto_increment The constrained field must have NOT NULL attribute .
- auto_increment The field of constraint can only be integer type (TINYINT、SMALLINT、INT、BIGINT etc. .
- auto_increment The maximum value of a constraint field is constrained by the data type of the field , If it reaches the upper limit ,auto_increment It will fail. .
Specify the initial value of the auto increment field
If the first record sets the initial value of this field , Then the newly added record will increase from this initial value . for example , If the first record inserted in the table is id Value is set to 5, Then insert the record again ,id The value will change from 5 Begin to increase
-- The way 1, Specify... When creating a table
create table t_user2 (
id int primary key auto_increment,
name varchar(20)
)auto_increment=100;
-- The way 2, After creating the table, specify
create table t_user3 (
id int primary key auto_increment,
name varchar(20)
);
alter table t_user2 auto_increment=100;
delete and truncate Change of auto increment column after deletion
- delete The data will grow automatically, starting from the breakpoint
- truncate The data will grow automatically after, starting from the default starting value
Four 、MySQL constraint - Non empty constraint (not null
Concept
MySQL Non empty constraint (not null) The value of the field cannot be empty . For fields with non null constraints , If the user does not specify a value when adding data , The database system will report an error .
grammar
The way 1:< Field name >< data type > not null;
The way 2:alter table Table name modify Field type not null;
Add non empty constraints - The way 1
-- The way 1, Specify... When creating a table
create table t_user6 (
id int ,
name varchar(20) not null,
address varchar(20) not null
);
Add non empty constraints - The way
create table t_user7 (
id int ,
name varchar(20) , -- Specify a non NULL constraint
address varchar(20) -- Specify a non NULL constraint
);
alter table t_user7 modify name varchar(20) not null;
alter table t_user7 modify address varchar(20) not null;
Delete non empty constraints
-- alter table Table name modify Field type
alter table t_user7 modify name varchar(20) ;
alter table t_user7 modify address varchar(20) ;
Four 、MySQL constraint - Unique constraint (unique)
Concept
Unique constraint (Unique Key) It means that the values of fields in all records cannot be repeated . for example , by id After the uniqueness constraint is added to the field , For each record id Value is the only , There can be no repetition .
grammar
The way 1:< Field name > < data type > unique
The way 2: alter table Table name add constraint Constraint name unique( Column );
Add unique constraints - The way 1
-- Specify... When creating a table
create table t_user8 (
id int ,
name varchar(20) ,
phone_number varchar(20) unique -- Specify a unique constraint
);
Add unique constraints - The way 2
create table t_user9 (
id int ,
name varchar(20) ,
phone_number varchar(20) -- Specify a unique constraint
);
alter table t_user9 add constraint unique_ph unique(phone_number);
Delete unique constraint
-- alter table < Table name > drop index < Unique constraint name >;
alter table t_user9 drop index unique_ph;
5、 ... and 、MySQL constraint - Default constraint (default)
Concept
MySQL The default value constraint is used to specify the default value of a column .
grammar
The way 1: < Field name > < data type > default < The default value is >;
The way 2: alter table Table name modify Name type default The default value is ;
Add default constraint - The way 1
create table t_user10 (
id int ,
name varchar(20) ,
address varchar(20) default ‘ Beijing ’ -- Specify the default constraint
);
Add default constraint - The way 2
-- alter table Table name modify Name type default The default value is ;
create table t_user11 (
id int ,
name varchar(20) ,
address varchar(20)
);
alter table t_user11 modify address varchar(20) default ‘ Beijing ’;
Delete default constraint
-- alter table < Table name > modify column < Field name > < type > default null;
alter table t_user11 modify column address varchar(20) default null;
6、 ... and 、MySQL constraint - Zero fill constraint (zerofill)
Concept
1、 When inserting data , When the length of the value of this field is less than the defined length , This value will be preceded by the corresponding 0
2、zerofill The default is int(10)
3、 When using zerofill when , By default, it will automatically add unsigned( Unsigned ) attribute , Use unsigned After attribute , The numerical range is of the original value 2 times , for example , The sign is -128~+127, No sign is 0~256.
operation
create table t_user12 (
id int zerofill , -- Zero fill constraint
name varchar(20)
);
Delete
alter table t_user12 modify id int;
边栏推荐
- 新手程序员该不该背代码?
- labelimg的安装与使用
- Insert sort and Hill sort
- Adjustable DC power supply based on LM317
- HDR image reconstruction from a single exposure using deep CNN reading notes
- PVL EDI project case
- Notes de développement du matériel (10): flux de base du développement du matériel, fabrication d'un module USB à RS232 (9): création de la Bibliothèque d'emballage ch340g / max232 SOP - 16 et Associa
- Aardio - 利用customPlus库+plus构造一个多按钮组件
- 十二、启动流程
- CCNA-思科网络 EIGRP协议
猜你喜欢
C#實現水晶報錶綁定數據並實現打印4-條形碼
Management background --3, modify classification
[线性代数] 1.3 n阶行列式
Search element topic (DFS)
[Digital IC hand tearing code] Verilog burr free clock switching circuit | topic | principle | design | simulation
【10点公开课】:视频质量评价基础与实践
zabbix 代理服务器 与 zabbix-snmp 监控
[MySQL] online DDL details
如何用程序确认当前系统的存储模式?
【数字IC手撕代码】Verilog无毛刺时钟切换电路|题目|原理|设计|仿真
随机推荐
Common sense: what is "preservation" in insurance?
How does the uni admin basic framework close the creation of super administrator entries?
volatile关键字
i. Mx6ull build boa server details and some of the problems encountered
The SQL response is slow. What are your troubleshooting ideas?
What a new company needs to practice and pay attention to
OpenCV VideoCapture. Get() parameter details
How do I write Flask's excellent debug log message to a file in production?
SQL Server生成自增序号
二分图判定
Maximum product of three numbers in question 628 of Li Kou
[sciter]: encapsulate the notification bar component based on sciter
Unity3D学习笔记6——GPU实例化(1)
Assembly and Interface Technology Experiment 6 - ADDA conversion experiment, AD acquisition system in interrupt mode
HDU 4912 paths on the tree (lca+)
[linear algebra] determinant of order 1.3 n
Management background --3, modify classification
Daily question 1: force deduction: 225: realize stack with queue
Management background --4, delete classification
柔性数组到底如何使用呢?