在 SQL 中,您可以使用 sum 和 product 计算列强制无向边的唯一性吗?
In SQL can you enforce unicity of undirected edge using sum and product computed columns?
create table Location (
id integer primary key(1, 1),
latitude decimal(8,6),
longitude decimal(9,6),
address varchar(100),
name varchar(60) unique
);
create table Journey (
id integer primary key(1,1),
id_from integer foreign key references Location(id),
id_to integer foreign key references Location(id),
s = id_from + id_to persistent,
p = id_from * id_to persistent,
unique(s, p),
name varchar(100) unique,
);
这是为每对位置强制执行单程(进出或返回)的正确方法吗?
没有。如果你想要唯一的边缘而不考虑方向,那么使用最小值和最大值:
create table Journey (
id integer primary key(1,1),
id_from integer foreign key references Location(id),
id_to integer foreign key references Location(id),
id_least as (case when id_from < id_to then id_from else id_to end) persistent,
id_greatest as (case when id_from < id_to then id_to else id_from end) persistent,
unique(least_id, greatest_id),
name varchar(100) unique,
);
create table Location (
id integer primary key(1, 1),
latitude decimal(8,6),
longitude decimal(9,6),
address varchar(100),
name varchar(60) unique
);
create table Journey (
id integer primary key(1,1),
id_from integer foreign key references Location(id),
id_to integer foreign key references Location(id),
s = id_from + id_to persistent,
p = id_from * id_to persistent,
unique(s, p),
name varchar(100) unique,
);
这是为每对位置强制执行单程(进出或返回)的正确方法吗?
没有。如果你想要唯一的边缘而不考虑方向,那么使用最小值和最大值:
create table Journey (
id integer primary key(1,1),
id_from integer foreign key references Location(id),
id_to integer foreign key references Location(id),
id_least as (case when id_from < id_to then id_from else id_to end) persistent,
id_greatest as (case when id_from < id_to then id_to else id_from end) persistent,
unique(least_id, greatest_id),
name varchar(100) unique,
);