Icon View Thread

The following is the text of the current message along with any replies.
Messages 1 to 2 of 2 total
Thread ADD IF NOT EXISTS
Tue, Dec 17 2013 11:03 AMPermanent Link

Antonio Marques

Hello! I have a question that is probably super easy to answer...

In my company we are developing a new version of our software and we are adding some foreign keys on the database. We made a script to perform all the changes needed. But we are not sure of all the changes so we have a button on the main form that we click to convert the current database by running the script. The problem is, when we run the script twice on the same database (when we add a new key for example) the execution fails because some of the keys are already there. Is there a way to add a key only if that key does not exists yet?

ALTER TABLE myTable ADD IF NOT EXISTS CONSTRAINT "frg_key" FOREIGN KEY ...

If not, how do I check for a foreign key on a script?

Thank you!
Tue, Dec 17 2013 3:46 PMPermanent Link

Uli Becker

Antonio,

> If not, how do I check for a foreign key on a script?

select * from information.constraints
where tablename = 'MyTable'

Uli
Image