Re: Question about databases and foreign keys
| From: | Philip Thompson | Date: | Mon, 15 Sep 2008 21:17:13 +0000 |
| Subject: | Re: Question about databases and foreign keys | ||
| References: | 1 2 3 4 5 | Groups: | php.db |
| Request: | Send a blank email to php-db+get-45415@lists.php.net to get a copy of this message | ||
On Sep 15, 2008, at 2:12 PM, Micah Gersten wrote:
You'll actually want to have the User Id in the clocking table, not the other way around. User Id is the foreign key because it has a many to one relationship with the time logging. Thank you, Micah Gersten onShore Networks Internal Developer http://www.onshore.comYes, I agree with that. That was just a quick example that I whipped up (and obviously didn't think enough about). ;) So, follow Micah's advice, people. =D ~Philip
Philip Thompson wrote:"innerHTML is a string. The DOM is not a string, it's a hierarchal object structure. Shoving a string into an object is impure and similar to wrapping a spaghetti noodle around an orange and calling it lunch."I'll throw this out there though. When dealing with foreign keys and multiple tables, remember to index appropriately. For example: Table--PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.phpusers: user_id int(10) primary key -- other fields here -- clock_id int(10) Tableclocking: clock_id int(10) primary key clock_in int(10) clock_out int(10) In tableclocking, clock_id is already indexed because it's primary. Be sure to index clock_id inusersso that when you join on the two, you'll have optimal speed! ALTER TABLEusersADD INDEX (clock_id); And an example query... SELECTu.user_id,c.clock_in,c.clock_outFROMusersuINNER JOINclockingcONu.clock_id=c.clock_idWHERE (u.user_id= '$user_id' ANDc.clock_in> '<sometime>' ANDc.clock_out< '<someothertime>'); Something along those lines. I always find it useful to have unique names throughout the database, so if you reference a name, you know where it originated. Because of this, I can just look atusersand determine thatclock_idis a foreign key. Hope that helps a little! ~Philip