Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the common constraints in MySQL

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Most people do not understand the knowledge points of this article "what are the common constraints in MySQL?", so the editor summarizes the following contents, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "what are the common constraints in MySQL?"

1. Non-null constraint (not null)

Non-null constraints are used to ensure that the value of the current column is not null and that non-null constraints can only appear on the columns of the table object.

Null type characteristics: the values of all types can be null, including int, float and other data types

2. Uniqueness constraint (unique)

The only constraint is that the columns or combinations of columns of the specified table cannot be repeated to ensure the uniqueness of the data.

Unique constraints do not allow duplicate values, but can be multiple null.

The same table can have multiple unique constraints, multiple column combinations of constraints.

When you create a unique constraint, if you do not give the unique constraint name, the default is the same as the column name.

Unique constraints can be created not only in one table, but also in multiple tables at the same time.

3. Primary key constraint (primary key) competition

The primary key constraint is equivalent to the combination of unique constraint and non-null constraint, and the primary key constraint column does not allow repetition or null values.

Each table allows at most one primary key, and establishing a primary key constraint can be created at the column level or at the table level.

When you create a constraint for a primary key, the system defaults to establishing a corresponding unique index on the column and column combination in which it resides.

4. Foreign key constraint (foreign key) FK

Foreign key constraints are used to strengthen the connection between one or more columns of data in two tables (master and slave tables) to ensure referential integrity between one or two tables. A foreign key is a reference relationship between two fields of a table or two fields of two tables.

The order in which foreign key constraints are created is to define the primary key of the primary table and then the foreign key of the slave table. In other words, only the primary key of the master table can be used as a foreign key in the slave table, the columns in the constrained slave table may not be the primary key, and the master table restricts the operation of updating and inserting from the slave table.

5. Default value constraint (Default)

If the default value constraint is defined in the table, the system assigns the default value to the column if the user inserts a new data row and the row does not specify data. If we do not set the default value, the system defaults to NULL.

6. Self-increasing constraint (AUTO_INCREMENT)

The self-increment constraint (AUTO_INCREMENT) can constrain any field, which is not necessarily a PRIMARY KEY field, which means that the self-incremented field is not equal to the primary key field.

But the primary key field of the PRIMARY_KEY constraint must be a self-increment field, that is, PRIMARY_KEY acts on the same field as AUTO_INCREMENT.

When inserting the first record, the self-increment field is not given a specific value and can be written as DEFAULT/NULL, so when inserting the field later, the self-increment field starts from 1, does not insert a record, and the value of the self-increment field increases by 1. When you insert the first record, give a specific value to the self-increment field, then the value of the later inserted record on the self-increment field will be increased by 1 at a time based on the value of the self-increment field of the first record.

You can also specify the rest of the fields to insert the record instead of specifying the self-increment field when inserting the record.

The above is about the content of this article on "what are the common constraints in MySQL?" I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report