我想建立一个具有不同模式的数据库,这些模式对应于不同的数据输入系统。需要有一个“master”(管理)模式和一个“party”表,以及多个其他模式,它们有自己的“party”表,这些表继承了 master party 表的字段
这是结构的一个简化示例:
'家长'
CREATE TABLE master.parties
(
party_key serial NOT NULL,
name text,
date_of_birth date,
phone text,
CONSTRAINT "PK_master_parties" PRIMARY KEY (party_key)
)
Run Code Online (Sandbox Code Playgroud)
'孩子'
CREATE TABLE corporate.parties
(
corp_key integer,
corp_role text
) INHERITS (master.parties)
Run Code Online (Sandbox Code Playgroud)
我希望能够删除添加到 master.parties 表中的一行,并将其插入到其他模式(即公司)中的任何party表中,而无需在主表中创建重复的行。
我在另一篇文章(/sf/ask/2065641371/)上找到的这个解决方案非常接近我正在寻找的为了:
WITH deleted AS (
DELETE FROM ONLY master.parties
WHERE party_key = 1
returning *
)
INSERT INTO corporate.parties (name, date_of_birth, phone, corp_key, corp_role)
SELECT name, date_of_birth, phone, 2, 'President'
from deleted;
Run Code Online (Sandbox Code Playgroud)
但是,在我看来,应该有更好的方法将“父”表中的一行添加到“子”表中,而不必删除“父”表中的现有行。
该解决方案还带来了另一个更严重的问题:如果对主模式中的参与方表进行了更改(例如添加了新列),我不想更新与子模式对应的所有数据输入系统。 -schemas 在每个 INSERT …
在以下架构中:
Collections
Upvotes
Reports
Upvotes
Reviews
Upvotes
Run Code Online (Sandbox Code Playgroud)
我很想有一个Upvotes表,一个单一的"entityId"列存储要么CollectionId,ReportId或ReviewId。还将有一个枚举用于Type存储collection, report, 或review。
该entityId会一直是必需的,而逻辑将始终确保刀片在每个实施唯一性type。
这样做的好处是添加另一种类型只是扩展枚举的问题。一切都将生活在一张没有冗余的桌子上。
成本似乎是逻辑方面增加的复杂性,它包含在我的应用程序逻辑中将新实体插入表中的一个地方。
实际上,这种方法有什么问题吗?避免这种情况的其他原因是什么?
I have a MySQL database with 3 tables holding the main classes of data:
companies (company_id)
persons (person_id, company_id)
loans (loan_id, company_id)
Run Code Online (Sandbox Code Playgroud)
Both a 'loan' and a 'person' belong to a company. A company can have loans and a company can have people (such as directors, employees etc.)
There are several scenarios where other data can belong to either a company, a person or a loan, such as 'notes'. For example a user can add a 'note' specific to either …
如果我创建一个新架构并将架构所有者设置为另一个架构,例如 dbo,那么这对我的新架构有何意义?
我的新架构是否继承了其所有者 dbo 的相同权限?
在 PostgreSQL 9.5.0 中,我有一个按月收集数据的分区表。尝试使用PostgreSQL新增的外表继承特性,将一个月的数据推送到另一台PostgreSQL服务器,结果得到了外表。当我从主服务器运行查询时,执行查询所需的时间比在我拥有外部表的服务器上长 7 倍。我没有通过网络传递大量数据,我的查询如下所示:
explain analyze
SELECT source, global_action, paid, organic, device, count(*) as count, sum(price) as sum
FROM "toys"
WHERE "toys"."container_id" = 857 AND (toys.created_at >= '2015-12-02 05:00:00.000000') AND
(toys.created_at <= '2015-12-30 04:59:59.999999') AND ("toys"."source" IS NOT NULL)
GROUP BY "toys"."source", "toys"."global_action", "toys"."paid", "toys"."organic", "toys"."device";
HashAggregate (cost=1143634.94..1143649.10 rows=1133 width=15) (actual time=1556.894..1557.017 rows=372 loops=1)
Group Key: toys.source, toys.global_action, toys.paid, toys.organic, toys.device
-> Append (cost=0.00..1143585.38 rows=2832 width=15) (actual time=113.420..1507.373 rows=76593 loops=1)
-> Seq Scan on toys (cost=0.00..0.00 rows=1 width=242) …Run Code Online (Sandbox Code Playgroud) postgresql performance inheritance foreign-data postgresql-9.5 postgresql-performance
inheritance ×5
schema ×3
postgresql ×2
duplication ×1
foreign-data ×1
mysql ×1
owner ×1
performance ×1
sql-server ×1