MySQL foreign key unique index +

MySQL foreign key unique index +

2019-08-22

1. Foreign Key

Refers to the table is a table of rows and columns of another connection relationship can be used for simple numbers or letters instead of a complex data structures, not only save space, but also has constraint function, can reduce the chance of error writing.

1.1 use

constraint variable name Foreign  Key (column names) References table 2 (column name 2);

Where the variable names are given their own, not have agreed to repeat;

I.e., column names need to connect foreign key column;

Table 2 refers to a value of a foreign key of the table;

2 is the column name in the table and the columns corresponding to the connection;

1.2 Examples

For example, the following two tables, Table 1 is a state machine table, machine information table in Table 2; the four states, four machine represented by four values:

create table machinestatus(
    status_id int auto_increment primary key,
    status_name char(20)
    )engine=innodb default charset=utf8;
insert into machinestatus(status_name) values("待机"),("运行"),("故障"),("关机");

create table machineinfo(
    id int auto_increment primary key,
    identifier int not null,
    status int,
    constraint fk_info_status foreign key (status) references machinestatus(status_id)
    )engine=innodb default charset=utf8;

Upcoming machineinfo table status and machinestatus the id correspondence.

 2. The only index

The only index keyword: unique; has the following features

  • Unrepeatable
  • Find accelerate
  • Can be empty
  • United may be generated by multiple columns

The only difference is the primary key index: 1) may have a unique index data is empty, 2) may be more than one unique index in a table.

2.1 Use:

unique variable name (column 1, row 2 ...)

If it is the only single-column index in brackets to fill a column name.

2.2 Examples

Single-column unique index

create table userinfo(
    id int auto_increment primary key,
    age int,
    name char(10),
    card int not null,
    email char(20),
    unique ucard (card)
    )engine=innodb default charset=utf8;

United unique index

create table userinfo(
    id int auto_increment primary key,
    age int,
    name char(10),
    card int not null,
    phone_num char(20),
    email char(20),
    unique ucard (card,phone_num)
    )engine=innodb default charset=utf8;

3. foreign key unique index +

3.1-many foreign key references

Data in the table is a foreign key connection may appear repeatedly using the same table; e.g.

- the department table 
the Create  the Table Department ( 
    the above mentioned id int  not  null AUTO_INCREMENT Primary  Key , 
    Depart's char ( 64- ) 
    ) Engine = InnoDB default charset = utf8; 

- employee information table 
the Create  the Table userinfo3 ( 
    the above mentioned id int  not  null AUTO_INCREMENT Primary  Key , 
    name char ( 20 ) not  null , 
    Ageint,
    depart_id int not null,
    constraint fk_u2d_depart foreign key (depart_id) references department(id)
    )engine=innodb default charset=utf8;

Department id value sector of the table can be repeated several times cited employee information table.

+ 3.2 foreign key constraints to achieve one unique index constraint references

--用户表
create table userinfo(
    id int not null auto_increment primary key,
    name char(20) not null,
    )engine=innodb default charset=utf8;

--旅行登记表
create table travelregister(
    id int not null auto_increment primary key,
    job_num int,
    constraint fk_t2u_depart foreign key (job_num) references userinfo(id),
    unique uj (job_num)
    )engine=innodb default charset=utf8;

Travel is registered in the registration form to participate in traveling employees, each employee registered at most once, by a foreign key referencing the id of the employee table to represent the employees themselves, through unique to limit staff repeated; the staff table id value most cited once.

3.3 foreign key constraints to achieve many-reference

You can duplicate records

--员工表
create table userinfo2(
    id int auto_increment primary key,
    name char(10) not null,
    gender char(10),
    )engine=innodb default charset=utf8;

--主机表
create table host(
    id int auto_increment primary key,
    hostname char(64) not null
    )engine=innodb default charset=utf8;

--员工主机关系表
create table user2host(
    id int auto_increment primary key,
    user_id int not null,
    host_id int not null,
    constraint fk_u2h_user foreign key (user_id) references userinfo2(id),
    constraint fk_u2h_host foreign key (host_id) references host(id)
    )engine=innodb default charset=utf8;

+ 3.4 foreign key constraints to achieve a unique index constraint-many references

You can prevent duplicate records

--员工表
create table userinfo2(
    id int auto_increment primary key,
    name char(10) not null,
    gender char(10),
    )engine=innodb default charset=utf8;

--主机表
create table host(
    id int auto_increment primary key,
    hostname char(64) not null
    )engine=innodb default charset=utf8;

--员工主机关系表
create table user2host(
    id int auto_increment primary key,
    user_id int not null,
    host_id int not null,
    constraint fk_u2h_user foreign key (user_id) references userinfo2(id),
    constraint fk_u2h_host foreign key (host_id) references host(id),
    unique uq_user_host (user_id,host_id)
    )engine=innodb default charset=utf8;

 

Guess you like

Origin www.cnblogs.com/sienbo/p/11397214.html