我有一个主要的父表“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