当前位置:网站首页>Classification, function and usage of MySQL constraints

Classification, function and usage of MySQL constraints

2022-07-06 22:24:00 Dark horse programmer official

 MySQL Strong performance , It is one of the most widely used databases at present , With  MySQL For learning the prototype, it is also convenient to master other databases later , Now let's give you Give a comprehensive explanation MySQL8.0 New features , From zero foundation to high-level one-stop learning , Combined with actual cases, we can gain something !

▼ MySQL8.0 introduction - Advanced learning notes :( Summary )


Catalog

One 、MySQL constraint

Concept

effect

classification

Two 、MySQL constraint - Primary key constraint

Concept

operation

operation - Add single column primary key

operation - Add multi column primary key ( Combined the primary key )

Add a primary key by modifying the table structure

Delete primary key constraint

3、 ... and 、MySQL constraint - Self growth constraints (auto_increment)

Concept

grammar

operation

characteristic

Specify the initial value of the auto increment field

delete and truncate Change of auto increment column after deletion

Four 、MySQL constraint - Non empty constraint (not null

Concept

grammar

Add non empty constraints - The way 1

Add non empty constraints - The way

Delete non empty constraints

Four 、MySQL constraint - Unique constraint (unique)

Concept

grammar

Add unique constraints - The way 1

Add unique constraints - The way 2

Delete unique constraint

5、 ... and 、MySQL constraint - Default constraint (default)

Concept

grammar

Add default constraint - The way 1

Add default constraint - The way 2

Delete default constraint

6、 ... and 、MySQL constraint - Zero fill constraint (zerofill)

Concept

operation

Delete


One 、MySQL constraint

Concept

Constrained English :constraint

Constraints are actually constraints on the data in the table

effect

The purpose of adding constraints in table design is to ensure the integrity and effectiveness of records in the table , For example, the values of some columns in the user table ( cell-phone number ) Can't be empty , Some column values ( ID number ) Can't repeat .

classification

  • Primary key constraint (primary key) PK
  • Self growth constraints (auto_increment)
  • Non empty constraint (not null)
  • Uniqueness constraint (unique)
  • Default constraint (default)
  • Zero fill constraint (zerofill)
  • Foreign key constraints (foreign key) FK

Two 、MySQL constraint - Primary key constraint

Concept

  • MySQL A primary key constraint is a column or a combination of columns , Its value uniquely identifies each row in the table , Convenient in RDBMS Find a line as soon as possible .
  • The primary key constraint is equivalent to Unique constraint + Non empty constraint The combination of , Primary key constraint columns are not allowed to be duplicate , Null values are not allowed .
  • Only one primary key is allowed per table
  • The key of the primary key constraint is :primary key
  • When creating constraints on primary keys , By default, the system will establish a unique index on the column and column combination .

operation

  • Add single column primary key
  • Add multi column joint primary key
  • Delete primary key

operation - Add single column primary key

There are two ways to create a single column primary key , One is to specify the primary key while defining the field , One is to specify the primary key after defining the field .

Method 1- grammar :

--  stay  create table  In the sentence , adopt  PRIMARY KEY  Keyword to specify the primary key .
-- Specify the primary key while defining the field , The syntax is as follows :
create table  Table name (
   ...
   < Field name > < data type > primary key 
   ...
)

Method 1- Realization :

create table emp1(
    eid int primay key,
    name VARCHAR(20),
    deptId int,
    salary double
);

The way 2- grammar :

-- Specify the primary key after defining the field , The syntax is as follows :
create table  Table name (
   ...
   [constraint < Constraint name >] primary key [ Field name ]
);

The way 2- Realization :

create table emp2(
    eid INT,
    name VARCHAR(20),
    deptId INT,
    salary double,
    constraint  pk1 primary key(id)
 );

operation - Add multi column primary key ( Combined the primary key )

The so-called federated primary key , The primary key is composed of multiple fields in a table .

Be careful :

  1. When the primary key is composed of multiple fields , You cannot declare a primary key constraint directly after a field name .
  2. A table can only have one primary key , A federated primary key is also a primary key

grammar :

create table  Table name (
   ...
   primary key ( Field 1, Field 2,…, Field n)
);

Realization :

create table emp3( 
  name varchar(20), 
  deptId int, 
  salary double, 
  primary key(name,deptId) 
);

Add a primary key by modifying the table structure

Primary key constraints can not only be created at the same time as the table is created , You can also add... When modifying the table .

grammar :

create table  Table name (
   ...
);
alter table < Table name > add primary key( Field list );

Realization :

--  Add single column primary key 
create table emp4(
  eid int, 
  name varchar(20), 
  deptId int, 
  salary double, 
);
alter table emp4 add primary key(eid);

Delete primary key constraint

When a primary key constraint is not required in a table , You need to remove it from the table . Deleting a primary key constraint is much easier than creating a primary key constraint .

Format :

alter table < Data table name > drop primary key;

Realization :

--  Delete single column primary key  
alter table emp1 drop primary key;
 
--  Delete federated primary key  
alter table emp5 drop primary key;

3、 ... and 、MySQL constraint - Self growth constraints (auto_increment)

Concept

stay MySQL in , When the primary key is defined as self growing , The value of the primary key no longer requires user input , The database system automatically assigns values according to the definition . Every time you add a record , The primary key will automatically grow in the same step size .

By adding auto_increment Property to realize the primary key self growth

grammar

 Field name   data type  auto_increment

operation

create table t_user1( 
  id int primary key auto_increment, 
  name varchar(20) 
);

characteristic

  • By default ,auto_increment The initial value of 1, Every new record , The field value is automatically added 1.
  • Only one field can be used in a table auto_increment constraint , And the field must have a unique index , To avoid repetition of serial numbers ( It is the primary key or part of the primary key ).
  • auto_increment The constrained field must have NOT NULL attribute .
  • auto_increment The field of constraint can only be integer type (TINYINT、SMALLINT、INT、BIGINT etc. .
  • auto_increment The maximum value of a constraint field is constrained by the data type of the field , If it reaches the upper limit ,auto_increment It will fail. .

Specify the initial value of the auto increment field

If the first record sets the initial value of this field , Then the newly added record will increase from this initial value . for example , If the first record inserted in the table is id Value is set to 5, Then insert the record again ,id The value will change from 5 Begin to increase

--  The way 1, Specify... When creating a table 
create table t_user2 ( 
  id int primary key auto_increment, 
  name varchar(20)
)auto_increment=100;
--  The way 2, After creating the table, specify 
create table t_user3 ( 
  id int primary key auto_increment, 
  name varchar(20)
);

alter table t_user2 auto_increment=100;

delete and truncate Change of auto increment column after deletion

  • delete The data will grow automatically, starting from the breakpoint
  • truncate The data will grow automatically after, starting from the default starting value

Four 、MySQL constraint - Non empty constraint (not null

Concept

MySQL Non empty constraint (not null) The value of the field cannot be empty . For fields with non null constraints , If the user does not specify a value when adding data , The database system will report an error .

grammar

 The way 1:< Field name >< data type > not null;
 The way 2:alter table  Table name  modify  Field   type  not null;

Add non empty constraints - The way 1

--  The way 1, Specify... When creating a table 
create table t_user6 ( 
  id int , 
  name varchar(20) not null, 
  address varchar(20) not null 
);

Add non empty constraints - The way

create table t_user7 ( 
  id int , 
  name varchar(20) , --  Specify a non NULL constraint  
  address varchar(20) --  Specify a non NULL constraint  
); 
alter table t_user7 modify name varchar(20) not null; 
alter table t_user7 modify address varchar(20) not null;

Delete non empty constraints

-- alter table  Table name  modify  Field   type  
alter table t_user7 modify name varchar(20) ; 
alter table t_user7 modify address varchar(20) ;

Four 、MySQL constraint - Unique constraint (unique)

Concept

Unique constraint (Unique Key) It means that the values of fields in all records cannot be repeated . for example , by id After the uniqueness constraint is added to the field , For each record id Value is the only , There can be no repetition .

grammar

 The way 1:< Field name > < data type > unique
 The way 2: alter table  Table name  add constraint  Constraint name  unique( Column );

Add unique constraints - The way 1

--  Specify... When creating a table 
create table t_user8 ( 
 id int , 
 name varchar(20) , 
 phone_number varchar(20) unique --  Specify a unique constraint  
);

Add unique constraints - The way 2

create table t_user9 ( 
  id int , 
  name varchar(20) , 
  phone_number varchar(20) --  Specify a unique constraint  
); 
alter table t_user9 add constraint unique_ph unique(phone_number);

Delete unique constraint

-- alter table < Table name > drop index < Unique constraint name >;
alter table t_user9 drop index unique_ph;

5、 ... and 、MySQL constraint - Default constraint (default)

Concept

MySQL The default value constraint is used to specify the default value of a column .

grammar

 The way 1: < Field name > < data type > default < The default value is >;
 The way 2: alter table  Table name  modify  Name   type  default  The default value is ; 

Add default constraint - The way 1

create table t_user10 ( 
  id int , 
  name varchar(20) , 
  address varchar(20) default ‘ Beijing ’ --  Specify the default constraint  
);

Add default constraint - The way 2

-- alter table  Table name  modify  Name   type  default  The default value is ; 

create table t_user11 ( 
  id int , 
  name varchar(20) , 
  address varchar(20)  
);
alter table t_user11 modify address varchar(20) default  ‘ Beijing ’;

Delete default constraint

-- alter table < Table name > modify column < Field name > < type > default null; 

alter table t_user11 modify column address varchar(20) default null;

6、 ... and 、MySQL constraint - Zero fill constraint (zerofill)

Concept

1、 When inserting data , When the length of the value of this field is less than the defined length , This value will be preceded by the corresponding 0

2、zerofill The default is int(10)

3、 When using zerofill when , By default, it will automatically add unsigned( Unsigned ) attribute , Use unsigned After attribute , The numerical range is of the original value 2 times , for example , The sign is -128~+127, No sign is 0~256.

operation

create table t_user12 ( 
  id int zerofill , --  Zero fill constraint 
  name varchar(20)   
);

Delete

alter table t_user12 modify id int;

原网站

版权声明
本文为[Dark horse programmer official]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207061438481550.html