[SQL] How to checking the existance of constraints for a table?

2011-02-03 Thread creationw

Hello,

I have a sample table describe as follows, anyone knows how to checking the
existence of a constraint?

oviddb=# \d myTable

 Column  |   Type   | Modifiers
-+--+---
 orderid | smallint | not null
 modelid | smallint | not null

Indexes:
"mytable_orderid_key" UNIQUE, btree (orderid)

For example, how to know that myTable has a constraint with name
"mytable_orderid_key"?
I have this problem because when doing migration script, some old table may
have this constraint or may not have. And after migration, we want to add
this constraint:

If I just run the command: 
alter table myTable add Unique (orderid) in the migration script, when the
constraint exits, a new index will be created as
 "mytable_orderid_key1" UNIQUE, btree (orderid)

So, it causes some duplication.
oviddb=# \d myTable

 Column  |   Type   | Modifiers
-+--+---
 orderid | smallint | not null
 modelid | smallint | not null

Indexes:
"mytable_orderid_key" UNIQUE, btree (orderid)
"mytable_orderid_key1" UNIQUE, btree (orderid)


My question becomes how to write sql statement with following semantecs

if constraint unique (orderid) exists
do nothing
else
alter table myTable add Unique (orderid)



Thank you so much!
-- 
View this message in context: 
http://postgresql.1045698.n5.nabble.com/How-to-checking-the-existance-of-constraints-for-a-table-tp3368417p3368417.html
Sent from the PostgreSQL - sql mailing list archive at Nabble.com.

-- 
Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-sql


[SQL] How to workaround DROP CONSTRAINT [ IF EXISTS ] in Postgres version 8.1?

2011-02-03 Thread creationw

Hello,


I found that DROP CONSTRAINT [ IF EXISTS ] is available in Postgres 9.1
http://developer.postgresql.org/pgdocs/postgres/sql-altertable.html
http://developer.postgresql.org/pgdocs/postgres/sql-altertable.html


But it is not avaiable in Postgres 8.1
http://www.postgresql.org/docs/8.1/interactive/sql-altertable.html
http://www.postgresql.org/docs/8.1/interactive/sql-altertable.html


Is there a way to achieve this on Postgres 8.1?

Thanks a lot
-- 
View this message in context: 
http://postgresql.1045698.n5.nabble.com/How-to-workaround-DROP-CONSTRAINT-IF-EXISTS-in-Postgres-version-8-1-tp3368570p3368570.html
Sent from the PostgreSQL - sql mailing list archive at Nabble.com.

-- 
Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-sql