我有一张像这样的 table
CREATE TABLE IF NOT EXISTS link_data (
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
connection_id UUID NOT NULL,
the_link TEXT NOT NULL,
UNIQUE (connection_id, the_link),
FOREIGN KEY (connection_id)
REFERENCES connection(id)
ON DELETE CASCADE
);
我需要修改表,使其仅在connection_id上是唯一的,即UNIQUE (connection_id)
如何更改表来实现此目的?
最佳答案
首先将其放在:
ALTER TABLE link_data
DROP CONSTRAINT constraint_name;
然后:
ALTER TABLE link_data
ADD CONSTRAINT constraint_name UNIQUE (connection_id);
您可以通过以下方式检查表约束:
SELECT con.*
FROM pg_catalog.pg_constraint con
INNER JOIN pg_catalog.pg_class rel
ON rel.oid = con.conrelid
INNER JOIN pg_catalog.pg_namespace nsp
ON nsp.oid = connamespace
WHERE nsp.nspname = '<schema name>'
AND rel.relname = 'link_data ';
关于postgresql - 修改postgresql中的UNIQUE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65006903/