Good day. There are tables of departments and users . Records from the users table may have a link to records from the departments , but not necessarily, in the definition of the column it is worth that it may be NULL .
It was necessary to establish reference integrity on them, that is, when deleting a record from the departments , if someone in users has a link to it, then an error should be sent. I use this request to install a foreign key:
ALTER TABLE `users` ADD CONSTRAINT `users_ibfk_2` FOREIGN KEY (`department_id`) REFERENCES `departments` (`id`) ON DELETE RESTRICT; And the whole thing is that it does not work for columns that can be NULL . When deleting an entry from the departments , in the users table, the value of the department_id column is stupidly set to NULL . If you disable NULL , then of course everything works and the error will fly out. But it is necessary for me that the column could be NULL . Tell me what to do. The engine, if anything, InnoDB , just read that it only works on it.