dbTalk Databases Forums  

Constrain duplicate patterns

comp.databases.postgresql.novice comp.databases.postgresql.novice


Discuss Constrain duplicate patterns in the comp.databases.postgresql.novice forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Russell Shaw
 
Posts: n/a

Default Constrain duplicate patterns - 02-01-2004 , 03:06 AM






Hi,

Is there a way to make postgresql report an error if when
inserting data, a multiple-column combination is duplicated?

CREATE TABLE parts_vendors (
part_id integer references parts (part_id),
vendor_id integer references vendors (vendor_id),
...
)

part_id vendor_id
------------------
1 3
2 5 <--+
7 9 |
2 5 <--+-- duplicate pattern
....

---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match


Reply With Quote
  #2  
Old   
Russell Shaw
 
Posts: n/a

Default Re: Constrain duplicate patterns - 02-01-2004 , 03:11 AM






Russell Shaw wrote:
Quote:
Hi,

Is there a way to make postgresql report an error if when
inserting data, a multiple-column combination is duplicated?

CREATE TABLE parts_vendors (
part_id integer references parts (part_id),
vendor_id integer references vendors (vendor_id),
...
)

part_id vendor_id
------------------
1 3
2 5 <--+
7 9 |
2 5 <--+-- duplicate pattern
...

Oops, i can just do a select on the various columns
to detect this...

---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddressHere" to majordomo (AT) postgresql (DOT) org)



Reply With Quote
  #3  
Old   
Michael Glaesemann
 
Posts: n/a

Default Re: Constrain duplicate patterns - 02-01-2004 , 03:21 AM




On Feb 1, 2004, at 6:06 PM, Russell Shaw wrote:

Quote:
Hi,

Is there a way to make postgresql report an error if when
inserting data, a multiple-column combination is duplicated?
I believe you're looking for UNIQUE(part_id, vendor_id), if you want to
prevent this from happening. If you just want it to report an error,
but allow the insert/update anyway, maybe a trigger? Not sure about
that though.

Hope this helps.

Michael Glaesemann
grzm myrealbox com


---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to majordomo (AT) postgresql (DOT) org so that your
message can get through to the mailing list cleanly



Reply With Quote
  #4  
Old   
joseph speigle
 
Posts: n/a

Default Re: Constrain duplicate patterns - 02-01-2004 , 03:40 AM



I found a way to do it like this

create table parts (part_id int, description character varying(33) constraint part_id unique , primary key (part_id));
create table vendors (vendor_id int, description character varying(33) constraint vendor_id unique, primary key (vendor_id) );
CREATE TABLE parts_vendors ( part_id integer unique references parts (part_id), vendor_id integer unique references vendors (vendor_id));

On Sun, Feb 01, 2004 at 08:11:50PM +1100, Russell Shaw wrote:
Quote:
Russell Shaw wrote:
Hi,

Is there a way to make postgresql report an error if when
inserting data, a multiple-column combination is duplicated?

CREATE TABLE parts_vendors (
part_id integer references parts (part_id),
vendor_id integer references vendors (vendor_id),
...
)

part_id vendor_id
------------------
1 3
2 5 <--+
7 9 |
2 5 <--+-- duplicate pattern
...


Oops, i can just do a select on the various columns
to detect this...

---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddressHere" to majordomo (AT) postgresql (DOT) org)
--
joe speigle

---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to majordomo (AT) postgresql (DOT) org



Reply With Quote
  #5  
Old   
Russell Shaw
 
Posts: n/a

Default Re: Constrain duplicate patterns - 02-01-2004 , 03:46 AM



Michael Glaesemann wrote:
Quote:
On Feb 1, 2004, at 6:06 PM, Russell Shaw wrote:

Hi,

Is there a way to make postgresql report an error if when
inserting data, a multiple-column combination is duplicated?

I believe you're looking for UNIQUE(part_id, vendor_id), if you want to
prevent this from happening. If you just want it to report an error, but
allow the insert/update anyway, maybe a trigger? Not sure about that
though.
Hi,
Thanks. I used this:

create table parts_vendors (
part_id integer references parts (part_id),
vendor_id integer references vendors (vendor_id),
unique(part_id,vendor_id)
)

---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to majordomo (AT) postgresql (DOT) org so that your
message can get through to the mailing list cleanly



Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.