witchy478 Posted May 28, 2012 Share Posted May 28, 2012 Hi I don't know if this is in the right place but I hope you can help. I would like to know how to make foreign keys. I have looked on the internet but it's not making any sense to me. Some say that you can't make foreign keys and then others give you a way to make foreign keys but I don't understand them. Can someone please tell me in a simple way how to do foreign keys. I'm using phpmyadmin 3.4.10.1 Thank you Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted May 29, 2012 Share Posted May 29, 2012 Only the InnoDB supports "Real" Foreign Keys: http://dev.mysql.com/doc/refman/5.5/en/innodb-foreign-key-constraints.html But you can have Foreign Keys in any other engine, but they won't be constant. With InnoDB, if you have a Primary Key and 10 tables that have a Foreign Key that points to the parent tables Primary Key, you can then update the primary key and all your foreign keys update along with it. In any other table you would have to update all the foreign keys manually. That is as much as I have read/heard about Foreign Keys. Quote Link to comment Share on other sites More sharing options...
scootstah Posted May 29, 2012 Share Posted May 29, 2012 Go to the "Structure" tab of a table and then click "Relation View" underneath the column definitions. Quote Link to comment Share on other sites More sharing options...
witchy478 Posted May 30, 2012 Author Share Posted May 30, 2012 Thanks for replying I tried to do the foreign keys and I get this error: ERROR: Cannot add or update a child row: a foreign key constraint fails (`test`.`friend_emails`, CONSTRAINT `friend_emails_ibfk_3` FOREIGN KEY (`email_id`) REFERENCES `emails` (`id`) ON DELETE CASCADE ON UPDATE CASCADE) Quote Link to comment Share on other sites More sharing options...
scootstah Posted May 30, 2012 Share Posted May 30, 2012 The parent row has to exist first. Quote Link to comment Share on other sites More sharing options...
witchy478 Posted June 1, 2012 Author Share Posted June 1, 2012 In my emails table i have id as the primary key and in my friend_emails I have email_id to use as my foreign key. Quote Link to comment Share on other sites More sharing options...
scootstah Posted June 1, 2012 Share Posted June 1, 2012 A row has to exist in the emails table before you can create a row in the friend_emails table. Quote Link to comment Share on other sites More sharing options...
sosdios Posted June 7, 2012 Share Posted June 7, 2012 If you want to read a simple explanation of foreign keys and how to use them appropriately, here's a pretty simple explanation: http://eliacom.com/wpForeignKeys.php. Once you've read through that, let me know if you're still have issues and we can work it out from there. Quote Link to comment Share on other sites More sharing options...
witchy478 Posted June 18, 2012 Author Share Posted June 18, 2012 I get an error saying: ERROR: Cannot add or update a child row: a foreign key constraint fails ('test"."friend_emails', CONSTRAINT 'friend_emails_ibfk_1' FOREIGN KEY('email_id') REFERENCES 'emails' ('email_id) ON UPDATE CASCADE) In my emails table I have email_id, name, email, password. My email_id is a primary key, it's type int(11), attributes unsigned. In my friend_emails table I have id, email_id, friend1, friend2, friend3. My id is the primary key, attributes unsigned. My email_id is index, attributes unsigned. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.