当前位置:网站首页>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 .
边栏推荐
- Anaconda自带的Spyder编辑器启动报错问题
- Remediation for Unsafe Cryptographic Encryption
- How to keep database and cache consistent
- How to create a subtype like relationship between two generic classes when the classes are generic related
- 我的创作纪念日
- Libuv library overview and comparison of libevent, libev and libuv (Reprint)
- 1018 hammer scissors cloth
- Redis 缓存穿透、缓存击穿、缓存雪崩
- Mécanisme d'attention du canal de fixation
- NotImplementedError: Could not run torchvision::nms
猜你喜欢

【HackTheBox】dancing(SMB)

Establishment of small and medium-sized enterprise network

Five thousand years of China

Redis cache penetration, cache breakdown, cache avalanche

PostgreSQL 出现cross-database references are not implemented的bug

Ask a simple question about SQL

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

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

Remediation for Unsafe Cryptographic Encryption

ROS URDF model is parsed into KDL tree
随机推荐
How sqlserver queries and removes results with null fields in the whole column
1018 hammer scissors cloth
Anaconda自带的Spyder编辑器启动报错问题
Libuv库概述及libevent、libev、libuv对比(转载)
Using assetstudio/unitystudio uabe, etc
043. (2.12) what will happen if you become enlightened?
CTO and programmer were both sentenced because the crawler was out of control!
Hi, my technology forum is online!
1019 数字黑洞
Hot renewal process
webassembly学习-动态链接
如何创建 robots.txt 文件?
HCIE-Security Day41:理论学习:信息收集与网络探测
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
The second meeting of the Second Council of Euler open source community was held, and Xinhua III, hyperfusion and Godson Zhongke became members of the Council
Mécanisme d'attention du canal de fixation
Nuxt - 每个页面单独设置 SEO 相关标签及网页标题、图标等(页面配置 head)
Why are you a test / development programmer? Can you recall
Inftnews | metauniverse technology will bring a new shopping experience
1016 部分A+B