Does MySQL foreign_key_checks affect the entire database?

后端 未结 6 1188
南方客
南方客 2020-11-30 18:27

When I execute this command in MySQL:

SET FOREIGN_KEY_CHECKS=0;

Does it affect the whole engine or it is only my current transaction?

6条回答
  •  感动是毒
    2020-11-30 19:07

    As explained by Ron, there are two variables, local and global. The local variable is always used, and is the same as global upon connection.

    SET FOREIGN_KEY_CHECKS=0;
    SET GLOBAL FOREIGN_KEY_CHECKS=0;
    
    SHOW Variables WHERE Variable_name='foreign_key_checks'; # always shows local variable
    

    When setting the GLOBAL variable, the local one isn't changed for any existing connections. You need to reconnect or set the local variable too.

    Perhaps unintuitive, MYSQL does not enforce foreign keys when FOREIGN_KEY_CHECKS are re-enabled. This makes it possible to create an inconsistent database even though foreign keys and checks are on.

    If you want your foreign keys to be completely consistent, you need to add the keys while checking is on.

提交回复
热议问题