54

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 query in Postgres?

0

1 Answer 1

106

It seems you want to drop the constraint, only if it exists.

In Postgres you can use:

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;
Sign up to request clarification or add additional context in comments.

1 Comment

This is why PostgreSQL is the best. You cannot do IF EXISTS in MySQL. What a sad thing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.