How to remove restrictions in postgres?

I have this query in 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 can I write this request in postgres? thanks in advance

+4
source share
1 answer

It seems you want to remove the restriction only if it exists.

In Postgres, you can use:

ALTER TABLE custom_table 
  DROP CONSTRAINT IF EXISTS fk_states_list;

You can also verify that the table exists:

ALTER TABLE IF EXISTS custom_table 
  DROP CONSTRAINT IF EXISTS fk_states_list;
+7
source

Source: https://habr.com/ru/post/1654701/


All Articles