What are the common constraints in mysql?
Mysql common constraints include: 1. Non-null constraint "not null"; 2. Unique constraint "unique"; 3. Primary key constraint "primary key"; 4. Foreign key constraint "foreign key"; 5 , default value constraint "Default" and so on.
Recommendation: "mysql tutorial"
Constraints are a kind of restriction, which pass through the table Limit the row or column data to ensure the integrity and uniqueness of the table data. The following article will introduce you to 6 common MySQL constraints. I hope it will be helpful to you.
1. Non-null constraint (not null)
The non-null constraint is used to ensure that the value of the current column is not null. The non-null constraint can only Appears on a column of a table object.
Null type characteristics: All types of values can be null, including int, float and other data types
2. Unique constraints (unique)
The unique constraint is that the columns or column combinations 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 nulls.
The same table can have multiple unique constraints and constraints on multiple column combinations.
When creating a unique constraint, if you do not give a unique constraint name, it will default to the same as the column name.
Unique constraints can not only be created within one table, but also combined unique constraints can be created in multiple tables at the same time.
3. Primary key constraint (primary key) PK
The primary key constraint is equivalent to the combination of the unique constraint non-null constraint. The primary key constraint column is not allowed to be repeated or appear. NULL value.
Each table allows only one primary key at most. Primary key constraints can be created at the column level or at the table level.
When creating a primary key constraint, the system will by default create a corresponding unique index on the column and column combination.
4. Foreign key constraint (foreign key) FK
Foreign key constraint is used to strengthen a column of two tables (master table and slave table) Or the connection between multiple columns of data, which can ensure the referential integrity between one or two tables. The foreign key is built on the reference relationship between two fields of one table or two fields of two tables.
The order of creating foreign key constraints is to first define the primary key of the master table, and then define the foreign key of the slave table. That is to say, only the primary key of the primary table can be used as a foreign key by the secondary table. The columns in the constrained secondary table may not be the primary key. The primary table limits the update and insertion operations of the secondary table.
5. Default value constraint (Default)
If a default value constraint is defined in the table, when the user inserts a new data row, if the If the row does not specify data, the system assigns the default value to the column. If we do not set the default value, the system defaults to NULL.
6. Auto-increment constraint (AUTO_INCREMENT)
Auto-increment constraint (AUTO_INCREMENT) can constrain any field, which is not necessarily a PRIMARY KEY field. In other words, the auto-incremented field is not equal to the primary key field.
But the primary key field of the PRIMARY_KEY constraint must be an auto-increment field, that is, PRIMARY_KEY and AUTO_INCREMENT must act on the same field.
When inserting the first record, the auto-increment field is not given a specific value and can be written as DEFAULT/NULL. Then when the field is inserted in the future, the auto-increment field starts from 1 and no record is inserted. , the value of this auto-increment field increases by 1. When the first record is inserted, a specific value is given to the auto-increment field. Then the value of the auto-increment field in the records inserted thereafter will increase by 1 each time based on the value of the auto-increment field in the first record.
You can also specify the remaining fields to insert the record without specifying the auto-increment field when inserting the record.
The above is the detailed content of What are the common constraints in mysql?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

The process of starting MySQL in Docker consists of the following steps: Pull the MySQL image to create and start the container, set the root user password, and map the port verification connection Create the database and the user grants all permissions to the database

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

I encountered a tricky problem when developing a small application: the need to quickly integrate a lightweight database operation library. After trying multiple libraries, I found that they either have too much functionality or are not very compatible. Eventually, I found minii/db, a simplified version based on Yii2 that solved my problem perfectly.

Article summary: This article provides detailed step-by-step instructions to guide readers on how to easily install the Laravel framework. Laravel is a powerful PHP framework that speeds up the development process of web applications. This tutorial covers the installation process from system requirements to configuring databases and setting up routing. By following these steps, readers can quickly and efficiently lay a solid foundation for their Laravel project.

MySQL and phpMyAdmin are powerful database management tools. 1) MySQL is used to create databases and tables, and to execute DML and SQL queries. 2) phpMyAdmin provides an intuitive interface for database management, table structure management, data operations and user permission management.

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

MySQL is suitable for web applications and content management systems and is popular for its open source, high performance and ease of use. 1) Compared with PostgreSQL, MySQL performs better in simple queries and high concurrent read operations. 2) Compared with Oracle, MySQL is more popular among small and medium-sized enterprises because of its open source and low cost. 3) Compared with Microsoft SQL Server, MySQL is more suitable for cross-platform applications. 4) Unlike MongoDB, MySQL is more suitable for structured data and transaction processing.
