改变 PSQL 中的唯一约束
Alter Unique Constraint in PSQL
我有一个名为地址的 table,它在两个字段上具有唯一约束:(地址和主机名)。我意识到我需要在约束中再添加 1 个字段。我有一行需要插入相同的地址和主机名,但不是 coin_id。尝试插入时我得到了这个。
ERROR: duplicate key value violates unique constraint "address_uniq"
DETAIL: Key (address, hostname)=(GfFrqCJtwuSSJyv6D1STtrT146N8p9cLtd, NHWithdrawal) already exists.
SQL state: 23505
我已尝试通过以下方式查看我的限制条件:
select * from information_schema.table_constraints
where constraint_type = 'UNIQUE'
我在该列表中看不到 address_uniq 约束。
我怎样才能做到以下几点:
- 使用 psql 定位这个约束
- Alter/Update 这个约束,再向它添加 1 列
[How can I] Locate this constraint with psql
如果我们违反主键约束,我们会收到相同的错误消息。
# alter table addresses add constraint addresses_pk primary key (id);
ALTER TABLE
# insert into addresses values (1, 2, 4, 4);
ERROR: duplicate key value violates unique constraint "addresses_pk"
DETAIL: Key (id)=(1) already exists.
#
尝试搜索信息架构 where constraint_type = 'PRIMARY KEY'
。
请注意,我们不必为主键约束指定名称,因为 Postgres 会生成默认值 <table_name>_pkey
。因此,对于您的解决方案,这意味着创建主键的人给了它一个显式名称 address_uniq
,这会造成混淆。
因此更有可能的是您在这些列上有一个唯一索引。索引不显示在信息模式中。您可以这样检查:
select * from pg_indexes where tablename = 'addresses';
[How can I] Alter/Update this constraint, and add 1 more column to it
如果您的问题是索引,请执行以下操作:
# drop index address_uniq;
DROP INDEX
# create unique index address_uniq on addresses (address, hostname, coin_id);
CREATE INDEX
#
如果事实证明这是一个主键约束,则过程类似:
# alter table addresses drop constraint address_uniq;
ALTER TABLE
# alter table addresses add constraint address_uniq primary key (address, hostname,coin_id);
ALTER TABLE
#
你的constraint_type也应该是"PRIMARY KEY"所以最好的定位方式
约束正在使用 constraint_name.
select * from information_schema.table_constraints where constraint_name = 'address_uniq'
您可以删除现有约束
ALTER TABLE your_table_name DROP CONSTRAINT address_uniq;
并添加一个新的:
ALTER TABLE your_table_name ADD CONSTRAINT address_uniq PRIMARY KEY(address, hostname, coin_id);
或
ALTER TABLE your_table_name ADD CONSTRAINT address_uniq UNIQUE(address, hostname, coin_id);
我有一个名为地址的 table,它在两个字段上具有唯一约束:(地址和主机名)。我意识到我需要在约束中再添加 1 个字段。我有一行需要插入相同的地址和主机名,但不是 coin_id。尝试插入时我得到了这个。
ERROR: duplicate key value violates unique constraint "address_uniq"
DETAIL: Key (address, hostname)=(GfFrqCJtwuSSJyv6D1STtrT146N8p9cLtd, NHWithdrawal) already exists.
SQL state: 23505
我已尝试通过以下方式查看我的限制条件:
select * from information_schema.table_constraints
where constraint_type = 'UNIQUE'
我在该列表中看不到 address_uniq 约束。
我怎样才能做到以下几点:
- 使用 psql 定位这个约束
- Alter/Update 这个约束,再向它添加 1 列
[How can I] Locate this constraint with psql
如果我们违反主键约束,我们会收到相同的错误消息。
# alter table addresses add constraint addresses_pk primary key (id);
ALTER TABLE
# insert into addresses values (1, 2, 4, 4);
ERROR: duplicate key value violates unique constraint "addresses_pk"
DETAIL: Key (id)=(1) already exists.
#
尝试搜索信息架构 where constraint_type = 'PRIMARY KEY'
。
请注意,我们不必为主键约束指定名称,因为 Postgres 会生成默认值 <table_name>_pkey
。因此,对于您的解决方案,这意味着创建主键的人给了它一个显式名称 address_uniq
,这会造成混淆。
因此更有可能的是您在这些列上有一个唯一索引。索引不显示在信息模式中。您可以这样检查:
select * from pg_indexes where tablename = 'addresses';
[How can I] Alter/Update this constraint, and add 1 more column to it
如果您的问题是索引,请执行以下操作:
# drop index address_uniq;
DROP INDEX
# create unique index address_uniq on addresses (address, hostname, coin_id);
CREATE INDEX
#
如果事实证明这是一个主键约束,则过程类似:
# alter table addresses drop constraint address_uniq;
ALTER TABLE
# alter table addresses add constraint address_uniq primary key (address, hostname,coin_id);
ALTER TABLE
#
你的constraint_type也应该是"PRIMARY KEY"所以最好的定位方式 约束正在使用 constraint_name.
select * from information_schema.table_constraints where constraint_name = 'address_uniq'
您可以删除现有约束
ALTER TABLE your_table_name DROP CONSTRAINT address_uniq;
并添加一个新的:
ALTER TABLE your_table_name ADD CONSTRAINT address_uniq PRIMARY KEY(address, hostname, coin_id);
或
ALTER TABLE your_table_name ADD CONSTRAINT address_uniq UNIQUE(address, hostname, coin_id);