MySQL多对多操作

MySQL多对多

返回首页

示例1:

  用户表和相亲记录表
  用户表

    用户id    用户名                  性别

    1        George      男
    2        Elizabeth     女
    3        Bruce      男
    4        Catherine     女

  相亲表

    相亲组id              用户甲    用户乙

     1        1           2

     2         1           4

     3         2           1

     4         2           3


示例2:

  用户主机关系表

  用户表userinfo

    用户id    用户名                  

    1        George       
    2        Elizabeth      
    3        Bruce         
    4        Catherine      


  主机表host

     主机id    主机名

    1       c1

    2        c2

    3          c3

  用户主机关系表user2host

    id      用户id    主机id

    1        1       1

    2        1      3

    3        2        1


create table userinfo(id int auto_increment primary key,

          name char(10),

          gender char(10),

          email varchar(64)

          )engine=innodb default charset=utf8;

create table host(id int auto_increment primary key,

         hostname char(64)

        )engine=innodb default charset=utf8;

create table user2host(id int auto_increment primary key,

            userid int not null,

            hostid int not null,

            unique uq_user_host (userid,hostid),

            constraint fk_u2h_user foreignkey (userid) references userinfo(id),

            constraint fk_u2h_host foreign key (hostid) references host(id)

            )engine=innodb default charset=utf8;

  

-------- END --------

原文地址:https://www.cnblogs.com/george92/p/7308514.html