通过仅知道模式和表名来删除postgresql中的主键约束

问题描述 投票:2回答:2

据我所知,在postgresql中删除主键的唯一方法是

ALTER TABLE schema.tableName DROP CONSTRAINT constraint_name;

默认情况下,约束名称是tableName_pkey,但是有时如果表已经重命名,我就无法获得原始表名来构造右约束名。

例如,表创建为A然后重命名为B约束仍为A_pkey但我只有B.

您是否知道通过仅知道模式名称和表名来删除pkey约束的正确方法?

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

sql postgresql
2个回答
9
投票

您可以使用目录表中的信息,如下所示:

创建一个以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

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


2
投票

使用命令行工具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。

否则,从系统目录中选择的另一个答案是要走的路。

© www.soinside.com 2019 - 2024. All rights reserved.