Jump to content
Larry Ullman's Book Forums

Recommended Posts

I hope this will not offend anyone i should of probably kept this till Halloween but i have a nice way to remember Foreign Keys. Well the fact is with foreign keys is that if there are constraints say with say a user having a Bank Account (parent) and possibly having one or two loans (children). If you try to delete the users Bank Account (parent) first without deleting the loans (children) first you will always get an error due to the constraint. So the way to remember is "You must kill the children first before you kill their parents", this may sound horrible and its definitely not something i would do in the real life, but i thought it was cool way to remember this stuff.

 

FOREIGN KEYS - "You must always remember to kill the Children first before you kill their Parents"

"HeHeHe"

Link to comment
Share on other sites

  • 3 weeks later...
  • 2 weeks later...

Hi,

 

whilts on the subject of foreign keys:I am currently working through the review and persue section of chapter 10, creating delete and edit scripts for the banking database. This database has foreign key constraints (ON DELETE NO ACTON). The database is made up of at least 2 tables (an accounts table [child] and the customers table [parent])

 

To delete a customer I first have to delete the child [account] that is clear. Question is, when writing my PHP script do I have to do this using 2 separate queries (i.e. DELETE FROM accounts WHERE customer_id=1 ...run that and then the same for the parent table) or is there a way to run this using just 1 query.

 

Thanks

LF

Link to comment
Share on other sites

You would likely want to use a foreign key constraint, which would automatically enforce the necessary rules. Also, by programming a restraint one time on the DB side, no additional queries would be required, which means that you'd only need one query each time.

 

Please see the following site for an intro to foreign key constraints:

http://www.w3schools.com/sql/sql_foreignkey.asp

Link to comment
Share on other sites

 Share

×
×
  • Create New...