MySQL database constraints (not null, unique, default, primary key, foreign key constraints)


1. Overview of constraint types

  • NOT NULL - Indicates that a column cannot store NULL values.

  • UNIQUE - Ensure that each data in this column cannot be repeated.

  • DEFAULT - specifies the default value when no value is assigned to the column. If a default value is set for a column. When inserting data, if no value is inserted for this column, the default value will be inserted as a value

  • PRIMARY KEY - A combination of NOT NULL and UNIQUE. Ensuring that a column (or a combination of two or more columns) is uniquely identified makes it easier and faster to find a particular record in a table. Note: There is not necessarily only one primary key (such as a student of one school - the primary key is the student number, and a student of multiple schools - the primary key is the school and student number)

  • FOREIGN KEY - Ensuring that data in one table matches values ​​in another table with referential integrity.

  • CHECK - Guarantees that the values ​​in the column meet the specified criteria. For MySQL databases, the CHECK clause is parsed, but the CHECK clause is ignored. (Generally, the value of the data is specified within a range, and an error will be reported if it exceeds this range)


2. Detailed explanation of constraint types

1. NULL constraint

When creating a table, you can specify that a column is not empty:
insert image description here

  1. If the specified column is not empty, and no data is inserted for that column when inserting data, an error will be reported
  2. You can add the default value after NOT NULL, then no error will be reported

2.UNIQUE: unique constraint

Specify the sn column as unique and non-repeating:
insert image description here
inserting a duplicate column will result in an error

3. DEFAULT: default value constraint

When specifying to insert data, the name column is empty, and the default value is unknown:
insert image description here
Default The default value execution condition is not to display any value inserted, including NULL; if you insert NULL when the given value is NULL, then default will not be executed If the default value is set, NULL will be displayed directly.

4. PRIMARY KEY: primary key constraint (auto)

Specify the id column as the primary key:

insert image description here
PRIMARY KEY is a combination of NOT NULL and UNIQUE

  1. The primary key can be used on multiple columns, representing a composite primary key
  2. For the primary key of integer type, it is often used with auto_increment. When the corresponding field of the inserted data does not give a value, the maximum value +1 is used. It is equivalent to giving a default value, and then if the insertion is not displayed, the default value will be +1

When we design the database, we can insert a value into the id without displaying it, and let it increase automatically. This is a common method.

5. FOREIGN KEY: foreign key constraints

insert image description here
Foreign keys are used to associate primary keys or unique keys of other tables, syntax:insert image description here
insert image description here

When using Mysql keywords as fields, you need to use `` to identify them.
Comments cannot be added after the statement of the associated foreign key.

insert image description here

The class id is the main table, and the student foreign key is associated with the class primary key, so the definition of the foreign key is placed in the student table

Copyright statement: This article is an original article of CSDN blogger "_Zebra", following the CC 4.0 BY-SA copyright agreement, please attach the original source link and this statement for reprinting.
Original link: https://blog.csdn.net/qq_24016309/article/details/123532538

Guess you like

Origin blog.csdn.net/qq_33183456/article/details/123802461