Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SQL:建模模板继承_Sql_Database_Postgresql_Database Design_Relational Database - Fatal编程技术网

SQL:建模模板继承

SQL:建模模板继承,sql,database,postgresql,database-design,relational-database,Sql,Database,Postgresql,Database Design,Relational Database,我想问的是,是否有可能对一个模板化的数据结构进行建模,如果需要,可以覆盖它 假设我们有一个包含以下项目的列表: 模板列表 项目1位置0 项目2位置1 项目3位置2 项目4位置3 现在,我想创建一个列表,该列表使用模板列表作为基础,但会修改其中的某些部分: 基于模板列表的具体列表 项目1位置0//从模板列表继承 第5项位置1//新的且仅在混凝土列表中可用 项目4位置2//继承自模板列表,但位置不同 项目3位置3//继承自模板列表,但位置不同 在此列表中,模板列表中的项目2缺失,不应成为结果

我想问的是,是否有可能对一个模板化的数据结构进行建模,如果需要,可以覆盖它

假设我们有一个包含以下项目的列表:

模板列表

  • 项目1位置0
  • 项目2位置1
  • 项目3位置2
  • 项目4位置3
现在,我想创建一个列表,该列表使用模板列表作为基础,但会修改其中的某些部分:

基于模板列表的具体列表

  • 项目1位置0//从模板列表继承
  • 第5项位置1//新的且仅在混凝土列表中可用
  • 项目4位置2//继承自模板列表,但位置不同
  • 项目3位置3//继承自模板列表,但位置不同
在此列表中,模板列表中的项目2缺失,不应成为结果列表的一部分


可以在SQL中对这些关系建模吗?(我们正在使用PostgreSQL)

可以做一些您想要的事情,但这不一定是一个好的解决方案,也不一定是您需要的。您所要求的看起来像一个元模型,但关系数据库是为一阶逻辑模型设计的,虽然SQL可以超越这一点,但通常最好不要过于抽象

也就是说,这里有一个例子。我假设列表项的标识是基于位置或插槽的

CREATE TABLE template_list (
    template_list_id SERIAL NOT NULL,
    PRIMARY KEY (template_list_id)
);

CREATE TABLE template_list_items (
    template_list_id INTEGER NOT NULL,
    slot_number INTEGER NOT NULL,
    item_number INTEGER NOT NULL,
    PRIMARY KEY (template_list_id, slot_number),
    FOREIGN KEY (template_list_id) REFERENCES template_list (template_list_id)
);

CREATE TABLE concrete_list (
    concrete_list_id SERIAL NOT NULL,
    template_list_id INTEGER NOT NULL,
    FOREIGN KEY (template_list_id) REFERENCES template_list (template_list_id),
    UNIQUE (concrete_list_id, template_list_id)
);

CREATE TABLE concrete_list_items (
    concrete_list_id INTEGER NOT NULL,
    template_list_id INTEGER NOT NULL,
    slot_number INTEGER NOT NULL,
    item_number INTEGER NULL,
    PRIMARY KEY (concrete_list_id, slot_number),
    FOREIGN KEY (concrete_list_id, template_list_id) REFERENCES concrete_list (concrete_list_id, template_list_id),
    FOREIGN KEY (template_list_id, slot_number) REFERENCES template_list_items (template_list_id, slot_number)
);
现在,要获取具体列表中的项目,可以使用如下查询:

SELECT c.concrete_list_id, x.slot_number, x.item_number
FROM concrete_list c
LEFT JOIN (
  SELECT ci.concrete_list_id,
         COALESCE(ci.template_list_id, ti.template_list_id) AS template_list_id,
         COALESCE(ci.slot_number, ti.slot_number) AS slot_number,
         COALESCE(ci.item_number, ti.item_number) AS item_number
  FROM concrete_list_items AS ci
  FULL JOIN template_list_items AS ti ON ci.template_list_id = ti.template_list_id
                                     AND ci.slot_number = ti.slot_number
) x ON c.concrete_list_id = x.concrete_list_id OR c.template_list_id = x.template_list_id;

这是一个演示的例子。请注意,为了便于演示,我将串行类型替换为整数和硬编码值。

感谢您花时间回答!然后,我将在应用程序逻辑中保留元模型。