MySQL Chapter 8 Database Constraints

Chapter 8 Database Constraints

8-1 Introduction to
database constraints 8-2 Primary key constraints
of database constraints 8-3 Unique constraints of
database constraints 8-4 Non-null constraints of database constraints
8-5 Setting of default values

8-1 Introduction to Database Constraints

Database constraints :

Further restrict the data in the table to ensure the correctness , validity and integrity of the data

primary key: primary key

unique: unique

not null: not empty

default: default

foreign key: foreign key

8-2 Primary key constraints of database constraints

First, the role of the primary key:

1. The primary key is used to uniquely identify a record , each table should have a primary key, and each table can only have one primary key

2. Which field should be used as the primary key of the table?
Usually, the business field is not used as the primary key, and an id field is separately designed for each table, and the id field is used as the primary key. Primary keys are used by databases and programs, not by end customers. So it doesn't matter whether the primary key has meaning or not, as long as it is not repeated and not empty.

2. Create a primary key

Primary key: PRIMARY KEY

Features of primary key:

  • Primary keys must contain unique values;
  • Primary key columns cannot contain NULL values

How to create a primary key:

Add a primary key to a field when creating a table:
field name field type PRIMARY KEY

Third, delete the primary key

ALTER TABLE table name DROP PRIMARY KEY;

Fourth, the primary key auto-increment

  • AUTO_INCREMENT means automatic growth (the field type must be an integer type)

expand

  • The default start value of AUTO_INCREMENT is 1. If you want to modify the start value, use the following SQL syntax:

  • ALTER TABLE table name AUTO_INCREMENT=initial value;

8. The difference between DELETE and TRUNCATE

DELETE: delete data in the table without repeating the value of AUTO_INCREMENT

TRUNCATE destroys the table, rebuilds the table, resets AUTO_INCREMENT to 1

8-3 Unique Constraints of Database Constraints

1. The only

The value of this field cannot be repeated in this table

2. The basic format of the unique constraint:

field name field type UNIQUE

eg: Create a hero table hero3, which contains the field (id, name) and set a unique constraint on the name column, and heroes with the same name cannot appear.

CREATE TABLE hero3(
id INT,
name VARCHAR(29) UNIQUE
);

实现唯一约束,不能插入相同的值,
但是NULL没有值,所以不存在重复的值,
可以插入多个NULL

8-4 Database Constraints Not Null Constraints

1. Non-null: This field must be set to a value and cannot be NULL

2. The basic syntax of non-null constraints: field name field type NOT NULL

3. Can be matched with unique constraints: field name field type UNIQUE NOT NULL

8-5 Setting of Default Values

1. Default value: When adding data to the table, if the data of this field is not specified, the default value is used

2. Basic syntax structure: field name field type DEFAULT default value

Guess you like

Origin blog.csdn.net/weixin_44411458/article/details/124350783
Recommended