sql_mode中的 STRICT_TRANS_TABLES和STRICT_ALL_TABLES 區別

mysql的官方說明中提出:

嚴格模式控制MySQL如何處理非法或丟失的輸入值。有幾種原因可以使一個值爲非法。例如,數據類型錯誤,不適合列,或超出範圍。當新插入的行不包含某列的沒有顯示定義DEFAULT子句的值,則該值被丟失。

對於事務表,當啓用STRICT_ALL_TABLESSTRICT_TRANS_TABLES模式時,如果語句中有非法或丟失值,則會出現錯誤。語句被放棄並滾動。

 

對於非事務表,如果插入或更新的第1行出現壞值,兩種模式的行爲相同。語句被放棄,表保持不變。如果語句插入或修改多行,並且壞值出現在第2或後面的行,結果取決於啓用了哪個嚴格選項:

· 對於STRICT_ALL_TABLESMySQL返回錯誤並忽視剩餘的行。但是,在這種情況下,前面的行已經被插入或更新。這說明你可以部分更新,這可能不是你想要的。要避免這點,最好使用單行語句,因爲這樣可以不更改表即可以放棄。

  對於STRICT_TRANS_TABLESMySQL將非法值轉換爲最接近該列的合法值並插入調整後的值。如果值丟失,MySQL在列中插入隱式 默認值。在任何情況下,MySQL都會生成警告而不是給出錯誤並繼續執行語句。

 

For transactional tables, an error occurs for invalid or missing values in a data-change statement
when either STRICT_ALL_TABLES or STRICT_TRANS_TABLES is enabled. The statement is
aborted and rolled back.


For nontransactional tables, the behavior is the same for either mode if the bad value occurs in the
first row to be inserted or updated: The statement is aborted and the table remains unchanged. If the
statement inserts or modifies multiple rows and the bad value occurs in the second or later row, the
result depends on which strict mode is enabled:

For STRICT_ALL_TABLES, MySQL returns an error and ignores the rest of the rows. However,
because the earlier rows have been inserted or updated, the result is a partial update. To avoid
this, use single-row statements, which can be aborted without changing the table.


For STRICT_TRANS_TABLES, MySQL converts an invalid value to the closest valid value for the
column and inserts the adjusted value. If a value is missing, MySQL inserts the implicit default
value for the column data type. In either case, MySQL generates a warning rather than an error
and continues processing the statement.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章