Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Data Bases > Pgsql Sql > Re: foreign key...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 3 of 3 Topic 3390 of 3717
Post > Topic >>

Re: foreign key to "compound foreign key table"

by shakahshakah <shakahshakah@[EMAIL PROTECTED] > Apr 7, 2008 at 04:53 AM

On Apr 6, 1:14=A0pm, Lew <l...@[EMAIL PROTECTED]
> wrote:
> shakahsha...@[EMAIL PROTECTED]
 wrote:
> > Is it possible via a FOREIGN KEY constraint to constrain a single
> > column value to a value in another table that's really unique on a
> > compound key?
>
> =A0From the PG SQL docs:
>
> > The referenced columns must be the columns of a unique or primary key
co=
nstraint in the referenced table.
>
> --
> Lew

In this case the referenced columns ("type" and "status") are the
primary key of the referenced table, unless I'm misreading that.

To put it another way, the following works just fine. I'm just
wondering if there's a syntax where I can avoid the explicit
definition of the implied status_type column:

BEGIN ;
CREATE TABLE statuses (
  type     char(1) NOT NULL
 ,status   integer NOT NULL
 ,meaning  varchar(64) NOT NULL
) ;
ALTER TABLE statuses ADD PRIMARY KEY(type,status) ;

INSERT INTO statuses(type,status,meaning) VALUES ('U',0,'created') ;
INSERT INTO statuses(type,status,meaning) VALUES ('G',1,'created') ;
INSERT INTO statuses(type,status,meaning) VALUES ('G',2,'closed') ;

-- ...skeletal tables for this example
CREATE TABLE tmpusers (
  status integer NOT NULL
 ,status_type char(1) NOT NULL -- always set to 'U' to allow FK
constraint?
 ,name   varchar(50) NOT NULL
) ;

-- ...works fine now, but can I avoid the implied
--    status_type with a literal 'U' in the FK constraint DDL ?
ALTER TABLE tmpusers
  ADD CONSTRAINT fk_tmpusers_status
  FOREIGN KEY (status_type,status)
  REFERENCES statuses(type,status) ;
 




 3 Posts in Topic:
foreign key to "compound foreign key table"
"shakahshakah@[EMAIL  2008-04-04 08:30:53 
Re: foreign key to "compound foreign key table"
Lew <lew@[EMAIL PROTEC  2008-04-06 13:14:13 
Re: foreign key to "compound foreign key table"
shakahshakah <shakahsh  2008-04-07 04:53:40 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Mon Oct 13 6:56:36 CDT 2008.