[英]Prevent overlapping values on CIDR column in PostgreSQL
是否有約束或其他一些PostgreSQL功能阻止CIDR列具有重疊的值?
例如:
192.168.1.0/24
和192.168.1.1/32
這些不能一起存在,因為192.168.1.1/32
包含在192.168.1.0/24
子網中。
是的,這可以通過排除約束輕松完成。
CREATE TABLE networks (
id integer GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
net cidr NOT NULL
);
ALTER TABLE networks ADD EXCLUDE USING gist (net inet_ops WITH &&);
INSERT INTO networks (net) VALUES ('192.168.1.0/24');
INSERT 0 1
INSERT INTO networks (net) VALUES ('192.168.1.1/32');
ERROR: conflicting key value violates exclusion constraint "networks_net_excl"
DETAIL: Key (net)=(192.168.1.1) conflicts with existing key (net)=(192.168.1.0/24).
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.