当前位置:网站首页>What are the constraints in MySQL? (instance verification)
What are the constraints in MySQL? (instance verification)
2022-06-29 08:04:00 【Wei Bo-】
1、 Default constraint
We set default values for fields , Is the default constraint . Verified as follows :
(1) Create a table
CREATE TABLE demo.importhead
(
listnumber INT,
-- We are in the field importype Defined as INT After type , Added default value 1.
importtype INT DEFAULT 1,
quantity DECIMAL(10,3),
recordingdate DATETIME
);(2) insert data , Do not add... To the default field
INSERT INTO demo.importhead
(
listnumber,
-- Here we do not insert fields importtype Value
quantity,
recordingdate
)
VALUES
(
3456,
10.23,
'2020-12-10'
);(3) Query data
SELECT * FROM demo.importhead
result : Field importtype The value of is already 1 , We have implemented the definition of the default value of the field as 1 Purpose .
2、 Primary key constraint
Primary key constraint is to define a primary key in a table to uniquely determine the identifier of each row of data in the table . Verified as follows :
(1) Create a table
CREATE TABLE demo.goodsmaster
(
barcode TEXT,
goodsname TEXT,
price DOUBLE,
itemnumber INT PRIMARY KEY AUTO_INCREMENT
);(2) insert data
INSERT INTO demo.goodsmaster( barcode, goodsname, price)VALUES ( '0001', ' book ', 0.47);
INSERT INTO demo.goodsmaster( barcode, goodsname, price)VALUES ( '0002', ' pen ', 0.44);
INSERT INTO demo.goodsmaster( barcode, goodsname, price)VALUES ( '0003', ' pen ', 0.19);(3) Query table structure and data
DESCRIBE demo.goodsmaster;
SELECT * from demo.goodsmaster;

(4) Conclusion : If a field is set as a primary key and automatically added , The value of this field is unique , After new data is inserted, the value of this field is incremented .
3、 Foreign key constraints
To be added
4、 Non empty constraint
A non NULL constraint indicates that the field value cannot be null . Verified as follows :
(1) Create table
CREATE TABLE demo.test3
(
apple INT NOT NULL,
price DECIMAL(10,3)
);(2) insert data
INSERT INTO demo.test3( apple, price) VALUES ( 1, 3.19);(3) View table structure and data
DESCRIBE demo.test3;
SELECT * FROM demo.test3;

(4) Insert data again ,apple Field is empty

(5) Conclusion : Indicates that a field is not empty , So when adding data , This field must have a value , Otherwise, the system will prompt an error .
5、 Uniqueness constraint
The uniqueness constraint means that the value of this field cannot be repeated , Otherwise, the system will prompt an error . Compared with the primary key constraint , The uniqueness constraint is weaker . Verified as follows :
(1) Create table
CREATE TABLE test4
(
id INT(11) PRIMARY KEY,
name VARCHAR(22) UNIQUE,
location VARCHAR(50)
);(2) insert data
INSERT INTO demo.test4( id, name,location) VALUES (3,'weibo','charchar');(3) View table structure and data
desc test4;
SELECT * FROM demo.test4;

(4) summary : In a table , We can specify that multiple fields satisfy the uniqueness constraint , The primary key constraint can only have one . Fields that satisfy the primary key constraint , Automatically satisfy non null constraints , But the fields that satisfy the uniqueness constraint , Can be null .
6、 Self increasing constraint
边栏推荐
- 【域渗透提权】CVE-2020-1472 NetLogon 权限提升漏洞
- 【深度之眼吴恩达机器学习作业班第四期】Logistic Regression 逻辑回归总结
- [eye of depth wuenda machine learning operation class phase IV] logic regression programming implementation
- Cartographer中的线程池操作
- Detailed design of PLC program control system for washing machine
- Improvement direction of programming ability
- AC automata
- Codeforces Round #799 (Div. 4)
- Code:: blocks code formatting shortcuts
- 产品经理应该学习墨刀还是Axure?
猜你喜欢

【kerberos】kerberos 认证浅析
![[industrial control old horse] detailed explanation of design principle of pattern fountain based on PLC](/img/28/690f9985f32675f5d50d196c293abe.jpg)
[industrial control old horse] detailed explanation of design principle of pattern fountain based on PLC

《动手学深度学习》(一)-- 线性神经网络

華為雲的AI深潜之旅

STM32基于HAL库的USART+DMA使用

手把手系列---安装SpotBugs、并快速上手使用

SizeBalanceTree

友元,静态关键字,静态方法以及对象间的关系

Thread pool operations in cartographer

Ceres optimization based on sophus
随机推荐
Detailed design of PLC program control system for washing machine
PostgreSQL安装:The database cluster initialisation failed,stack Hbulider安装
音视频开发案例99讲-目录
qtcreator设置字符集
華為雲的AI深潜之旅
VSLAM特征之线特征&面特征
js异或混淆代码
SVM,人脸识别遇到的问题及解决方法
Reflection perfectionism
产品安全 - 小漏洞导致大问题
Vulnhub's dc6 target
Reasons why the ext.dic file configured in ES does not take effect
Postman pre request
互联网公司的组织结构与产品经理岗位职责是什么?
搭建jenkins环境并自动关联打包好的工程jar进行自动发布
101. symmetric binary tree (recursive and iterative methods)
Soft restart
AI deep dive of Huawei cloud
C mqtt subscription message
Time operation - time format conversion