dbTalk Databases Forums  

Foreign Key with "backup"?

comp.databases.postgresql comp.databases.postgresql


Discuss Foreign Key with "backup"? in the comp.databases.postgresql forum.



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

Default Foreign Key with "backup"? - 07-15-2009 , 10:59 AM






Hi,


I have projects that *may* be split into phases and timereports records
that refer to a project and possibly to a phase.

It's not difficult to enforce this in software, what I'm unsure is if at
all is possible to express it in SQL

The situation is as follows

CREATE TABLE project (
project_code varchar(10) PRIMARY KEY,
...
);
CREATE TABLE phase (
code varchar(10) PRIMARY KEY,
project_code varchar(10) PRIMARY_KEY
...
);
CREATE TABLE timereport (
...
project_code varchar(10) NOT NULL,
phase_code varchar(10) NULL,
...
);
ALTER TABLE timereport ADD CONSTRAINT prj_phase
FOREIGN KEY (project_code, phase_code) REFERENCES phase(project_code, code)


This works *if* there is a project phase in table 'phase'. If there is no
phase, I want to retain the possibility to have a foreign key constraint
that points to the project.

The only solution I have is to create a view 'project_phase' that is an
OUTER JOIN between project and phase and make the foreign key constraint
of the table timereport reference that view.

Is there any other solution that I should be aware of?

thanks in advance
sandro

Reply With Quote
  #2  
Old   
Bob Badour
 
Posts: n/a

Default Re: Foreign Key with "backup"? - 07-15-2009 , 11:18 AM






ortles wrote:

Quote:
Hi,

I have projects that *may* be split into phases and timereports records
that refer to a project and possibly to a phase.

It's not difficult to enforce this in software, what I'm unsure is if at
all is possible to express it in SQL

The situation is as follows

CREATE TABLE project (
project_code varchar(10) PRIMARY KEY,
...
);
CREATE TABLE phase (
code varchar(10) PRIMARY KEY,
project_code varchar(10) PRIMARY_KEY
...
);
CREATE TABLE timereport (
...
project_code varchar(10) NOT NULL,
phase_code varchar(10) NULL,
...
);
ALTER TABLE timereport ADD CONSTRAINT prj_phase
FOREIGN KEY (project_code, phase_code) REFERENCES phase(project_code, code)

This works *if* there is a project phase in table 'phase'. If there is no
phase, I want to retain the possibility to have a foreign key constraint
that points to the project.

The only solution I have is to create a view 'project_phase' that is an
OUTER JOIN between project and phase and make the foreign key constraint
of the table timereport reference that view.

Is there any other solution that I should be aware of?

thanks in advance
sandro
The use of NULL at all is questionable at best. That said, nothing stops
you from declaring a second foreign key reference from timereport to
project.

However, the use of NULL (and in particular the use of NULL in part of a
compound key) is wonky suggesting a poor design from the start.

Reply With Quote
  #3  
Old   
ortles
 
Posts: n/a

Default Re: Foreign Key with "backup"? - 07-15-2009 , 03:41 PM



Bob Badour wrote:

Quote:
ortles wrote:

Hi,

I have projects that *may* be split into phases and timereports records
that refer to a project and possibly to a phase.

It's not difficult to enforce this in software, what I'm unsure is if at
all is possible to express it in SQL

The situation is as follows

CREATE TABLE project (
project_code varchar(10) PRIMARY KEY,
...
);
CREATE TABLE phase (
code varchar(10) PRIMARY KEY,
project_code varchar(10) PRIMARY_KEY
...
);
CREATE TABLE timereport (
...
project_code varchar(10) NOT NULL,
phase_code varchar(10) NULL,
...
);
ALTER TABLE timereport ADD CONSTRAINT prj_phase
FOREIGN KEY (project_code, phase_code) REFERENCES phase(project_code, code)

This works *if* there is a project phase in table 'phase'. If there is no
phase, I want to retain the possibility to have a foreign key constraint
that points to the project.

The only solution I have is to create a view 'project_phase' that is an
OUTER JOIN between project and phase and make the foreign key constraint
of the table timereport reference that view.

Is there any other solution that I should be aware of?

thanks in advance
sandro

The use of NULL at all is questionable at best. That said, nothing stops
you from declaring a second foreign key reference from timereport to
project.
?? I thought that a second foreign key would at best be a second
requirement, so that the first one should be satisfied as well. Am i
wrong?

Quote:
However, the use of NULL (and in particular the use of NULL in part of a
compound key) is wonky suggesting a poor design from the start.
Why that? can you explain why you say so?
As I said a view with an OUTER JOIN allows me to avoid that. I tested it
and just works. So I wouldn't call it poor design but in this moment I
was just wandering if there is a way to avoid a view whose only purpose
is to enforce a foreign key.

It's a fact that not all projects need to be split into phases, and
that if I don't have a phase there's no point in adding a phase field. I
don't think that forcing all projects to declare a default phase would
be a better design.

sandro

Reply With Quote
  #4  
Old   
Bob Badour
 
Posts: n/a

Default Re: Foreign Key with "backup"? - 07-15-2009 , 04:30 PM



ortles wrote:

Quote:
Bob Badour wrote:


ortles wrote:


Hi,

I have projects that *may* be split into phases and timereports records
that refer to a project and possibly to a phase.

It's not difficult to enforce this in software, what I'm unsure is if at
all is possible to express it in SQL

The situation is as follows

CREATE TABLE project (
project_code varchar(10) PRIMARY KEY,
...
);
CREATE TABLE phase (
code varchar(10) PRIMARY KEY,
project_code varchar(10) PRIMARY_KEY
...
);
CREATE TABLE timereport (
...
project_code varchar(10) NOT NULL,
phase_code varchar(10) NULL,
...
);
ALTER TABLE timereport ADD CONSTRAINT prj_phase
FOREIGN KEY (project_code, phase_code) REFERENCES phase(project_code, code)

This works *if* there is a project phase in table 'phase'. If there is no
phase, I want to retain the possibility to have a foreign key constraint
that points to the project.

The only solution I have is to create a view 'project_phase' that is an
OUTER JOIN between project and phase and make the foreign key constraint
of the table timereport reference that view.

Is there any other solution that I should be aware of?

thanks in advance
sandro

The use of NULL at all is questionable at best. That said, nothing stops
you from declaring a second foreign key reference from timereport to
project.

?? I thought that a second foreign key would at best be a second
requirement, so that the first one should be satisfied as well. Am i
wrong?
You have a second requirement: Every time report is specific to a
project even when the timereport is not specific to a phase.


Quote:
However, the use of NULL (and in particular the use of NULL in part of a
compound key) is wonky suggesting a poor design from the start.

Why that? can you explain why you say so?
Entire books have been written on the subject going back 20 or 30 years.
Date's various /Writings.../ books explore the topic in great depth.

Reply With Quote
  #5  
Old   
ortles
 
Posts: n/a

Default Re: Foreign Key with "backup"? - 07-16-2009 , 01:57 AM



Bob Badour wrote:


Quote:
The use of NULL at all is questionable at best. That said, nothing stops
you from declaring a second foreign key reference from timereport to
project.

?? I thought that a second foreign key would at best be a second
requirement, so that the first one should be satisfied as well. Am i
wrong?

You have a second requirement: Every time report is specific to a
project even when the timereport is not specific to a phase.

Exactely. If I add a second constraint *both* will be enforced, while as
you got pretty well some project may not even be present in table phase
as it does not have a phase. Adding a second constraint will not make it
fall back to the second constraint as far as I know. It would just
prevent the project from being accepted as it doesn't appear in table
phase. Unless I have a wrong idea of the role of the second constraint.


Quote:
However, the use of NULL (and in particular the use of NULL in part of a
compound key) is wonky suggesting a poor design from the start.

Why that? can you explain why you say so?

Entire books have been written on the subject going back 20 or 30 years.
Date's various /Writings.../ books explore the topic in great depth.
Strong ideas are normally easier to convey....

sandro

Reply With Quote
  #6  
Old   
Marco Mariani
 
Posts: n/a

Default Re: Foreign Key with "backup"? - 07-16-2009 , 03:20 AM



Bob Badour wrote:


Quote:
However, the use of NULL (and in particular the use of NULL in part
of a compound key) is wonky suggesting a poor design from the start.

Why that? can you explain why you say so?

Entire books have been written on the subject going back 20 or 30 years.
Date's various /Writings.../ books explore the topic in great depth.

I would suggest reading a recent one:

http://www.amazon.com/SQL-Relational...8?tag=xaprb-20

The day we have all the other bells and whistles of tutorial D, I'll be
glad to avoid NULLs completely, but not sooner.

Reply With Quote
  #7  
Old   
Bob Badour
 
Posts: n/a

Default Re: Foreign Key with "backup"? - 07-16-2009 , 08:35 AM



ortles wrote:
Quote:
Bob Badour wrote:

The use of NULL at all is questionable at best. That said, nothing stops
you from declaring a second foreign key reference from timereport to
project.

?? I thought that a second foreign key would at best be a second
requirement, so that the first one should be satisfied as well. Am i
wrong?

You have a second requirement: Every time report is specific to a
project even when the timereport is not specific to a phase.

Exactely. If I add a second constraint *both* will be enforced, while as
you got pretty well some project may not even be present in table phase
as it does not have a phase. Adding a second constraint will not make it
fall back to the second constraint as far as I know. It would just
prevent the project from being accepted as it doesn't appear in table
phase. Unless I have a wrong idea of the role of the second constraint.
I don't know about postgres. Usually, partially missing keys are not
enforced.


Quote:
However, the use of NULL (and in particular the use of NULL in part of a
compound key) is wonky suggesting a poor design from the start.

Why that? can you explain why you say so?

Entire books have been written on the subject going back 20 or 30 years.
Date's various /Writings.../ books explore the topic in great depth.

Strong ideas are normally easier to convey....
NULL is not a strong idea. It was half-baked from the beginning.

Reply With Quote
  #8  
Old   
Bob Badour
 
Posts: n/a

Default Re: Foreign Key with "backup"? - 07-16-2009 , 08:52 AM



Marco Mariani wrote:

Quote:
Bob Badour wrote:

However, the use of NULL (and in particular the use of NULL in part
of a compound key) is wonky suggesting a poor design from the start.

Why that? can you explain why you say so?

Entire books have been written on the subject going back 20 or 30
years. Date's various /Writings.../ books explore the topic in great
depth.

I would suggest reading a recent one:

http://www.amazon.com/SQL-Relational...8?tag=xaprb-20
I have not had the pleasure of reading that one yet. How well does it
cover the problems caused by partially missing keys?

Reply With Quote
  #9  
Old   
Marco Mariani
 
Posts: n/a

Default Re: Foreign Key with "backup"? - 07-16-2009 , 09:03 AM



Bob Badour wrote:

Quote:
I would suggest reading a recent one:

http://www.amazon.com/SQL-Relational...8?tag=xaprb-20


I have not had the pleasure of reading that one yet. How well does it
cover the problems caused by partially missing keys?

Basically, always avoiding NULLs, and providing a not null value
whenever NULLs are coming into existence.

Quote:
Strong recommendations:

* Do specify NOT NULL, explicitly or implicitly, for every column in every base table.
* Don't use the keyword NULL in any other context whatsoever (i.e., anywhere other than a NOT NULL constraint).
[...]
* Don't use NULLIF.
* Don't use outer join, and don't use the keywords OUTER, FULL, LEFT, and RIGHT (except possibly as suggested in the section "A Remark on Outer Join" below).
* Don't use union join.
* Don't specify either PARTIAL or FULL on MATCH (they have meaning only when nulls are present). For similar reasons, don't use the MATCH option on foreign key constraints, and don't use IS DISTINCT FROM. (In the absence of nulls, the expression x IS DISTINCT FROM y is equivalent to the expression x <> y.)
* [...] use COALESCE on every scalar expression that might otherwise
"evaluate to null."

Reply With Quote
  #10  
Old   
Bob Badour
 
Posts: n/a

Default Re: Foreign Key with "backup"? - 07-16-2009 , 09:15 AM



Marco Mariani wrote:

Quote:
Bob Badour wrote:

I would suggest reading a recent one:

http://www.amazon.com/SQL-Relational...8?tag=xaprb-20

I have not had the pleasure of reading that one yet. How well does it
cover the problems caused by partially missing keys?

Basically, always avoiding NULLs, and providing a not null value
whenever NULLs are coming into existence.
I did not mean to ask how Date recommends addressing the problems. I
meant how well the book explains the problems. Date's various
_Writings..._ books explain the myriad problems in great depth. The OP
asked why partially missing keys are wonky. Does this book explain why?

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.