Edward Posted April 20, 2012 Share Posted April 20, 2012 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 More sharing options...
Joel Posted May 9, 2012 Share Posted May 9, 2012 Not as clean cut as D.R.Y or K.I.S.S but definitely hard to forget now lol Link to comment Share on other sites More sharing options...
HartleySan Posted May 10, 2012 Share Posted May 10, 2012 What's "DRY" short for? Link to comment Share on other sites More sharing options...
margaux Posted May 10, 2012 Share Posted May 10, 2012 don't repeat yourself Link to comment Share on other sites More sharing options...
Edward Posted May 10, 2012 Author Share Posted May 10, 2012 I feel very guilty and sinful about this post, looking back at it now. Come on, surely someone here must have a better way of remember Foreign Key's. Someone please better this... AC someone? Link to comment Share on other sites More sharing options...
lutinFou Posted May 23, 2012 Share Posted May 23, 2012 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 More sharing options...
HartleySan Posted May 23, 2012 Share Posted May 23, 2012 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 More sharing options...
Recommended Posts