MySql 中文参考指南
1.7.3 How MySQL Deals with Constraints
MySQL 允许您使用允许回滚的事务表和不允许回滚的非事务表。因此,MySQL 中的约束处理与其他 DBMS 中的约束处理略有不同。当您在非事务表中插入或更新大量行时,我们必须处理这种情况,在出现错误时无法回滚这些更改。
MySQL enables you to work both with transactional tables that permit rollback and with nontransactional tables that do not. Because of this, constraint handling is a bit different in MySQL than in other DBMSs. We must handle the case when you have inserted or updated a lot of rows in a nontransactional table for which changes cannot be rolled back when an error occurs.
基本原则是,MySQL Server 会尝试对任何在解析要执行的语句时可以检测到的内容产生一个错误,并尝试从执行语句时发生的任何错误中恢复。在大多数情况下,我们都会这样做,但尚未对所有情况都这样做。
The basic philosophy is that MySQL Server tries to produce an error for anything that it can detect while parsing a statement to be executed, and tries to recover from any errors that occur while executing the statement. We do this in most cases, but not yet for all.
在错误发生时,MySQL 有的选项是停止中间语句或可能从问题恢复并继续。默认情况下,服务器会遵循后者路线。例如,这意味着服务器可能会将无效值强制转换为最近的有效值。
The options MySQL has when an error occurs are to stop the statement in the middle or to recover as well as possible from the problem and continue. By default, the server follows the latter course. This means, for example, that the server may coerce invalid values to the closest valid values.
提供了几种 SQL 模式选项,以更好地控制处理错误数据值,以及当发生错误时是继续语句执行还是中止。使用这些选项,您可以将 MySQL Server 配置为以更传统的方式运行,就像拒绝不当输入的其他 DBMS 一样。可以在服务器启动时全局设置 SQL 模式,以影响所有客户端。各个客户端可以在运行时设置 SQL 模式,这样每个客户端都可以选择最适合其要求的行为。请参阅 Section 7.1.11, “Server SQL Modes”。
Several SQL mode options are available to provide greater control over handling of bad data values and whether to continue statement execution or abort when errors occur. Using these options, you can configure MySQL Server to act in a more traditional fashion that is like other DBMSs that reject improper input. The SQL mode can be set globally at server startup to affect all clients. Individual clients can set the SQL mode at runtime, which enables each client to select the behavior most appropriate for its requirements. See Section 7.1.11, “Server SQL Modes”.
以下部分描述了 MySQL Server 如何处理不同类型的约束。
The following sections describe how MySQL Server handles different types of constraints.