ll
- 写在前面
- 正文
- Overview
- When to Use
- FOREIGN_KEY_CHECKS Details
写在前面
刚在import xx.sql文件的时候,使用的是 Navicat Data Model
现在接触的项目是基本不用外键的(foreign key)的,可能单表设计,操作SQL更方便,尤其是JPA的存在,几乎都不用写sql的,但在表的设计使用若关联,即不使用外键约束表,只是在表里加上关联字段,这样当需要联表的时候,可拼接SQL去关联查询。我这里有些例子,关于没有外键约束下的连表实现,只总结了3中方式
- JPQL方式(基于Model的操作)
- 原生SQL的方式
- QueryDsl
后来我试着去用@OneToMany,@MnayToMany等等,做表的外键约束,配置级联关系(可以选择在CRUD操作时的级联操作),JPA下查询是挺方便的,多表查也很方便,是自动关联的,但CUD操作时,就很头疼了,你要关心更多的Many 或者One的映射关系,否则会出现,外键的各种异常,后来在设计表结构时,就很少再做外键映射了…
正文
SET FOREIGN_KEY_CHECKS,是外键检查的,如果设置为零的话,是不是在有外键约束的时候,就不会出现外键的各种异常了??还没试。。。。
怕翻译不准,就没去翻译,自己留着学习用,
FOREIGN_KEY_CHECKS option specifies whether or not to check foreign key constraints for InnoDB tables.
Quick Example:
Overview
MySQL FOREIGN_KEY_CHECKS option:
Syntax SET FOREIGN_KEY_CHECKS = 0 | 1
Default 1 Foreign keys are checked
When Set to 1 Existing data are not re-validated
Get the Current Value SELECT @@FOREIGN_KEY_CHECKS
Last Update: MySQL 5.6
When to Use
Temporarily disabling referential constraints (set FOREIGN_KEY_CHECKS to 0) is useful when you need to re-create the tables and load data in any parent-child order.
Without this option, it may require a lot of effort to define the correct parent-child order especially if you have a lot of tables, and a table can be a parent for some tables, and a child for others.
But as a result, you can insert data that violate foreign key constraints, and when you enable the referential constraints (set FOREIGN_KEY_CHECKS to 1), MySQL does not re-validate the inserted rows.
As an alternative, you can firstly create tables without foreign key constraints, load data and then create foreign keys using ALTER TABLE statements.
FOREIGN_KEY_CHECKS Details
In MySQL InnoDB storage engine, you can use foreign keys to set referential constraints between parent and child tables.
By default, FOREIGN_KEY_CHECKS option is set to 1, and InnoDB does not allow inserting a row that violates a foreign key constraint:
– Try to insert a row to child table (corresponding rows does not exist in the parent table)
INSERT INTO cities VALUES (‘Boston’, ‘MA’);
– ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails
You can disable referential integrity checks, and insert a row that violates FOREIGN KEY constraint:
The Right Way to Create FOREIGN KEY Constraint in MySQL
Many databases (Oracle, Sybase SQL Anywhere i.e) allow a simplified syntax to specify a foreign key constraint:
This syntax is valid in MySQL, but still does not create FOREIGN KEY:
MySQL: