The MySQL implementation of foreign key constraints differs from the SQL standard in the following key respects:外键约束的MySQL实现与SQL标准的不同之处在于以下几个关键方面:
If there are several rows in the parent table with the same referenced key value, 如果父表中有几行具有相同的引用键值,InnoDB
performs a foreign key check as if the other parent rows with the same key value do not exist. InnoDB
会执行外键检查,就好像其他具有相同键值的父行不存在一样。For example, if you define a 例如,如果定义了RESTRICT
type constraint, and there is a child row with several parent rows, InnoDB
does not permit the deletion of any of the parent rows.RESTRICT
类型约束,并且有一个子行具有多个父行,则InnoDB
不允许删除任何父行。
If 如果ON UPDATE CASCADE
or ON UPDATE SET NULL
recurses to update the same table it has previously updated during the same cascade, it acts like RESTRICT
. ON UPDATE CASCADE
或ON UPDATE SET NULL
重复出现以更新其先前在同一级联期间更新的同一表,则其行为类似于RESTRICT
。This means that you cannot use self-referential 这意味着您不能使用自引用的ON UPDATE CASCADE
or ON UPDATE SET NULL
operations. ON UPDATE CASCADE
或ON UPDATE SET NULL
操作。This is to prevent infinite loops resulting from cascaded updates. 这是为了防止级联更新导致无限循环。A self-referential 另一方面,自引用ON DELETE SET NULL
, on the other hand, is possible, as is a self-referential ON DELETE CASCADE
. ON DELETE SET NULL
是可能的,自引用的ON DELETE CASCADE
也是可能的。Cascading operations may not be nested more than 15 levels deep.级联操作的嵌套深度不能超过15层。
In an SQL statement that inserts, deletes, or updates many rows, foreign key constraints (like unique constraints) are checked row-by-row. 在插入、删除或更新许多行的SQL语句中,外键约束(如唯一约束)会逐行检查。When performing foreign key checks, 当执行外键检查时,InnoDB
sets shared row-level locks on child or parent records that it must examine. InnoDB
会在它必须检查的子记录或父记录上设置共享行级锁。MySQL checks foreign key constraints immediately; the check is not deferred to transaction commit. MySQL会立即检查外键约束;检查不会延迟到事务提交。According to the SQL standard, the default behavior should be deferred checking. 根据SQL标准,默认行为应该是延迟检查。That is, constraints are only checked after the entire SQL statement has been processed. 也就是说,只有在处理完整个SQL语句之后才检查约束。This means that it is not possible to delete a row that refers to itself using a foreign key.这意味着无法删除使用外键引用自身的行。
No storage engine, including 包括InnoDB
, recognizes or enforces the MATCH
clause used in referential-integrity constraint definitions. InnoDB
在内的任何存储引擎都无法识别或强制执行引用完整性约束定义中使用的MATCH
子句。Use of an explicit 使用显式MATCH
clause does not have the specified effect, and it causes ON DELETE
and ON UPDATE
clauses to be ignored. MATCH
子句不会产生指定的效果,并且会导致忽略ON DELETE
和ON UPDATE
子句。Specifying the 应避免指定MATCH
should be avoided.MATCH
。
The SQL标准中的MATCH
clause in the SQL standard controls how NULL
values in a composite (multiple-column) foreign key are handled when comparing to a primary key in the referenced table. MATCH
子句控制在与引用表中的主键进行比较时如何处理复合(多列)外键中的NULL
值。MySQL essentially implements the semantics defined by MySQL本质上实现了MATCH SIMPLE
, which permits a foreign key to be all or partially NULL
. MATCH SIMPLE
定义的语义,它允许外键全部或部分为NULL
。In that case, a (child table) row containing such a foreign key can be inserted even though it does not match any row in the referenced (parent) table. (It is possible to implement other semantics using triggers.)在这种情况下,可以插入包含此类外键的(子表)行,即使该行与引用的(父)表中的任何行都不匹配。(可以使用触发器实现其他语义。)
MySQL requires that the referenced columns be indexed for performance reasons. However, MySQL does not enforce a requirement that the referenced columns be MySQL出于性能原因要求对引用的列进行索引。但是,MySQL并没有强制要求引用的列是UNIQUE
or be declared NOT NULL
.UNIQUE
或声明为NOT NULL
。
A 引用非FOREIGN KEY
constraint that references a non-UNIQUE
key is not standard SQL but rather an InnoDB
extension. UNIQUE
键的FOREIGN KEY
约束不是标准SQL,而是InnoDB
扩展。The 另一方面,NDB
storage engine, on the other hand, requires an explicit unique key (or primary key) on any column referenced as a foreign key.NDB
存储引擎需要在任何被引用为外键的列上有一个显式的唯一密钥(或主键)。
The handling of foreign key references to nonunique keys or keys that contain 对于NULL
values is not well defined for operations such as UPDATE
or DELETE CASCADE
. UPDATE
或DELETE CASCADE
等操作,没有很好地定义对非唯一键或包含NULL
值的键的外键引用的处理。You are advised to use foreign keys that reference only 建议您使用仅引用UNIQUE
(including PRIMARY
) and NOT NULL
keys.UNIQUE
(包括PRIMARY
)和NOT NULL
键的外键。
MySQL parses but ignores “inline MySQL解析但忽略“内联REFERENCES规范”(如SQL标准中定义的),其中引用被定义为列规范的一部分。REFERENCES
specifications” (as defined in the SQL standard) where the references are defined as part of the column specification. MySQL accepts MySQL仅在作为单独的REFERENCES
clauses only when specified as part of a separate FOREIGN KEY
specification. FOREIGN KEY
规范的一部分指定时才接受REFERENCES
子句。For storage engines that do not support foreign keys (such as 对于不支持外键的存储引擎(如MyISAM
), MySQL Server parses and ignores foreign key specifications.MyISAM
),MySQL Server会解析并忽略外键规范。
For information about foreign key constraints, see Section 13.1.20.5, “FOREIGN KEY Constraints”.有关外键约束的信息,请参阅第13.1.20.5节,“FOREIGN KEY约束”。