PostgreSQL 删除check约束的实现

删除check约束:

1.\d table_name 查找约束名称

 \d dm_alarms_fct_20170613 
 "dm_alarms_fct_20170613_dm_transct_date_key_check"

2.alter table .. drop constraint ..

ALTER TABLE "dm_alarms_fct_20170613" Drop constraint "dm_alarms_fct_20170613_dm_transct_date_key_check";

补充:PostgreSQL查询约束和创建删除约束

查询约束constraint

SELECT
   tc.constraint_name, tc.table_name, kcu.column_name, 
   ccu.table_name AS foreign_table_name,
   ccu.column_name AS foreign_column_name,
   tc.is_deferrable,tc.initially_deferred
 FROM 
   information_schema.table_constraints AS tc 
   JOIN information_schema.key_column_usage AS kcu ON tc.constraint_name = kcu.constraint_name
   JOIN information_schema.constraint_column_usage AS ccu ON ccu.constraint_name = tc.constraint_name
 WHERE constraint_type = 'UNIQUE' AND tc.table_name = 'table_name'; 

constraint_type有四种:

UNIQUE、PRIMARY KEY、CHECK、FOREIGN KEY, 通过修改上边sql语句的table_name和constraint_type来进行相应的查询。

添加约束

ALTER TABLE table_name ADD CONSTRAINT uk_users_name1 UNIQUE (NAME);

删除约束

alter table table_name drop constraint if EXISTS uk_users_name1;

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。如有错误或未考虑完全的地方,望不吝赐教。

你可能感兴趣的:(PostgreSQL 删除check约束的实现)