473,395 Members | 1,468 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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

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
Nov 22 '07 #1
6 1406
Fritz Franz (fr**********@hotmail.com) writes:
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, es****@sommarskog.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
Nov 22 '07 #2
"Erland Sommarskog" <es****@sommarskog.sewrote
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.
>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
Nov 22 '07 #3
>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?
>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?
Nov 23 '07 #4
RRR
On Nov 22, 4:41 pm, Erland Sommarskog <esq...@sommarskog.sewrote:
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!"
Nov 23 '07 #5
Fritz Franz wrote:
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.
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.
Nov 23 '07 #6
Fritz Franz (fr**********@hotmail.com) writes:
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, es****@sommarskog.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
Nov 23 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Bodza Bodza | last post by:
I'm having an argument with an incumbent self-taught programmer that it is OK to use null foreign keys in database design. My take is the whole point of a foreign key is that it's not supposed...
0
by: Scott Ribe | last post by:
I've got a problem which I think may be a bug in Postgres, but I wonder if I'm missing something. Two tables, A & B have foreign key relations to each other. A 3rd table C, inherits from A. A...
5
by: Ross A. Finlayson | last post by:
Hi, I'm scratching together an Access database. The development box is Office 95, the deployment box Office 2003. So anyways I am griping about forms and global variables. Say for example...
9
by: Jax | last post by:
I'm making my first ever database for my program. I understand the concept of one to many relationships but fail to see the advantage of using a secondary key over a primary one. I have a lot of...
3
by: Alex Satrapa | last post by:
There's some mention in the (old!) documentation that constraints such as foreign keys won't include data from inheriting tables, eg: CREATE TABLE foo ( id SERIAL PRIMARY KEY ); CREATE TABLE...
2
by: Benjamin Smith | last post by:
I have two tables like following: create table attendancereport ( id serial unique not null, staff_id integer not null references staff(id), schoolyear varchar not null references...
2
by: Ian Davies | last post by:
I have created a database with about 17 tables. I have been creating foreign keys some of which have worked but when creating others I get the message below ************************* 1005...
9
by: sonal | last post by:
Hi all, I hv started with python just recently... and have been assigned to make an utility which would be used for data validations... In short we take up various comma separated data files for...
1
by: Wes Groleau | last post by:
INSERT INTO X ...... ( A, B, C ) INSERT INTO Y ...... ( J, K, L ) If Y has a foreign key M which is the primary key D of X, is there an easy and/or efficient way to have SQL Server assign D,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.