1
我有一个主,父表'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
感谢您的答复@craig。说得通。我假设一个触发器(比如说BEFORE INSERT ON transaction_)将在我对表分区施加的任何CHECK约束之前执行。它是否正确? – aerospatiale
@JoshuaLawes更正'BEFORE'触发器,是的。 –