dbTalk Databases Forums  

foreign keys - on which kind of keys do the base on?

comp.databases.ms-sqlserver comp.databases.ms-sqlserver


Discuss foreign keys - on which kind of keys do the base on? in the comp.databases.ms-sqlserver forum.



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

Default foreign keys - on which kind of keys do the base on? - 11-22-2007 , 03:44 PM






Hello!

I have a table A with fields id,startdate and other fields. id and startdate
are in the primary key.
In the table B I want to introduce a Foreign key to field id of table A.

Is this possible? If yes, which kind of key I have to build in table A?

Thx in advance,

Fritz



Reply With Quote
  #2  
Old   
Erland Sommarskog
 
Posts: n/a

Default Re: foreign keys - on which kind of keys do the base on? - 11-22-2007 , 04:41 PM






Fritz Franz (fritzfranz24 (AT) hotmail (DOT) com) writes:
Quote:
I have a table A with fields id,startdate and other fields. id and
startdate are in the primary key.
In the table B I want to introduce a Foreign key to field id of table A.

Is this possible? If yes, which kind of key I have to build in table A?
If I understand this correctly, you have this:

CREATE TABLE A(id int NOT NULL,
startdate datetime NOT NULL,
otherfield varchar(89) NULL,
PRIMARY KEY (id, startdate))

Now you want to create a table B with a column id, and you want to add a
check that the values in B.id corresponds to a value of id that also in in
A. You cannot do this with DRI (Declarative Referential Integrity), but
you would have to use a trigger. A foreign key must refer to all columns
of the primary key in the other table; it cannot be a partial key.

I don't know about your data model, but I would hold it as likely that
either you have an incorrect design, or you have a misconception of what
you want to do. I have ran into the situation that I wanted a partial
FK myself, but it's a very rare scenario.


--
Erland Sommarskog, SQL Server MVP, esquel (AT) sommarskog (DOT) se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx


Reply With Quote
  #3  
Old   
Fritz Franz
 
Posts: n/a

Default Re: foreign keys - on which kind of keys do the base on? - 11-22-2007 , 05:52 PM



"Erland Sommarskog" <esquel (AT) sommarskog (DOT) se> wrote

Quote:
If I understand this correctly, you have this:

CREATE TABLE A(id int NOT NULL,
startdate datetime NOT NULL,
otherfield varchar(89) NULL,
PRIMARY KEY (id, startdate))

Now you want to create a table B with a column id, and you want to add a
check that the values in B.id corresponds to a value of id that also in in
A.
Yes, that's all right.

Quote:
You cannot do this with DRI (Declarative Referential Integrity), but
you would have to use a trigger. A foreign key must refer to all columns
of the primary key in the other table; it cannot be a partial key.

I don't know about your data model, but I would hold it as likely that
either you have an incorrect design, or you have a misconception of what
you want to do. I have ran into the situation that I wanted a partial
FK myself, but it's a very rare scenario.
OK, I understand. Yes, you ar right, the design of DB is not normalized
correctly. But for some reasons we decided to do it this way. It is not that
important to have the FK defined in the DB, the applications will ensure the
integrity of the data.

Thanks for your help!

Regards,

Fritz




Reply With Quote
  #4  
Old   
--CELKO--
 
Posts: n/a

Default Re: foreign keys - on which kind of keys do the base on? - 11-23-2007 , 07:24 AM



Quote:
OK, I understand. Yes, you are right, the design of DB is not normalized correctly. But for some reasons we decided to do it this way.
Then go back and do it right. Would you accept an automobile mechanic
telling you that he did not put your tires on right, but as long as
you don't go too fast or turn too sharply, there will not be any
problems?

Quote:
It is not that important to have the FK defined in the DB, the applications will ensure the integrity of the data.
How do you plan to hire only perfect programmers now and in the future
who will never subvert your intended business rules buy always writing
perfect application code? How do you plan on keeping people away from
QA and other tools that go directly to this disaster waiting to
happen?


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

Default Re: foreign keys - on which kind of keys do the base on? - 11-23-2007 , 10:33 AM



On Nov 22, 4:41 pm, Erland Sommarskog <esq... (AT) sommarskog (DOT) se> wrote:
Quote:
I don't know about your data model, but I would hold it as likely that
either you have an incorrect design, or you have a misconception of what
you want to do. I have ran into the situation that I wanted a partial
FK myself, but it's a very rare scenario.
A foreign key should reference the (primary) "key, the whole key and
nothing but the whole key, so help me Codd!"


Reply With Quote
  #6  
Old   
Ed Murphy
 
Posts: n/a

Default Re: foreign keys - on which kind of keys do the base on? - 11-23-2007 , 11:20 AM



Fritz Franz wrote:

Quote:
OK, I understand. Yes, you ar right, the design of DB is not normalized
correctly. But for some reasons we decided to do it this way.
Please post the specific reasons, so that we can specifically
debunk them.

Quote:
It is not that
important to have the FK defined in the DB, the applications will ensure the
integrity of the data.
Celko is right, this will fail as soon as one of the application
programmers makes a mistake. DRI should be enforced directly in
the DB. More complex business logic may be enforced at the
application layer, but you should still look for ways to
centralize it.


Reply With Quote
  #7  
Old   
Erland Sommarskog
 
Posts: n/a

Default Re: foreign keys - on which kind of keys do the base on? - 11-23-2007 , 04:18 PM



Fritz Franz (fritzfranz24 (AT) hotmail (DOT) com) writes:
Quote:
OK, I understand. Yes, you ar right, the design of DB is not normalized
correctly. But for some reasons we decided to do it this way. It is not
that important to have the FK defined in the DB, the applications will
ensure the integrity of the data.
An application cannot ensure data integrity. An application can add its
own checks, and sometimes it has to make messages user-friendly. But it
cannot ensure data integrity, because sooner or later someone will run
an UPDATE/INSERT/DELETE directly from SQL when the application is not
watching.


--
Erland Sommarskog, SQL Server MVP, esquel (AT) sommarskog (DOT) se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx


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.