mysql啟動和關閉外鍵約束的方法,mysql關閉鍵約束

來源:互聯網
上載者:User

mysql啟動和關閉外鍵約束的方法,mysql關閉鍵約束
mysql啟動和關閉外鍵約束的方法(FOREIGN_KEY_CHECKS)

在MySQL中刪除一張表或一條資料的時候,出現

[Err] 1451 -Cannot delete or update a parent row: a foreign key constraint fails (...) 

這是因為MySQL中設定了foreign key關聯,造成無法更新或刪除資料。可以通過設定FOREIGN_KEY_CHECKS變數來避免這種情況。

禁用外鍵約束,我們可以使用:
SET FOREIGN_KEY_CHECKS=0;
啟動外鍵約束,我們可以使用:
SET FOREIGN_KEY_CHECKS=1;
查看當前FOREIGN_KEY_CHECKS的值,可用如下命令:
SELECT  @@FOREIGN_KEY_CHECKS; 

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.