【发布时间】:2014-01-29 01:21:02
【问题描述】:
我有一个主表“transaction_”,我想对它进行分区。我知道我可以使用任何子表中的检查约束轻松地根据 transaction_ 中列出的任何字段(包括外键)进行分区。基本上我想知道的是,在我的检查约束中,我是否可以以某种方式引用表中我有外键的其他字段。我想避免在我的 transaction_ 表中包含太多来自卖方和客户表的外键,因为这似乎有很多不必要的重复。
CREATE SEQUENCE transaction_id_seq;
CREATE TABLE transaction_ (
transaction_id bigint PRIMARY KEY DEFAULT nextval('transaction_id_seq'),
seller_id int REFERENCES seller(id),
client_id int REFERENCES client(id),
purchase_date date,
purchase_time time,
price real,
quantity int
);
CREATE TABLE seller (
id int PRIMARY KEY,
name text,
location text,
open_time time,
close_time time
);
CREATE TABLE client (
id int PRIMARY KEY,
name text,
billing_suburb text,
billing_zipcode int
);
例如,我认为我可以做到以下几点:
CREATE TABLE transaction_client1_20130108 (
CHECK ( client_id = 1 AND purchase_date = DATE '2013-01-08')
) INHERITS (transaction_);
我想做如下的事情:
CREATE TABLE transaction_sellerZip90210_20130108 (
CHECK ( client(billing_zipcode) = 90210 AND purchase_date = DATE '2013-01-08')
) INHERITS (transaction_);
如果提供更好的解决方案,请使用以下内容,但很高兴更新:
mydb=#SELECT version();
PostgreSQL 9.1.11 on x86_64-unknown-linux-gnu, compiled by gcc (Ubuntu/Linaro 4.8.1-10ubuntu9) 4.8.1, 64-bit
【问题讨论】:
标签: postgresql partitioning check-constraints cross-reference