创建外键
postgres=# create table t_p(f1 int not null,f2 int ,primary key(f1));create table t_f(f1 int not null,f2 int );NOTICE: Replica identity is needed for shard table, please add to this table through "alter table" command.CREATE TABLEpostgres=# create table t_f(f1 int not null,f2 int );NOTICE: Replica identity is needed for shard table, please add to this table through "alter table" command.CREATE TABLEpostgres=# ALTER TABLE t_f ADD CONSTRAINT t_f_f1_fkey FOREIGN KEY (f1) REFERENCES t_p (f1);ALTER TABLEpostgres=#
外键只是同一个节点内约束有效果,所以外键字段和对应主键字段必须都是表的分布键,否则由于数据分布于不同的节点内会导致更新失败。
删除外键
postgres=# \\d+ t_fTable "public.t_f"Column | Type | Collation | Nullable | Default | Storage | Stats target | Description--------+---------+-----------+----------+---------+---------+--------------+-------------f1 | integer | | not null | | plain | |f2 | integer | | | | plain | |Foreign-key constraints:"t_f_f1_fkey" FOREIGN KEY (f1) REFERENCES t_p(f1)Distribute By: SHARD(f1)Location Nodes: ALL DATANODESpostgres=# ALTER TABLE t_f drop constraint t_f_f1_fkey;ALTER TABLEpostgres=# \\d+ t_fTable "public.t_f"Column | Type | Collation | Nullable | Default | Storage | Stats target | Description--------+---------+-----------+----------+---------+---------+--------------+-------------f1 | integer | | not null | | plain | |f2 | integer | | | | plain | |Distribute By: SHARD(f1)Location Nodes: ALL DATANODES