PDA

View Full Version : database foreign keys


jbroder
02-04-2000, 03:55 PM
I am developing a database with multiple tables. I was make
an auto_increment id for each table as the primary key.

I put the primary key from one table into another table as a foreign key, but I haven't been telling mySQL it is a foreign key. Should I go back and fix this so that all foreign keys are explicit?

Until yesterday I didn't know I could make the explicit.

Thanks for any guidance.

Jon

pier
02-05-2000, 05:12 AM
It depends on what you are doing and where you are using them.

In a good, clean, normalised database-design, the foreign keys garantee that data is inserted or deleted in the
right way, i.e. 'master' table needs to exits before making
changed to other tables. In other words: you let the database do the checks on an insert or delete.

In this case, you (probably) want to tell the database explicit that it are foreign keys, to prevent corrupt data.

However, when using 'log-tables', you DON'T want to use foreign key checking, because the master-rows can be removed:
Think of a user with a userid, and you are counting how many times a user visits a site. When a user is removed, you still want to keep the visit-counting, for 'datawarehouse' thingies. Now you really don'tw want an explicit foreign key.

H.T.H.,
[nbsp]Pier