dbTalk Databases Forums  

Foreign key to CHECK constrained column

comp.databases.postgresql comp.databases.postgresql


Discuss Foreign key to CHECK constrained column in the comp.databases.postgresql forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Karsten Wutzke
 
Posts: n/a

Default Foreign key to CHECK constrained column - 04-01-2010 , 04:30 PM






Hello,

it's rather a standard SQL or best practices question I have:

Given a column "name" in a table

CREATE TABLE Prepositions
(
name VARCHAR(4) PRIMARY KEY CHECK (name IN ('', 'of', 'to', 'in',
'for', 'on', 'with', 'as', 'by', 'at', 'from'))
)

and using it (as an identifying relationship) from another table by
referencing it via foreign key from another table does not require
repeating the CHECK constraint there, like

CREATE TABLE Actions
(
name VARCHAR(20),
preposition_name VARCHAR(4) CHECK (preposition_name IN ('', 'of',
'to', 'in', 'for', 'on', 'with', 'as', 'by', 'at', 'from')) REFERENCES
Prepositions (name),
PRIMARY KEY (name, preposition_name)
)

right? I haven't seen any code like this, so I suspect this is not
possible and thus not applicable. I'm jmust not 100% sure.

Can anyone confirm this please?

In case: Is the second check not needed because the foreign key
constraint will fail if the check fails on the other table?

Karsten

Reply With Quote
  #2  
Old   
Thomas Kellerer
 
Posts: n/a

Default Re: Foreign key to CHECK constrained column - 04-01-2010 , 05:26 PM






Karsten Wutzke wrote on 01.04.2010 23:30:
Quote:
Hello,

it's rather a standard SQL or best practices question I have:

Given a column "name" in a table

CREATE TABLE Prepositions
(
name VARCHAR(4) PRIMARY KEY CHECK (name IN ('', 'of', 'to', 'in',
'for', 'on', 'with', 'as', 'by', 'at', 'from'))
)

and using it (as an identifying relationship) from another table by
referencing it via foreign key from another table does not require
repeating the CHECK constraint there, like

CREATE TABLE Actions
(
name VARCHAR(20),
preposition_name VARCHAR(4) CHECK (preposition_name IN ('', 'of',
'to', 'in', 'for', 'on', 'with', 'as', 'by', 'at', 'from')) REFERENCES
Prepositions (name),
PRIMARY KEY (name, preposition_name)
)

right? I haven't seen any code like this, so I suspect this is not
possible and thus not applicable. I'm jmust not 100% sure.

Can anyone confirm this please?

In case: Is the second check not needed because the foreign key
constraint will fail if the check fails on the other table?
The second check is not needed by design. actions.preposition_names can only take values that are present in the table prepositions and thus by definition can only have values that are covered by the check constraint.

Thomas

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.