postgresql - 修改postgresql中的UNIQUE

标签 postgresql

我有一张像这样的 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/

相关文章:

linux - 在 linux 中找不到文件,即使 "locate"返回它的位置

database - 如何重新设计这个SQL数据结构?

mysql - 除以零的处理方式不同

ruby-on-rails - Rails 在 list 模型中重新排列订单

postgresql - Docker 群堆栈忽略 postgres 密码环境变量

php - PostgreSQL 插入不工作

postgresql - GeoServer 不会写入我的 PostgreSQL 可更新 View

python - 从本地机器上的 psycopg2 连接到 Docker 上的 PostgreSQL 数据库

sql - 在文本 block 中查找单词/短语中的字符百分比

postgresql - 使用 postgres uuid_generate_v4 时有很多重复值