在PostgreSQL中仅凭模式和表名知道如何删除主键约束

29
据我所知,PostgreSQL 中唯一删除主键的方法是:

ALTER TABLE schema.tableName DROP CONSTRAINT constraint_name;

默认情况下,约束名为tableName_pkey。然而,有时如果表已重命名,则无法获取原始表名以构建正确的约束名。

例如,对于一个创建为A然后重命名为B的表,约束仍然是A_pkey,但我只有表名B

您知道仅通过知道模式名称和表名称就可以删除pkey约束的正确方法吗?

我正在编写程序来完成此操作,因此我只需要使用SQL查询。像“打开pgAdmin并查看约束名”这样的解决方案将无法使用。

3个回答

55

您可以这样使用目录表中的信息:

创建一个以id为主键的表

create table test1 (id int primary key, name text);

创建SQL以删除键

select concat('alter table public.test1 drop constraint ', constraint_name) as my_query
from information_schema.table_constraints
where table_schema = 'public'
      and table_name = 'test1'
      and constraint_type = 'PRIMARY KEY';

结果将会是:

alter table public.test1 drop constraint test1_pkey

您可以创建一个存储函数来提取此查询,然后执行它。


2
我使用了上述代码,并将concat替换为更加动态的方式... SELECT 'ALTER TABLE ' || table_schema || '.' || table_name || ' DROP CONSTRAINT ' || constraint_name AS my_query - rjchicago

3

使用命令行工具psql登录到数据库。

然后输入以下命令:

\d <table_name>

例如:
\d claim
                                                  Table "public.claim"
             Column             |            Type             | Collation | Nullable |              Default              
--------------------------------+-----------------------------+-----------+----------+-----------------------------------
 id                             | integer                     |           | not null | nextval('claim_id_seq'::regclass)
 policy_id                      | integer                     |           |          | 
 person_id                      | integer                     |           |          | 
 incident_id                    | integer                     |           |          | 
 first_notification_of_loss     | timestamp without time zone |           |          | 
 police_reference               | character varying(40)       |           |          | 
 photos_to_follow               | boolean                     |           |          | 
 sketch_to_follow               | boolean                     |           |          | 
 description_of_weather         | character varying(2000)     |           |          | 
 description_of_property_damage | character varying(2000)     |           |          | 
 created_at                     | timestamp without time zone |           | not null | now()
 updated_at                     | timestamp without time zone |           | not null | 
Indexes:
    "primary_key_claim" PRIMARY KEY, btree (id)
Foreign-key constraints:
    "foreign_key_claim_incident" FOREIGN KEY (incident_id) REFERENCES incident(id)
    "foreign_key_claim_person" FOREIGN KEY (person_id) REFERENCES person(id)
    "foreign_key_claim_policy" FOREIGN KEY (policy_id) REFERENCES policy(id)
Referenced by:
    TABLE "claimant" CONSTRAINT "foreign_key_claimant_claim" FOREIGN KEY (claim_id) REFERENCES claim(id)
    TABLE "damage" CONSTRAINT "foreign_key_damage_claim" FOREIGN KEY (claim_id) REFERENCES claim(id)
    TABLE "witness" CONSTRAINT "foreign_key_witness_claim" FOREIGN KEY (claim_id) REFERENCES claim(id)

这里展示的是主键名称(以及其他信息)。

如果你想以编程方式实现,并且正在使用Java或另一种使用JDBC接口的语言,则可以使用类DatabaseMetaData,方法getPrimaryKeys。

否则,选择从系统目录中选择是正确的方法。


谢谢,但我需要以编程方式完成这个任务。我正在编写一个程序,可以删除用户指定表名的主键。 - ArmanHunanyan
1
换句话说,我需要仅使用 SQL 查询来完成这个任务。 - ArmanHunanyan

1
对于使用PGAdmin的用户: 导航到Database>Schemas>{your schema}>Tables>{your table name},右键单击>属性。 转到约束选项卡并随意添加/删除。
我是使用PGAdmin 4和PostgreSQL 14完成这个操作的。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接