pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
How to checking the existance of constraints for a table?
2+ messages / 2 participants
[nested] [flat]

* How to checking the existance of constraints for a table?
@ 2011-02-02 20:40  creationw <mr.creation.register@gmail.com>
  0 siblings, 1 reply; 2+ messages in thread

From: creationw @ 2011-02-02 20:40 UTC (permalink / raw)
  To: pgsql-sql


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-tp3...
Sent from the PostgreSQL - sql mailing list archive at Nabble.com.



^ permalink  raw  reply  [nested|flat] 2+ messages in thread

* Re: How to checking the existance of constraints for a table?
@ 2011-02-04 16:38  bricklen <bricklen@gmail.com>
  parent: creationw <mr.creation.register@gmail.com>
  0 siblings, 0 replies; 2+ messages in thread

From: bricklen @ 2011-02-04 16:38 UTC (permalink / raw)
  To: creationw <mr.creation.register@gmail.com>; +Cc: pgsql-sql

On Wed, Feb 2, 2011 at 12:40 PM, creationw
<mr.creation.register@gmail.com> wrote:
>
> 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"?

You could try checking the information_schema.table_constraints view.
Eg. select * from information_schema.table_constraints where
table_name='myTable'



^ permalink  raw  reply  [nested|flat] 2+ messages in thread


end of thread, other threads:[~2011-02-04 16:38 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2011-02-02 20:40 How to checking the existance of constraints for a table? creationw <mr.creation.register@gmail.com>
2011-02-04 16:38 ` bricklen <bricklen@gmail.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox