MySQL - table constraints

Constrain the most critical data types, but in order to better ensure the legality of the data, we need to understand some additional constraints.

1. Empty property

Let's first look at its practical operation, and then explain the syntax.

For example, we need to create a table containing the class name and the class teacher name,

 So what changes after we add not null? Let's create a mytb2 to compare. We can see that the attribute is not empty after adding it, and the field is empty if it is not added.

 2. Default value

A certain data in the table may often use a value, we can set it as the default value and use it directly when needed.

For example, we create a person1 table, which contains name, age and gender.

 

 We can see that when we set the default value as above, and we don't need to assign a value to the corresponding field when inserting, it will automatically use the default value.

3. Column Description

comment, used to describe the data in the table, equivalent to a comment.

We see that desc cannot display the comment, we need to enter show create table person2\g to see the table creation statement

 4.zerofill

How should we understand the number after the number type? For example, int(5) is related to the attribute zerofill.

Let's first look at the table without this constraint

 Then we create the table and add zerofill

There are a few more 0s visible to the naked eye, right, just 5 bits. This is the function of zerofill. When the width is less than the set width, zeros are automatically filled.

5. Primary key

primary key There can only be at most one primary key in a table, which cannot be duplicated or empty.

Create a table to store student IDs and names.

The above proves that the primary key field cannot be repeated.

 When the table has no primary key, we can append the primary key

delete primary key

Earlier we learned that primary keys cannot be duplicated, but with composite primary keys it is possible to have multiple primary keys.

6. Self-growth

auto_increment: When the corresponding field does not give a value, the system will +1 from the current maximum value. A table can have at most one auto-increment.

Used in conjunction with the primary key as a logical primary key.

7. Unique key

There are often many fields in a table that require uniqueness. Using the primary key to ensure uniqueness can only guarantee one field. At this time, the unique key can solve the problem that multiple fields need uniqueness.

We create a table to store numbers and names, and set the number as a unique key.

8. Foreign keys

Define the relationship between the master table and the slave table, and the foreign key constraint is mainly defined on the slave table. The primary table must have a primary key constraint or a unique key constraint.

 We first create the master table and the slave table

 Next, we insert data, we first insert the main table data, and then insert the slave table data.

 We tried to insert data into the slave table before the master table, and an error was reported. This is the role of foreign keys.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324304038&siteId=291194637