当前位置:网站首页>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 .

 

原网站

版权声明
本文为[Longbow dog learning C]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202221438094996.html