Mysql primary key constraints and unique constraints

Mysql constraints

1. Function

Constraints are defined as rules that must be followed to ensure data integrity.
Constraints can be created during table creation or added later.
When you add a constraint after creating a table, it checks the existing data to determine if it violates the constraint.
If the constraint to be added is violated by existing data, the constraint will not be applied to the specified column.

2. Type of constraints

MySQL supports the following constraints that can be defined on columns in a table:

1) Primary key constraints
2) Unique constraint
3) Foreign key constraints
3. Primary key constraints

Definition of a column or a set of columns whose valuesonlyIdentifies all rows in the table.
These columns are called primary key columns.
Primary key columns cannot contain NULL values, as it is used to uniquely identify rows in a table.

grammar:

CREATE TABLE table_name
(
<column definition 1>,
<column definition 2>,
............
<constraint_definition>);

<constraint_definition>=
[CONSTRAINT constraint_name] PRIMARY KEY (<column_name> [{
   
   ,
<column_name>}...]))

Example:
Create a new table named emp1. The column names are id, name, age and address. Set id as the primary key.

create table emp1 
(
id int,
name varchar(20),
age int,
address varchar(30),
Constraint Key_ID primary key(id)
);

or

create table emp1 
(
id int primary key,
name varchar(20),
age int,
address varchar(30)
);

Note: In version 1 of the code [Constraint Key_ID primary key(id)] is the standard code for setting the id column as the primary key. Constraint is the fixed format of the constraint. Key_ID is the name of the set constraint. You can choose it at will. Primary key is the only constraint. Keyword, (id) means setting the id column to be unique

You can use this button to see that the id of emp1 is already the primary key and has a key mark, or you can view the structure through the desc command.
Insert image description here
Insert image description here

4. The only constraint

used forEnforce uniqueness on non-primary key columns=.
Similar to a primary key constraint, but on the column defined by a unique constraintAllow NULL values
Canon the tableCreate multiple unique constraints

grammar:

CREATE TABLE table_name
(
<column_definition_1>,
<column_definition_2>,
.........
<constraint_definition>


<constraint_definition>=
[CONSTRAINT constraint_name] UNIQUE (<column_name> 
[{
   
   ,
<column_name>}...]))

Example:

Design table emp2, add a phone number column as before, because we know that phone is unique, design id as the primary key, and design phone as unique

create table emp2 
(
id int,
name varchar(20),
age int,
address varchar(30),
phone int,
Constraint Key_ID primary key(id),
Constraint Unique_Phone UNIQUE(phone)
);

or

create table emp2 
(
id int Primary Key,
name varchar(20),
age int,
address varchar(30),
phone int UNIQUE
);

Note: In the code version 1, [Constraint Unique_Phone UNIQUE(phone)] is the code to set the phone column to be unique. Constraint is the fixed format of the constraint. Unique_Phone is the name of the set constraint. You can choose it at will. UNIQUE is the keyword of the unique constraint. , (phone) means setting the phone column to be unique.
Insert image description here

5. Foreign key constraints (details will be introduced later)

Eliminate inconsistencies in two tables when data in one table depends on data in another table.
Foreign keys in a table always refer to primary key columns in other tables.
A table that contains a primary key that is referenced as a foreign key in another table is called the primary table or the referenced table.
Tables that contain foreign keys that reference primary keys in other tables are called transaction tables or reference tables.

Guess you like

Origin blog.csdn.net/agatha_aggie/article/details/129294182