当前位置:网站首页>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
边栏推荐
猜你喜欢

穿越过后,她说多元宇宙真的存在
![[old horse of industrial control] detailed explanation of Siemens PLC s7-300scl programming](/img/da/08f23cfe7f8759b73576eb0638fec5.png)
[old horse of industrial control] detailed explanation of Siemens PLC s7-300scl programming

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

Should product managers learn from ink knife or Axure?

code::blocks代码格式化快捷键

【深度之眼吴恩达机器学习作业班第四期】Logistic Regression 逻辑回归总结

AC automata

电检码配置

Vulnhub's dc7 target

【量化投资系统】因子处理安装talib
随机推荐
Protobuf binary file learning and parsing
声波通讯 - 流式数据处理 - 窗口对齐
【深度之眼吴恩达机器学习作业班第四期】Linear Regression with One Variable,单变量线性回归
js实现图片懒加载的一个详细方案(引入即可使用)
ROS2中的行为树 BehaviorTree
C mqtt subscription message
基于Sophus的Ceres优化
Automated test - uiautomator2 framework application - automatic clock in
SVM, problems encountered in face recognition and Solutions
[old horse of industrial control] detailed explanation of Siemens PLC s7-300scl programming
Segment tree and use
【深度之眼吴恩达第四期作业班】多元线性回归Linear Regression with multiple variables总结
线段树以及使用
编程能力提升方向
Appium environment setup
多态中的向上和向下转型
关于组织2021-2022全国青少年电子信息 智能创新大赛西北赛区(陕西)复赛的通知
Reflection perfectionism
How to share the virtual environment of pycharm to jupyter Lab
Codeforces Round #799 (Div. 4)