Restrictions

Role: used to ensure data integrity and consistency

Include:

PRIMARY  KEY (PK) identifies the field as the primary key of the table, which can uniquely identify the record
 FOREIGN  KEY (FK) identifies the field as the foreign key of the table
 NOT  NULL     identifies the field cannot be empty
 UNIQUE  KEY (UK) identifies the field's Values ​​are unique (uniqueness constraint)
AUTO_INCREMENT indicates that the value of this field grows automatically (integer type, and the primary key)
DEFAULT     sets the default value for the field

UNSIGNED unsigned
ZEROFILL is filled with 0 
--------------------------------------------- --------
The primary key is not null by default after it is created,
but not null is not displayed.
null means empty, but not a string, not null is the same as



the uniqueness constraint:
constraint host_port unique(host,port) 
#constraint host_port This is only used to set the name of the unique constraint, or you can have it without setting the default
 

 

A table can:

Single column as primary key
Multiple columns as primary key (composite primary key)

But a table can only have one primary key primary key 


defines a single primary key:
  1. When no primary key is declared, the first not null+unique is the primary key by default
  2. Add primary key after a field
  3. Define constraints separately pk_name primary key(id); #Create a primary key and name it pk_name

Define multiple primary keys:

  1. Define primary key(ip, port) separately

 

Auto-increment constraints:

self_incerment

offset: auto_increment_offset defaults to 1
 
    1. When setting auto increment, it starts with 10
      1. create table dep1(
        id int primary key auto_increment,
        )auto_increment = 10;
    2. a

uto_increment_increment:

    Auto increment step size, default is 1
    1. create table dep3(
      id int primary key auto_increment,
       name char(10)
      );
      
      
      # Session: connect to the server through the client (one link is called a session)
      set session auto_increment_increment =  2 ; #Session level, only valid for the current session
       set global auto_increment_increment = 2 ; #Global, valid for all sessions
       insert  into dep3(name) values ​​( ' IT ' ​​),( ' HR ' ),( ' SALE ' ),( ' Boss ' );
  1. show variables like '%auto_in%'; #View variables. As long as auto_in is included, it will be checked out

 Foreign key constraints:

 

There is a relationship between the two tables, 

First build a department table (associated table) main table
 create  table dep(
id int  not  null  unique ,
name varchar(50),
comment varchar(100)
);
Rebuild the employee table (association table) sub-table
 create  table emp_info(
id int primary key auto_increment,
namevarchar(20),
dep_id int ,
 constraint FK_depid_id foreign  key (dep_id) references dep(id) # references : associated
 on  delete  cascade #The   associated table is deleted, and the associated table is also deleted
 on  update  cascade #The   associated table is modified, the associated table also modified
);

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325172111&siteId=291194637
Recommended