phppup Posted June 1, 2022 Share Posted June 1, 2022 I felt fairly confident of my understanding and code for a second table with a foreign key, yet in testing, when I deleted a record in Table1, it did NOT get deleted in Table2 (which holds the foreign key that references Table1) I even went so far as to replicate the steps from https://www.geeksforgeeks.org/mysql-deleting-rows-when-there-is-a-foreign-key/ but the final steps for After Deleting did not match up ( as I continued to have 4 records in the subordinate table) because the record that should have been removed was still there. I have tried to troubleshoot and ran SHOW GLOBAL VARIABLES LIKE 'foreign_key_checks'; directly in MySQL at phpMyAdmin and received Variable_name Value foreign_key_checks ON as my result. Guidance, please. Quote Link to comment https://forums.phpfreaks.com/topic/314875-foreign-key-not-working/ Share on other sites More sharing options...
Barand Posted June 1, 2022 Share Posted June 1, 2022 Show us the CREATE TABLE code for those two tbles. Quote Link to comment https://forums.phpfreaks.com/topic/314875-foreign-key-not-working/#findComment-1596918 Share on other sites More sharing options...
phppup Posted June 1, 2022 Author Share Posted June 1, 2022 CREATE TABLE student_details ( student_id INT PRIMARY KEY, Student_name varchar(8), student_year varchar(8) ); CREATE TABLE student_exam ( exam_id INT PRIMARY KEY, exam_name varchar(8), student_id INT, FOREIGN KEY(student_id) REFERENCES student_details(student_id) ON DELETE CASCADE ); Quote Link to comment https://forums.phpfreaks.com/topic/314875-foreign-key-not-working/#findComment-1596923 Share on other sites More sharing options...
benanamen Posted June 1, 2022 Share Posted June 1, 2022 (edited) What are you trying to accomplish? What you have will delete all exams for a student when you delete a particular student Edited June 1, 2022 by benanamen Quote Link to comment https://forums.phpfreaks.com/topic/314875-foreign-key-not-working/#findComment-1596924 Share on other sites More sharing options...
phppup Posted June 1, 2022 Author Share Posted June 1, 2022 @benanamen Quote What you have will delete all exams for a student when you delete a particular student That's what I thought. But if you READ my opening entry, you'll understand that when I deleted a particular student, the particular student was deleted from the PRIMARY table ONLY. The child table did NOT get affected at all. And that's a problem. Quote Link to comment https://forums.phpfreaks.com/topic/314875-foreign-key-not-working/#findComment-1596928 Share on other sites More sharing options...
benanamen Posted June 1, 2022 Share Posted June 1, 2022 Your schema worked just fine for me. Are you sure you didn't have spaces at the start or end of your data? Quote Link to comment https://forums.phpfreaks.com/topic/314875-foreign-key-not-working/#findComment-1596929 Share on other sites More sharing options...
Solution Barand Posted June 1, 2022 Solution Share Posted June 1, 2022 Your create table doesn't show whether they are InnoDb or not. They need to be, MyISAM won't work. 1 Quote Link to comment https://forums.phpfreaks.com/topic/314875-foreign-key-not-working/#findComment-1596931 Share on other sites More sharing options...
phppup Posted June 1, 2022 Author Share Posted June 1, 2022 @Barand Ran a new test with ENGINE=INNODB; and had SUCCESS. Thank you. Oddly, after ALTERing the table to INNODB, the problem persisted. But recreating the tables worked Quote Link to comment https://forums.phpfreaks.com/topic/314875-foreign-key-not-working/#findComment-1596937 Share on other sites More sharing options...
gizmola Posted June 1, 2022 Share Posted June 1, 2022 4 minutes ago, phppup said: @Barand Ran a new test with ENGINE=INNODB; and had SUCCESS. Thank you. Oddly, after ALTERing the table to INNODB, the problem persisted. But recreating the tables worked All the tables involved must be InnoDB. InnoDB is now the default engine for MySQL, although in the past myisam was the default engine. Make sure that all your tables are InnoDB, regardless of whether or not they are used in relations. The InnoDB engine has a number of other features that are important (data caching, row level locking, transactions, Clustered Indexes...). 1 Quote Link to comment https://forums.phpfreaks.com/topic/314875-foreign-key-not-working/#findComment-1596939 Share on other sites More sharing options...
phppup Posted June 12, 2022 Author Share Posted June 12, 2022 @gizmola Thanks for the helpful information. Quote Link to comment https://forums.phpfreaks.com/topic/314875-foreign-key-not-working/#findComment-1597215 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.