PostgreSQL 11 新特性之分割槽表外來鍵
阿新 • • 發佈:2019-01-09
文章目錄
對於 PostgreSQL 10 中的分割槽表,無法建立引用其他表的外來鍵約束。
-- PostgreSQL 10
CREATE TABLE cities (
city_id int not null PRIMARY KEY,
name text not null
);
CREATE TABLE measurement (
city_id int not null REFERENCES cities(city_id),
logdate date not null ,
peaktemp int,
unitsales int
) PARTITION BY RANGE (logdate);
ERROR: foreign key constraints are not supported on partitioned tables
LINE 2: city_id int not null REFERENCES cities(city_id),
^
PostgreSQL 11 解決了這個限制,可以建立分割槽表上的外來鍵。
-- PostgreSQL 11
CREATE TABLE cities (
city_id int not null PRIMARY KEY,
name text not null
);
CREATE TABLE measurement (
city_id int not null REFERENCES cities(city_id),
logdate date not null,
peaktemp int,
unitsales int
) PARTITION BY RANGE (logdate);
\d measurement
Table "public.measurement"
Column | Type | Collation | Nullable | Default
-----------+---------+-----------+----------+---------
city_id | integer | | not null |
logdate | date | | not null |
peaktemp | integer | | |
unitsales | integer | | |
Partition key: RANGE (logdate)
Foreign-key constraints:
"measurement_city_id_fkey" FOREIGN KEY (city_id) REFERENCES cities(city_id)
Number of partitions: 0
目前,還不支援引用分割槽表的外來鍵,也就是說分割槽表不能作為外來鍵引用中的父表。
CREATE TABLE orders (
order_id int not null,
order_date date not null,
PRIMARY KEY (order_id, order_date)
) PARTITION BY RANGE (order_date);
CREATE TABLE orders_detail (
order_id int not null,
order_date date not null,
order_item varchar(50) not null,
FOREIGN KEY (order_id, order_date) REFERENCES orders(order_id, order_date)
) PARTITION BY RANGE (order_date);
ERROR: cannot reference partitioned table "orders"
通常來說,分割槽表都是資料量很大的表,不建議建立引用分割槽表的外來鍵。如果有必要,可以建立引用分割槽的外來鍵。
官方文件:Table Partitioning
人生本來短暫,你又何必匆匆!點個贊再走吧!