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;

以上為個人經驗,希望能給大傢一個參考,也希望大傢多多支持WalkonNet。如有錯誤或未考慮完全的地方,望不吝賜教。

推薦閱讀: