当前位置:网站首页>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 .
边栏推荐
- Is the interviewer too difficult to serve? A try catch asks so many tricks
- How sqlserver queries and removes results with null fields in the whole column
- JVM_ 16_ Garbage collector
- sink端 一般都是jdbc的insert update delete么?
- Sword finger offer II 040 Largest rectangle in matrix
- [C language] explain the thread exit function pthread_ exit
- 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
- Binary tree serialization and deserialization (leetcode (difficult))
- 【C语言】解决 “address of stack memory associated with local variable ‘num‘ returned”
- NotImplementedError: Could not run torchvision::nms
猜你喜欢

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

ROS TF coordinate transformation Library & rewrite to create high frequency coordinate transformation broadcaster

HCIE-Security Day41:理论学习:信息收集与网络探测

ROS URDF model is parsed into KDL tree

c语言 --- 分支结构

不使用union实现Mysql 列转行

Idea modifying JVM memory

Remediation for Unsafe Cryptographic Encryption

Build a simple website by yourself
![[C language] explain the thread exit function pthread_ exit](/img/fb/96db1c712370dbb216a06440ecdc5e.png)
[C language] explain the thread exit function pthread_ exit
随机推荐
[C language] explain the thread exit function pthread_ exit
How to create robots Txt file?
源代码防泄露技术种类浅析
The people's Bank of China printed and distributed the notice on supporting cross-border RMB settlement of new foreign trade formats
Analysis of moudo Network Library
Redis 缓存穿透、缓存击穿、缓存雪崩
大神们 在富函数的open中从mysql连接池里取连接 连接池初始化是20个 如果富函数的并行度是1
自己动手搭建一个简单的网站
MySQL column to row conversion without Union
ROS URDF model is parsed into KDL tree
CTO and programmer were both sentenced because the crawler was out of control!
Hot renewal process
树莓派用VNC Viewer方式远程连接
JSX的基本使用
Analysis on the types of source code anti leakage technology
Anaconda自带的Spyder编辑器启动报错问题
How to merge upstream and downstream SQL data records
LabVIEW显示Unicode字符
Path and LD_ LIBRARY_ Example of path usage
【HackTheBox】dancing(SMB)