文章目录
对于 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,
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_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
人生本来短暂,你又何必匆匆!点个赞再走吧!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。