【问题标题】:How to implement one-to-one relationship while designing tables.\如何在设计表格时实现一对一的关系。\
【发布时间】:2021-05-24 09:21:17
【问题描述】:

我只需要简单解释一下在设计表时如何实现一对一关系

【问题讨论】:

  • 在子表中放一个parent_id
  • 两列都必须定义为 UNIQUE 和 NOT NULL。

标签: mysql database datatable relationship


【解决方案1】:
CREATE TABLE master ( master_id INT PRIMARY KEY,
                      {another columns} );

CREATE TABLE slave ( slave_id INT PRIMARY KEY,
                     master_id INT NOT NULL,
                     UNIQUE (master_id),
                     FOREIGN KEY (master_id) REFERENCES master (master_id),
                      {another columns} );

slave 中与 master 中的行匹配的行必须由客户端逻辑检查。或者,slave 中的行可能是在创建master 的行期间插入的(通过触发器),但这可能与slave 中的某些列属性相矛盾。

【讨论】:

    猜你喜欢
    • 2011-11-09
    • 2015-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多