Assign Foreign key

T

Tanya

Hi
My understanding about foreign keys is that they are generally a primary key
in foreign table. The primary table primary key would be unique. In fact I
believe all keys should be unique????

The problem i am having is I have two tables

ChemicalsTbl
symbol - Text 8 Bytes long (primary key)
name - Text 25 Bytes

ReadingsTbl
symbol - Text 8 Bytes long (foreign key)
Readdate - Date/Time Default value today
Read1 - Decimal - Precision 10 - scale 3 - Decimal 3
Read2 "
Read3 "
Read4"

Next... After inputing data into both tables I have duplicates in
ReadingsTbl.symbol (foreign key)

Therefore I am unable to ensure referential integrity.

My question is how do I get around this?

I have tinkered with indexing and not sure where else to look.

If anyone could help I would be greatful.

Regards
Tanya
 
A

Allen Browne

Tanya, it sounds like you are trying to set up a one-to-many relationship
between ChemiclesTbl and ReadingsTbl, i.e. one Chemical can have many
Readings.

ReadingsTbl should have its own primary key field - typically an autonumber.
The symbol field cannot be primary key in this table, since it can occur
many times.

To create the relationship betweeen the 2 tables, open the Relationships
window (Tools menu, or Database Tools tab of ribbon if you use A2007.)
Define the relationship by dropping ChemicalsTbl.symbol onto
ReadingsTbl.symbol, and check the box for Referential Integrity (so there
can never be a reading for a symbol that doesn't exist.)

In summary, a foreign key is not a primary key. It is a field in one table
that links to the primary key of another table, but it is not a primary key
in its own table (except in a one-to-one relation, which is rare.)
 
T

Tanya

Thank you Allen


Allen Browne said:
Tanya, it sounds like you are trying to set up a one-to-many relationship
between ChemiclesTbl and ReadingsTbl, i.e. one Chemical can have many
Readings.

ReadingsTbl should have its own primary key field - typically an autonumber.
The symbol field cannot be primary key in this table, since it can occur
many times.

To create the relationship betweeen the 2 tables, open the Relationships
window (Tools menu, or Database Tools tab of ribbon if you use A2007.)
Define the relationship by dropping ChemicalsTbl.symbol onto
ReadingsTbl.symbol, and check the box for Referential Integrity (so there
can never be a reading for a symbol that doesn't exist.)

In summary, a foreign key is not a primary key. It is a field in one table
that links to the primary key of another table, but it is not a primary key
in its own table (except in a one-to-one relation, which is rare.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top