Search code examples
mysqlforeign-keysinnodb

MySQL What happens if we set back SET FOREIGN_KEY_CHECKS=1 in a table with invalid data?


What actually happens when FOREIGN_KEY_CHECKS=0, then we add some invalid rows with, or add a foreign key to a table then set back FOREIGN_KEY_CHECKS=1 while some rows have invalid data?

Will the index just be ignored? Will everything function normally and there will just be no relation?

My guess is that nothing will change but if we try to edit the row and save it, an error will pop.. Is that right? I can't really confirm this looking in the docs.

EDIT:
I want to know precisely if anything below the surface might be broken. The data might appear ok when looking at the table but I'm not sure if something would be missing somewhere. I believe foreign checks are made only when inserts/deletes/updates are executed, but I'm worried something else I don't know about might break while I do these operations.

I'm just asking for advice from people who have more expertise than me for god's sake. I didn't know this would be asking for the moon.


Solution

  • The only way you notice something, is when your data integrity is corrupted, for example when you have children without parents. An insert statement might fail or something. Nothing else happens.