postgresql 如何删除postgres中的约束?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39512411/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 02:23:35 来源:igfitidea点击:
How to drop constraints in postgres?
提问by user880386
I have this query in sql:
我在 sql 中有这个查询:
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id('[FK_states_list]') AND OBJECTPROPERTY(id, 'IsForeignKey') = 1)
ALTER TABLE [custom_table] DROP CONSTRAINT [FK_states_list] ;
How I can write this query in postgres? Thanks in advance
我如何在 postgres 中编写此查询?提前致谢
回答by a_horse_with_no_name
It seems you want to drop the constraint, only if it exists.
似乎您想删除约束,前提是它存在。
In Postgres you can use:
在 Postgres 中,您可以使用:
ALTER TABLE custom_table
DROP CONSTRAINT IF EXISTS fk_states_list;
You can also make sure the table exists:
您还可以确保该表存在:
ALTER TABLE IF EXISTS custom_table
DROP CONSTRAINT IF EXISTS fk_states_list;