当前位置:网站首页>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
边栏推荐
- 在iframe标签中操作外层dom
- 搭建jenkins环境并自动关联打包好的工程jar进行自动发布
- Vulnhub's dc7 target
- [old horse of industrial control] detailed explanation of Siemens PLC s7-300scl programming
- aws iam内联策略示例
- Protobuf binary file learning and parsing
- Vulnhub's dc9 target
- Codeforces Round #799 (Div. 4)
- Detailed design of PLC program control system for washing machine
- 【深度之眼吴恩达第四期作业班】多元线性回归Linear Regression with multiple variables总结
猜你喜欢

Prompt during packaging: property 'sqlsessionfactory' or 'sqlsessiontemplate'‘

AI and the meta universe sparked a spark: human beings lost only shackles and gained all-round liberation

4 years of working experience, and you can't tell the five communication modes between multithreads. Can you believe it?

C actual combat - high configuration version of Snake game design
![[eye of depth wuenda machine learning operation class phase IV] logic regression programming implementation](/img/48/0ed9b9b2adbce6ab3cf4a07674a9f4.png)
[eye of depth wuenda machine learning operation class phase IV] logic regression programming implementation

SizeBalanceTree

线段树以及使用

Thread pool operations in cartographer

Vulnhub's dc9 target

MySQL中有哪些约束?(实例验证)
随机推荐
AI and the meta universe sparked a spark: human beings lost only shackles and gained all-round liberation
SVM, problems encountered in face recognition and Solutions
【修复收藏功能、更新登录接口】知识付费小程序、博客小程序、完整版开源源码、资源变现小程序,带299整站资源数据
Protobuf binary file learning and parsing
软重启(reboot)
手撕二叉搜索树(Binary Search Tree)
4 years of working experience, and you can't tell the five communication modes between multithreads. Can you believe it?
低配MySQL数据库几十秒插入百万数据
【域渗透提权】CVE-2020-1472 NetLogon 权限提升漏洞
Common MySQL errors and solutions summarized painstakingly (I)
Indextree and Application
php 清除多维数组里面的空值
Audio and video development cases 99 lectures - Contents
Un voyage profond d'IA dans Huawei Cloud
aws elastic beanstalk入门之简单使用
SizeBalanceTree
Product security - small vulnerabilities cause big problems
【深度之眼吴恩达第四期作业班】多元线性回归Linear Regression with multiple variables总结
编程能力提升方向
Appium environment setup