zoukankan      html  css  js  c++  java
  • MySql创建多表关联的步骤

    一,一对多表的创建

    1.创建主表

    create table HostTable(
      cid varchar(32) primary key,
      cname varchar(100)
    );

    2.创建从表

    create table FromTable(
      pid varchar(32) primary key,
      pname varchar(40),
      price double,
    );

    3.给从表创建从键列

    alter table FromTable add hosttable_id varchar(32);

    4.添加约束

    alter table FromTable  add constraint fromtable _fk foreign key(hosttable_id ) references HostTable(cid);

    二,多对多表的创建

    引入一张中间表,存储两个从键分别引用于两个主键。两个从键可以多次重复。这样就实现了多对多的表关系。

    create table  InterTable(

      hosttableid  varchar(32),

      fromtableid  varchar(32) 

    );

    1.添加联合主键

    alter table InterTable add primary key (hosttableid, fromtableid );

    2.添加约束

    alter table InterTable add constraint   inter_hosttable_fk foreign key (hosttableid) referrences HostTable(cid);

    alter table InterTable add constraint   inter_fromtable_fk foreign key (fromtableid) referrences FromTable(pid );

    这时InterTable 和 HostTable ,FromTable分别都是一对多的

  • 相关阅读:
    【Leetcode】113Path Sum II
    【leetcode】112. Path Sum
    virtualbox 中安装win7虚拟机
    制作一个vagrant的win7 box
    socket编程
    异常处理
    strip(),replace()和re.sub()用法
    面象对象 高阶篇
    面象对象 基础篇
    Subprocess模块介绍
  • 原文地址:https://www.cnblogs.com/MrZhang1/p/7296071.html
Copyright © 2011-2022 走看看