当前位置:网站首页>What are the MySQL database constraint types
What are the MySQL database constraint types
2022-06-29 04:22:00 【Yisu cloud】
MySQL What are the types of database constraints
This article mainly introduces MySQL What are the relevant knowledge of database constraint types , The content is detailed and easy to understand , The operation is simple and fast , It has certain reference value , I believe that after reading this article MySQL What articles on database constraint types will be fruitful , Let's have a look .
Constraints in the database , As the name suggests, it is to limit the data inserted into the database , The purpose of this is to ensure the validity and integrity of the data . This greatly improves the quality of data in the database , It saves the space of the database and the time of calling data .

1.NOT NULL: Non empty constraint , Specifies that a column cannot be empty , Can only be used as a column level constraint , Only column level constraint syntax can be used to define .
SQL Medium null value ,null Case insensitive , Has the following characteristics : The values of all data types can be null, Include int、float、boolean etc. . Empty string not equal to null,0 Also is not equal to null. Set a non empty constraint for the specified column when creating a table , Just add after the column definition not null that will do , for example :
create table t_test ( id int not null, name varchar(255) default 'xyz' not null, gender varchar(2) null );
Use alter table Add or delete non empty constraints when modifying tables , for example :
# Add non NULL constraint alter table t_test modify gender varchar(2) not null;# Remove non empty constraints alter table t_test modify gender varchar(2) null;# Remove non empty constraints , And specify the default value alter table t_test modify name varchar(255) default 'abc' null;
2.UNIQUE: Unique constraint , The specified column or combination of columns cannot be repeated .
Although columns with unique constraints cannot have duplicate values , But there can be multiple null value , Because in the database null It's not equal to null. Multiple unique constraints can be created in the same table , Unique constraints can also be composed of multiple columns . When creating a unique constraint for a column ,MySQL A unique index will be created for this column , If you don't name a unique constraint , This unique constraint defaults to the same column name . Unique constraints can be created using column level syntax , You can also use table level syntax to create . If you are creating a combined constraint for multiple columns , Or you need to specify a constraint name for the constraint , Only table level syntax can be used .
Use column level syntax to create unique constraints , Just add... After the column definition unique Key words can be used :
# Create unique constraints when creating tables , Use column level syntax to create create table unique_test ( # Create a non NULL constraint , signify id Not for null id int not null, # Establish unique constraints , This means that the column values of multiple rows of data cannot be equal name varchar(255) unique );
Table level syntax creates a unique constraint format :[constraint Constraint name ] unique ( Name [, Name ,…]), The above table level constraint syntax format can be placed in create table Statement juxtaposed with the column definition , It can also be placed in alter table Use in statement add Keyword addition :
# Create table time , Create unique constraints using table level syntax create table uniques_test2 ( # by id Create a non NULL constraint , signify id Not for null id int not null, name varchar(255), pass varchar(255), # The table level syntax used is name Establish unique constraints unique (name), # The table level syntax used is pass Establish unique constraints , And specify the constraint name as test2_uk constraint test2_uk unique (pass) );# Create table time , Use table level syntax to create unique constraints on combined columns create table unique_test3 ( id int not null, name varchar(255), pass varchar(255), # Appoint name and pass Two column combinations cannot be repeated constraint test3_uk unique (name, pass) );# When modifying a table , Use add Keyword to add unique constraints alter table unique_test3 add unique (id, name);# When modifying a table , Use modify keyword , To set unique constraints for a single column alter table unique_test3 modify name varchar(100) unique;
MySQL Delete unique constraint in :
alter table tableName drop index Constraint name ; example sentence :# Delete unique_test3 In the table test3_uk Unique constraint alter table unique_test3 drop index test3_uk;
3.PRIMARY KEY: Primary key constraint , Specify the value of the column to uniquely identify the record .
4.FOREIGN KEY: Foreign key constraints , Specifies that the row record is subordinate to a record in the main table , It is mainly used to ensure referential integrity .
5.CHECK(MySQL I won't support it ): Check constraint , Specify a Boolean expression , The value used to specify the corresponding column must satisfy the expression .
About “MySQL What are the types of database constraints ” That's all for this article , Thank you for reading ! I'm sure you're right “MySQL What are the types of database constraints ” Knowledge has a certain understanding , If you want to learn more , Welcome to the Yisu cloud industry information channel .
边栏推荐
- Runtimeerror in yolox: dataloader worker (PID (s) 17724, 1364, 18928) exited unexpectedly
- 剑指 Offer II 040. 矩阵中最大的矩形
- SQL database stored procedure writing method
- Seattention channel attention mechanism
- Using assetstudio/unitystudio uabe, etc
- 赚钱的5个层次,你在哪一层?
- C language -- branch structure
- 1017 A除以B分
- MySQL can only add small tables when adding tables dynamically. If you increase the size of tables, you won't read data when running tasks. Is there any solution
- [C language] explain the thread exit function pthread_ exit
猜你喜欢

Baidu AI Cloud service grid product CSM release 𞓜 hot public beta

Idea modifying JVM memory

要不是和阿里P7聊过,我也不知道自己是个棒槌

Rapid development project -vscode plug-in

Anaconda自带的Spyder编辑器启动报错问题

树莓派用VNC Viewer方式远程连接

Error accessing database

What is the dry goods microservice architecture? What are the advantages and disadvantages?

Implementation of thread pool based on variable parameter template

If you choose the right school, you can enter Huawei as a junior college. I wish I had known
随机推荐
String differences between different creation methods
c语言 --- 分支结构
Mécanisme d'attention du canal de fixation
Analysis on the types of source code anti leakage technology
Blue Bridge Cup DFS (I)
Anaconda's own Spyder editor starts with an error
1015 theory of virtue and talent
Multi machine LAN office artifact rustdesk use push!!!
[C language] address of stack memory associated with local variable 'num' returned
【C语言】解决 “address of stack memory associated with local variable ‘num‘ returned”
MySQL column to row conversion without Union
Using assetstudio/unitystudio uabe, etc
Hcie security day41: theoretical learning: information collection and network detection
项目开发修养
我的创作纪念日
HCIE-Security Day41:理论学习:信息收集与网络探测
赚钱的5个层次,你在哪一层?
Is the interviewer too difficult to serve? A try catch asks so many tricks
MySQL can only add small tables when adding tables dynamically. If you increase the size of tables, you won't read data when running tasks. Is there any solution
干货丨微服务架构是什么?有哪些优点和不足?