当前位置:网站首页>MySQL - table constraints
MySQL - table constraints
2022-06-21 13:35:00 【Longbow dog learning C】
Constrain the most critical data types , But in order to better ensure the legitimacy of the data , We need to understand some additional constraints .
1. Empty properties
Let's first look at its actual operation , Let's talk about grammar .
For example, we need to create a table containing the name of the class and the name of the head teacher ,
Then we add not null What has changed since ? We create a mytb2 Let's compare . We can see that the added attribute is not empty , Empty without field .
2. The default value is
Some data in the table may often use a value , We can set it as the default , Just use it when you need it .
Let's create person1 surface , The table contains names, ages, genders .
We can see , When we set the default value as above , When we do not need to assign a value to the corresponding field when inserting , It will automatically use the default values .
3. Columns.
comment, Used to describe data in a table , It's equivalent to annotation .
We see desc Can't show comment, We need to type show create table person2\g See table creation statement
4.zerofill
How should we understand the numbers behind the number type ? such as int(5), Just like zerofill This attribute is related to .
Let's first look at the table without this constraint

Then we create the table and add zerofill

A few more are visible to the naked eye 0 Right , just 5 position , This is it. zerofill The role of , When the width is less than the set width , Auto zero .
5. Primary key
primary key A table can only have one primary key at most , Can't repeat , Can't be empty .
Create a table , The student ID and name are stored inside .
The above proves that the primary key field cannot be duplicate .
When a table has no primary key , We can append the primary key
Delete primary key
We learned that primary keys cannot be repeated , But using a composite primary key can have multiple primary keys .
6. Self growth
auto_increment: When the corresponding field is not given a value , The system will start from the current maximum +1. A table can have at most one self growth .
Use with primary key , As a logical primary key .
7. The only key
Many fields in a table need uniqueness , Using a primary key to ensure uniqueness can only guarantee one field , At this time, the unique key can solve the problem that multiple fields need to be unique .
Let's create a table , Storage number and name , And set the number as the unique key .
8. Foreign keys
Define the relationship between master table and slave table , Foreign key constraints are mainly defined on the slave table . The primary table must have a primary key constraint or a unique key constraint .
Let's create the master table and the slave table first
Next we insert the data , We first insert the main table data , Then insert the data from the table .
We try to insert data into the slave table before the master table , Wrong report , This is what foreign keys do .
边栏推荐
- uva11991
- Is it safe to open a securities account by downloading the app of qiniu business school? Is there a risk?
- 3. operator
- 小程序直播互动功能运行在App里?
- Kubernetes快速實戰與核心原理剖析
- Sharing new experiences in home office | community essay solicitation
- Leetcode height checker
- How to use search engine?
- 3. function improvement
- Tami dog sharing: the way of property right transaction and the significance of data-based property right transaction market
猜你喜欢

Matplotlib drawing tips
![[course assignment] floating point operation analysis and precision improvement](/img/de/ae3c1a41b81dffd8e4299bbe926099.png)
[course assignment] floating point operation analysis and precision improvement

Kube-prometheus grafana安装插件和grafana-image-renderer

Distributed transaction processing scheme big PK

Work content of service governance

5000字解析:实战化场景下的容器安全攻防之道

3D slicer import label and view label

Implementation principle and application practice of Flink CDC mongodb connector

如何阅读AI顶会论文?

服务治理的工作内容
随机推荐
CVPR2022 | 上科大x小红书首次提出动作序列验证任务,可应用于体育赛事打分等多场景
PingCAP 入选 2022 Gartner 云数据库“客户之声”,获评“卓越表现者”最高分
Eureka's timedsupersortask class (periodic task with automatic interval adjustment)
Hot information of Tami dog: Xiamen property right trading center creates its first time again!
Is it safe to open a securities account by downloading the app of qiniu business school? Is there a risk?
Talk about the delay of public network
如何阅读AI顶会论文?
How to use search engine?
Kubernetes快速實戰與核心原理剖析
Work content of service governance
MySQL约束(创建表时的各种条件说明)
How to read AI summit papers?
Memcached (high performance memory object cache)
May the mountains and rivers be safe
2. reference
C language elementary order (VI) function
scrapy_redis分布式爬虫
Navigation bar switching, message board, text box losing focus
还在用generator生成xxx管理系统的CRUD代码?来看看我是怎么写的
如何编写测试用例


















