当前位置:网站首页>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;
边栏推荐
- Four data streams of grpc
- HDU 2008 digital statistics
- Seata aggregates at, TCC, Saga and XA transaction modes to create a one-stop distributed transaction solution
- i.mx6ull搭建boa服务器详解及其中遇到的一些问题
- [MySQL] online DDL details
- Set status bar style demo
- ResNet-RS:谷歌领衔调优ResNet,性能全面超越EfficientNet系列 | 2021 arxiv
- [leetcode daily clock in] 1020 Number of enclaves
- pytorch_YOLOX剪枝【附代码】
- Oracle-控制文件及日志文件的管理
猜你喜欢
Senior soft test (Information System Project Manager) high frequency test site: project quality management
Management background --5, sub classification
Assembly and Interface Technology Experiment 6 - ADDA conversion experiment, AD acquisition system in interrupt mode
[MySQL] online DDL details
LeetCode刷题(十一)——顺序刷题51至55
Installation and use of labelimg
BarcodeX(ActiveX打印控件) v5.3.0.80 免费版使用
基于 QEMUv8 搭建 OP-TEE 开发环境
A Mexican airliner bound for the United States was struck by lightning after taking off and then returned safely
CCNA-思科网络 EIGRP协议
随机推荐
Chapter 3: detailed explanation of class loading process (class life cycle)
二叉(搜索)树的最近公共祖先 ●●
Assembly and interface technology experiment 5-8259 interrupt experiment
NPDP认证|产品经理如何跨职能/跨团队沟通?
PVL EDI project case
Solve project cross domain problems
2022-07-04 mysql的高性能数据库引擎stonedb在centos7.9编译及运行
Management background --3, modify classification
空结构体多大?
Daily question 1: force deduction: 225: realize stack with queue
网络基础入门理解
Spatial domain and frequency domain image compression of images
Netxpert xg2 helps you solve the problem of "Cabling installation and maintenance"
Qt | UDP广播通信、简单使用案例
2020 Bioinformatics | GraphDTA: predicting drug target binding affinity with graph neural networks
The SQL response is slow. What are your troubleshooting ideas?
墨西哥一架飞往美国的客机起飞后遭雷击 随后安全返航
Powerful domestic API management tool
Attack and defense world miscall
2022-07-05 使用tpcc对stonedb进行子查询测试