简体   繁体   English

防止PostgreSQL中CIDR列上的重叠值

[英]Prevent overlapping values on CIDR column in PostgreSQL

Is there a constraint or some other PostgreSQL feature that prevents CIDR columns from having values that overlap? 是否有约束或其他一些PostgreSQL功能阻止CIDR列具有重叠的值?

For example: 例如:

192.168.1.0/24 and 192.168.1.1/32 192.168.1.0/24192.168.1.1/32

These could not exist together because 192.168.1.1/32 is contained in the 192.168.1.0/24 subnet. 这些不能一起存在,因为192.168.1.1/32包含在192.168.1.0/24子网中。

Yes, that is easily done with an exclusion constraint. 是的,这可以通过排除约束轻松完成。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM