Mysql:命令选项、配置选项、(全局、会话)系统变量、状态变量:SQL模式
2009-06-13 10:00
796 查看
sql_mode
为什么会有各种各样的sqlmode呢?不外乎标准与非标准的问题!标准的东西贵在普适,但不是最合适的!各种各样的dbms总会加入自己的理
解,扩展自己的产品功能,这就会引起非标准的问题。所以一个成熟的dbms总会提供sql_mode这一解决方案形式,使dbms系统具有某种行为模式以
便实现 和 标准、其他dbms系统的兼容!
mysql的sql_mode
mysql可以同时运行在各种sqlmode下,尤其是可以在会话级别进行模式设置,提供了最大的应用程序灵活性!
mysql的sqlmode定义了:语法支持规则、执行何种数据有效性验证处理。
mysql的sqlmode可以叠加,例如在strict模式下允许部分其他行为...
mysql的sqlmode有很多预定义的模式!
mysql的sql_mode的设置方式
启动mysqld服务器是指定: --sql-mode="modes"
在选项文件里设置:sql-mode="modes"
运行时设置全局系统变量:set global sql_mode="modes" --需要super权限
运行时设置会话系统变量:set [session] sql_mode="modes"
可以将sqlmode设为空,mysql将使用编译时默认的行为方式
mysql的各种mode
STRICT_ALL_TABLES --对所有的存储引擎应用 严格 模式。行为:拒绝无效数据或缺失数据。对于事务性引擎:如果输入无效数据或位提供数据,语句终止,事务回滚,错误发生。对于非非非事务性引擎:如果是第一行数据就发生数据无效或丢失,语句终止,表数据不改变;如果不是第一行数据,保留之前影响数据,终止语句执行,错误发生!部分啊,数据不完整!
STRICT_TRANS_TABLES --对事务性存储引擎应用 严格 模式。可能的话,对非事物性存储引擎应用 严格 模式!行为:拒绝无效数据或缺失数据。对于事务性引擎:如果输入无效数据或位提供数据,语句终止,事务回滚,错误发生。对于非非非事务性引擎:如果是第一行数据就发生数据无效或丢失,语句终止,表数据不改变;如果不是第一行数据,mysql会以 数据类型的隐式默认值 插入,语句成功执行,有警告!
ALLOW_INVALID_DATES --允许:非法的日期时间、"0000-00-00“日期。否则出错
NO_ZERO_DATE --禁止"0000-00-00“日期。但是你仍然可以通过ignore选项插入0日期
NO_ZERO_IN_DATE --禁止日期中的month、day部分出现”00“。但是你仍然可以通过ignore选项插入0日期
ANSI_QUOTES --双引号引用标识符,而不是表示字符串
ERROR_FOR_DIVISION_BY_ZERO --被0除出错,而不是警告后返回null
HIGH_NOT_PRECEDENCE --NOT具有较高的操作符优先顺序,主要是兼容较低版本的mysql。例如在此模式下:not a and b 依据 (not a) and b解析,而不是较高版本的not (a and b)的解析顺序
IGNORE_SPACE --对于部分内置函数名称作为保留字处理,不能直接当做用户标识符!这样这些函数的括号“(”前面可以有若干空白
——不必紧紧跟随着其函数名称。在“非IGNORE_SPACE”的默认模式下,部分内置函数名称不作为保留字处理,如果其后紧跟“(”就认为是函数,否
则认为是用户的标识符。其他mysql就不应该出现这个使事情复杂化的东西——一律当做保留字处理岂不更简单!
NO_AUTO_CREATE_USER --禁止grant语句在没有指定 非空白 密码的情况下创建目前尚不存在的用户
NO_AUTO_VALUE_ON_ZERO --禁止在auto_increment列上通过赋值“0"自增!该模式主要是解决自增列存储有意义的0列值,或数据导入时可能将有意义的0当做自增值的情况下
NO_BACKSLASH_ESCAPES --禁止字符转义,“\”仅仅作为普通字符使用
NO_DIR_IN_CREATE --在创建table时,忽略inex directory 和 data directory语句
NO_ENGINE_SUBTITUTION --在创建或修改table时,当指定的引擎不可用是,语句出错,禁止mysql使用用默认的引擎。默认情况下,mysql发出警告进行无效引擎的替换
NO_FIELD_OPTIONS、NO_KEY_OPTIONS、NO_TABLE_OPTIONS --控制mysqldump和show语句是否显示额外的信息
NO_UNSIGNED_SUBTRACTION --在含有unsigned 整数 的整数减法(-)运算中,禁止将结果result当做unsigned整数处理。小心这个雷啊!
ONLY_FULL_GROUP_BY --select 列表中的非聚合列 必须是在group by中出现的列!自mysql5.1.11后,还限制having子句中的非聚合列也必须是group by中的列
PAD_CHAR_TO_FULL_LENGTH --保留char列的尾随空格(不trim掉),注意:varchar列总是trim!。该模式自5.1.20加入
PIPES_AS_CONCAT --"||"操作符当做字符串连接符,类似于concat(str,...);而不是逻辑 or!
REAL_AS_FLOAT --将real类型作为float类型;而不是douoble
mysql的预定义模式
ANSI <==> PIPES_AS_CONCAT、ANSI_QUOTE、IGNORE_SPACE、REAL_AS_FLOAT
TRADITIONAL <==> STRICT_TRANS_TABLES、STRICT_ALL_TABLES、NO_ZERO_IN_DATE、NO_ZERO_DATE、ERROR_FOR_DIVISION_BY_ZERO、NO_AUTO_CREATE_USER
MYSQL323 <==> NO_FIELD_OPTIONS、HIGH_NOT_PRECDEDENCE
MYSQL40 <==> NO_FIELD_OPTIONS、HIGH_NOT_PRECDEDENCE
MSSQL <==> PIPES_AS_CONCAT、ANSI_QUOTE、IGNORE_SPACE、NO_KEY_OPTIONS、NO_TABLE_OPTIONS、NO_FIELD_OPTIONS、REAL_AS_FLOAT
DB2 <==> PIPES_AS_CONCAT、ANSI_QUOTE、IGNORE_SPACE、NO_KEY_OPTIONS、NO_TABLE_OPTIONS、NO_FIELD_OPTIONS
MAXDB <==> PIPES_AS_CONCAT、ANSI_QUOTE、IGNORE_SPACE、NO_KEY_OPTIONS、NO_TABLE_OPTIONS、NO_FIELD_OPTIONS、NO_AUTO_CREATE_USER
ORACLE <==> PIPES_AS_CONCAT、ANSI_QUOTE、IGNORE_SPACE、NO_KEY_OPTIONS、NO_TABLE_OPTIONS、NO_FIELD_OPTIONS、NO_AUTO_CREATE_USER
POSTGRESQL<==> PIPES_AS_CONCAT、ANSI_QUOTE、IGNORE_SPACE、NO_KEY_OPTIONS、NO_TABLE_OPTIONS、NO_FIELD_OPTIONS
modes differently for different clients. This capability enables each
application to tailor the server's operating mode to its own requirements.
For answers to some questions that are often asked about server SQL modes in
MySQL, see Section A.3, “MySQL 5.1 FAQ — Server SQL Mode”.
Modes define what SQL syntax MySQL should support and what kind of data
validation checks it should perform. This makes it easier to use MySQL in
different environments and to use MySQL together with other database servers.
You can set the default SQL mode by starting mysqld[/b] with the
by comma (“
The default value is empty (no modes set). The
Unix systems or in
clear it explicitly.
You can change the SQL mode at runtime by using a
statement to set the
and affects the operation of all clients that connect from that time on. Setting
the
Any client can change its own session
after creating and inserting data into partitioned tables can cause major
changes in the behavior of such tables, and could lead to loss or corruption of
data. It is strongly recommended that you never change the SQL mode once you
have created tables employing user-defined partitioning.
See Section 18.5, “Restrictions and Limitations on
Partitioning”, for more information.
You can retrieve the current global or session
with the following statements:
The most important
This mode changes syntax and behavior to conform more closely to standard
SQL.
If a value could not be inserted as given into a transactional table, abort
the statement. For a non-transactional table, abort the statement if the value
occurs in a single-row statement or the first row of a multiple-row statement.
More detail is given later in this section.
Make MySQL behave like a “traditional” SQL database
system. A simple description of this mode is “give an error
instead of a warning” when inserting an incorrect value into a column.
as the error is noticed. This may not be what you want if you are using a
non-transactional storage engine, because data changes made prior to the error
may not be rolled back, resulting in a “partially done”
update.
When this manual refers to “strict mode,” it means a
mode where at least one of
The following list describes all supported modes:
Don't do full checking of dates. Check only that the month is in the range
from 1 to 12 and the day is in the range from 1 to 31. This is very convenient
for Web applications where you obtain year, month, and day in three different
fields and you want to store exactly what the user inserted (without date
validation). This mode applies to
does not apply
which always require a valid date.
The server requires that month and day values be legal, and not merely in the
range 1 to 12 and 1 to 31, respectively. With strict mode disabled, invalid
dates such as
enabled, invalid dates generate an error. To allow such dates, enable
Treat “
identifier quote character (like the “
character. You can still use “
quote literal strings, because it is interpreted as an identifier.
Produce an error in strict mode (otherwise a warning) when a division by zero
(or
occurs during an
returns
of the operation is
The precedence of the
parsed as
higher-precedence behavior can be obtained by enabling the
mysql>
-> 0
mysql>
mysql>
-> 1
[/code]
Allow spaces between a function name and the “
be treated as reserved words. As a result, identifiers that are the same as
function names must be quoted as described in Section 8.2, “Schema Object
Names”. For example, because there is a
function, the use of
following statement causes an error:
ERROR 1064 (42000): You have an error in your SQL syntax
[/code]
The table name should be quoted:
Query OK, 0 rows affected (0.00 sec)
[/code]
The
not to user-defined functions or stored functions. It is always allowable to
have spaces after a UDF or stored function name, regardless of whether
For further discussion of
and Resolution”.
Prevent the
if it would otherwise do so, unless a non-empty password also is specified.
sequence number for the column by inserting either
generates the next sequence number.
This mode can be useful if
table's
if you dump the table with mysqldump[/b] and then reload it, MySQL normally
generates new sequence numbers when it encounters the
the one that was dumped. Enabling
solves this problem. mysqldump[/b] now automatically includes in its
output a statement that enables
Disable the use of the backslash character (“
mode enabled, backslash becomes an ordinary character like any other.
When creating a table, ignore all
and
on slave replication servers.
Control automatic substitution of the default storage engine when a statement
such as
or not compiled in.
Up through MySQL 5.1.11, with
used and a warning occurs if the desired engine is known but disabled or not
compiled in. If the desired engine is invalid (not a known engine name), an
error occurs and the table is not created or altered.
With
table is not created or altered if the desired engine is unavailable for any
reason (whether disabled or invalid).
As of MySQL 5.1.12, storage engines can be pluggable at runtime, so the
distinction between disabled and invalid no longer applies. All unavailable
engines are treated the same way:
With
occurs if the desired engine is unavailable. For
altered.
With
table is not created or altered if the desired engine is unavailable.
Do not print MySQL-specific column options in the output of
Do not print MySQL-specific index options in the output of
Do not print MySQL-specific table options (such as
In integer subtraction operations, do not mark the result as
words, the result of a subtraction is always signed
whenever this mode is in effect, even if one of the operands is
unsigned. For example, compare the type of column
column
mysql>
mysql>
mysql>
+-------+---------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------------------+------+-----+---------+-------+
| c2 | bigint(21) unsigned | | | 0 | |
+-------+---------------------+------+-----+---------+-------+
mysql>
mysql>
mysql>
+-------+------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------+------+-----+---------+-------+
| c2 | bigint(21) | | | 0 | |
+-------+------------+------+-----+---------+-------+
[/code]
Note that this means that
100% usable in all contexts. See Section 11.9, “Cast Functions and Operators”.
mysql>
+-------------------------+
| CAST(0 AS UNSIGNED) - 1 |
+-------------------------+
| 18446744073709551615 |
+-------------------------+
mysql>
mysql>
+-------------------------+
| CAST(0 AS UNSIGNED) - 1 |
+-------------------------+
| -1 |
+-------------------------+
[/code]
In strict mode, don't allow
valid date. You can still insert zero dates with the
accepted but a warning is generated.
In strict mode, do not accept dates where the year part is non-zero but the
month or day part is 0 (for example,
legal but
mode, the date is accepted but a warning is generated.
Do not allow queries for which the
non-aggregated columns that are not named in the
As of MySQL 5.1.11, this mode also restricts references to non-aggregated
columns in the
the
By default, trailing spaces are trimmed from
retrieval. If
occur and retrieved
to their full length. This mode does not apply to
which trailing spaces are retained on retrieval. This mode was added in MySQL
5.1.20.
Query OK, 0 rows affected (0.37 sec)
mysql>
Query OK, 1 row affected (0.01 sec)
mysql>
Query OK, 0 rows affected (0.00 sec)
mysql>
+------+-----------------+
| c1 | CHAR_LENGTH(c1) |
+------+-----------------+
| xy | 2 |
+------+-----------------+
1 row in set (0.00 sec)
mysql>
Query OK, 0 rows affected (0.00 sec)
mysql>
+------------+-----------------+
| c1 | CHAR_LENGTH(c1) |
+------------+-----------------+
| xy | 10 |
+------------+-----------------+
1 row in set (0.00 sec)
[/code]
Treat
a string concatenation operator (same as
rather than as a synonym for
Treat
treats
Enable strict mode for all storage engines. Invalid data values are rejected.
Additional detail follows.
Enable strict mode for transactional storage engines, and when possible for
non-transactional storage engines. Additional details follow.
Strict mode controls how MySQL handles input values that are invalid or
missing. A value can be invalid for several reasons. For example, it might have
the wrong data type for the column, or it might be out of range. A value is
missing when a new row to be inserted does not contain a value for a non-
the value is missing.)
For transactional tables, an error occurs for invalid or missing values in a
statement when either of the
aborted and rolled back.
For non-transactional 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 option is enabled:
For
the rest of the rows. However, in this case, the earlier rows still have been
inserted or updated. This means that you might get a partial update, which might
not be what you want. To avoid this, it is best to use single-row statements
because these can be aborted without changing the table.
For
the closest valid value for the column and insert 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. Implicit defaults are described in Section 10.1.4, “Data Type Default
Values”.
Strict mode disallows invalid date values such as
day parts such as
If you are not using strict mode (that is, neither
values for invalid or missing values and produces warnings. In strict mode, you
can produce this behavior by using
Strict mode does not affect whether foreign key constraints are checked.
Variables”.)
The following special modes are provided as shorthand for combinations of
mode values from the preceding list.
The descriptions include all mode values that are available in the most
recent version of MySQL. For older versions, a combination mode does not include
individual mode values that are not available except in newer versions.
Equivalent to
As of MySQL 5.1.18,
queries where a set function
outer reference
the outer query against which the outer reference has been resolved. This is
such a query:
Here,
it appears in the
Standard SQL requires an error in this situation. If
enabled, the server treats
way that it would interpret
5.1.18.
See Section 1.7.3, “Running MySQL in ANSI Mode”.
Equivalent to
Equivalent to
Equivalent to
Equivalent to
Equivalent to
Equivalent to
Equivalent to
Equivalent to
为什么会有各种各样的sqlmode呢?不外乎标准与非标准的问题!标准的东西贵在普适,但不是最合适的!各种各样的dbms总会加入自己的理
解,扩展自己的产品功能,这就会引起非标准的问题。所以一个成熟的dbms总会提供sql_mode这一解决方案形式,使dbms系统具有某种行为模式以
便实现 和 标准、其他dbms系统的兼容!
mysql的sql_mode
mysql可以同时运行在各种sqlmode下,尤其是可以在会话级别进行模式设置,提供了最大的应用程序灵活性!
mysql的sqlmode定义了:语法支持规则、执行何种数据有效性验证处理。
mysql的sqlmode可以叠加,例如在strict模式下允许部分其他行为...
mysql的sqlmode有很多预定义的模式!
mysql的sql_mode的设置方式
启动mysqld服务器是指定: --sql-mode="modes"
在选项文件里设置:sql-mode="modes"
运行时设置全局系统变量:set global sql_mode="modes" --需要super权限
运行时设置会话系统变量:set [session] sql_mode="modes"
可以将sqlmode设为空,mysql将使用编译时默认的行为方式
mysql的各种mode
STRICT_ALL_TABLES --对所有的存储引擎应用 严格 模式。行为:拒绝无效数据或缺失数据。对于事务性引擎:如果输入无效数据或位提供数据,语句终止,事务回滚,错误发生。对于非非非事务性引擎:如果是第一行数据就发生数据无效或丢失,语句终止,表数据不改变;如果不是第一行数据,保留之前影响数据,终止语句执行,错误发生!部分啊,数据不完整!
STRICT_TRANS_TABLES --对事务性存储引擎应用 严格 模式。可能的话,对非事物性存储引擎应用 严格 模式!行为:拒绝无效数据或缺失数据。对于事务性引擎:如果输入无效数据或位提供数据,语句终止,事务回滚,错误发生。对于非非非事务性引擎:如果是第一行数据就发生数据无效或丢失,语句终止,表数据不改变;如果不是第一行数据,mysql会以 数据类型的隐式默认值 插入,语句成功执行,有警告!
ALLOW_INVALID_DATES --允许:非法的日期时间、"0000-00-00“日期。否则出错
NO_ZERO_DATE --禁止"0000-00-00“日期。但是你仍然可以通过ignore选项插入0日期
NO_ZERO_IN_DATE --禁止日期中的month、day部分出现”00“。但是你仍然可以通过ignore选项插入0日期
ANSI_QUOTES --双引号引用标识符,而不是表示字符串
ERROR_FOR_DIVISION_BY_ZERO --被0除出错,而不是警告后返回null
HIGH_NOT_PRECEDENCE --NOT具有较高的操作符优先顺序,主要是兼容较低版本的mysql。例如在此模式下:not a and b 依据 (not a) and b解析,而不是较高版本的not (a and b)的解析顺序
IGNORE_SPACE --对于部分内置函数名称作为保留字处理,不能直接当做用户标识符!这样这些函数的括号“(”前面可以有若干空白
——不必紧紧跟随着其函数名称。在“非IGNORE_SPACE”的默认模式下,部分内置函数名称不作为保留字处理,如果其后紧跟“(”就认为是函数,否
则认为是用户的标识符。其他mysql就不应该出现这个使事情复杂化的东西——一律当做保留字处理岂不更简单!
NO_AUTO_CREATE_USER --禁止grant语句在没有指定 非空白 密码的情况下创建目前尚不存在的用户
NO_AUTO_VALUE_ON_ZERO --禁止在auto_increment列上通过赋值“0"自增!该模式主要是解决自增列存储有意义的0列值,或数据导入时可能将有意义的0当做自增值的情况下
NO_BACKSLASH_ESCAPES --禁止字符转义,“\”仅仅作为普通字符使用
NO_DIR_IN_CREATE --在创建table时,忽略inex directory 和 data directory语句
NO_ENGINE_SUBTITUTION --在创建或修改table时,当指定的引擎不可用是,语句出错,禁止mysql使用用默认的引擎。默认情况下,mysql发出警告进行无效引擎的替换
NO_FIELD_OPTIONS、NO_KEY_OPTIONS、NO_TABLE_OPTIONS --控制mysqldump和show语句是否显示额外的信息
NO_UNSIGNED_SUBTRACTION --在含有unsigned 整数 的整数减法(-)运算中,禁止将结果result当做unsigned整数处理。小心这个雷啊!
ONLY_FULL_GROUP_BY --select 列表中的非聚合列 必须是在group by中出现的列!自mysql5.1.11后,还限制having子句中的非聚合列也必须是group by中的列
PAD_CHAR_TO_FULL_LENGTH --保留char列的尾随空格(不trim掉),注意:varchar列总是trim!。该模式自5.1.20加入
PIPES_AS_CONCAT --"||"操作符当做字符串连接符,类似于concat(str,...);而不是逻辑 or!
REAL_AS_FLOAT --将real类型作为float类型;而不是douoble
mysql的预定义模式
ANSI <==> PIPES_AS_CONCAT、ANSI_QUOTE、IGNORE_SPACE、REAL_AS_FLOAT
TRADITIONAL <==> STRICT_TRANS_TABLES、STRICT_ALL_TABLES、NO_ZERO_IN_DATE、NO_ZERO_DATE、ERROR_FOR_DIVISION_BY_ZERO、NO_AUTO_CREATE_USER
MYSQL323 <==> NO_FIELD_OPTIONS、HIGH_NOT_PRECDEDENCE
MYSQL40 <==> NO_FIELD_OPTIONS、HIGH_NOT_PRECDEDENCE
MSSQL <==> PIPES_AS_CONCAT、ANSI_QUOTE、IGNORE_SPACE、NO_KEY_OPTIONS、NO_TABLE_OPTIONS、NO_FIELD_OPTIONS、REAL_AS_FLOAT
DB2 <==> PIPES_AS_CONCAT、ANSI_QUOTE、IGNORE_SPACE、NO_KEY_OPTIONS、NO_TABLE_OPTIONS、NO_FIELD_OPTIONS
MAXDB <==> PIPES_AS_CONCAT、ANSI_QUOTE、IGNORE_SPACE、NO_KEY_OPTIONS、NO_TABLE_OPTIONS、NO_FIELD_OPTIONS、NO_AUTO_CREATE_USER
ORACLE <==> PIPES_AS_CONCAT、ANSI_QUOTE、IGNORE_SPACE、NO_KEY_OPTIONS、NO_TABLE_OPTIONS、NO_FIELD_OPTIONS、NO_AUTO_CREATE_USER
POSTGRESQL<==> PIPES_AS_CONCAT、ANSI_QUOTE、IGNORE_SPACE、NO_KEY_OPTIONS、NO_TABLE_OPTIONS、NO_FIELD_OPTIONS
附件:
Server SQL Modes
The MySQL server can operate in different SQL modes, and can apply thesemodes differently for different clients. This capability enables each
application to tailor the server's operating mode to its own requirements.
For answers to some questions that are often asked about server SQL modes in
MySQL, see Section A.3, “MySQL 5.1 FAQ — Server SQL Mode”.
Modes define what SQL syntax MySQL should support and what kind of data
validation checks it should perform. This makes it easier to use MySQL in
different environments and to use MySQL together with other database servers.
You can set the default SQL mode by starting mysqld[/b] with the
--sql-mode="option, or by usingmodes[/i]"
sql-mode="inmodes[/i]"
my.cnf(Unix operating systems) or
my.ini(Windows).
modes[/i] is a list of different modes separated
by comma (“
,”) characters.
The default value is empty (no modes set). The
modes[/i] value also can be empty (
--sql-mode=""on the command line, or
sql-mode=""in
my.cnfon
Unix systems or in
my.inion Windows) if you want to
clear it explicitly.
You can change the SQL mode at runtime by using a
SET [GLOBAL|SESSION] sql_mode='modes[/i]'
statement to set the
sql_modesystem value. Setting the
GLOBALvariable requires the
SUPERprivilege
and affects the operation of all clients that connect from that time on. Setting
the
SESSIONvariable affects only the current client.
Any client can change its own session
sql_modevalue at any time.
Important
SQL mode and user-defined partitioning. Changing the server SQL modeafter creating and inserting data into partitioned tables can cause major
changes in the behavior of such tables, and could lead to loss or corruption of
data. It is strongly recommended that you never change the SQL mode once you
have created tables employing user-defined partitioning.
See Section 18.5, “Restrictions and Limitations on
Partitioning”, for more information.
You can retrieve the current global or session
sql_modevalue
with the following statements:
SELECT @@GLOBAL.sql_mode; SELECT @@SESSION.sql_mode;
The most important
sql_modevalues are probably these:
ANSI
This mode changes syntax and behavior to conform more closely to standard
SQL.
STRICT_TRANS_TABLES
If a value could not be inserted as given into a transactional table, abort
the statement. For a non-transactional table, abort the statement if the value
occurs in a single-row statement or the first row of a multiple-row statement.
More detail is given later in this section.
TRADITIONAL
Make MySQL behave like a “traditional” SQL database
system. A simple description of this mode is “give an error
instead of a warning” when inserting an incorrect value into a column.
Note
TheINSERT/
UPDATEaborts as soon
as the error is noticed. This may not be what you want if you are using a
non-transactional storage engine, because data changes made prior to the error
may not be rolled back, resulting in a “partially done”
update.
When this manual refers to “strict mode,” it means a
mode where at least one of
STRICT_TRANS_TABLESor
STRICT_ALL_TABLESis enabled.
The following list describes all supported modes:
ALLOW_INVALID_DATES
Don't do full checking of dates. Check only that the month is in the range
from 1 to 12 and the day is in the range from 1 to 31. This is very convenient
for Web applications where you obtain year, month, and day in three different
fields and you want to store exactly what the user inserted (without date
validation). This mode applies to
DATEand
DATETIMEcolumns. It
does not apply
TIMESTAMPcolumns,
which always require a valid date.
The server requires that month and day values be legal, and not merely in the
range 1 to 12 and 1 to 31, respectively. With strict mode disabled, invalid
dates such as
'2004-04-31'are converted to
'0000-00-00'and a warning is generated. With strict mode
enabled, invalid dates generate an error. To allow such dates, enable
ALLOW_INVALID_DATES.
ANSI_QUOTES
Treat “
"” as an
identifier quote character (like the “
`” quote character) and not as a string quote
character. You can still use “
`” to quote identifiers with this mode enabled. With
ANSI_QUOTESenabled, you cannot use double quotes to
quote literal strings, because it is interpreted as an identifier.
ERROR_FOR_DIVISION_BY_ZERO
Produce an error in strict mode (otherwise a warning) when a division by zero
(or
MOD(X,0))
occurs during an
INSERTor
UPDATE. If this mode is not enabled, MySQL instead
returns
NULLfor divisions by zero. For
INSERT IGNOREor
UPDATE IGNORE, MySQL generates a warning for divisions by zero, but the result
of the operation is
NULL.
HIGH_NOT_PRECEDENCE
The precedence of the
NOToperator is such that expressions such as
NOT a BETWEEN b AND care parsed as
NOT (a BETWEEN b AND c). In some older versions of MySQL, the expression was
parsed as
(NOT a) BETWEEN b AND c. The old
higher-precedence behavior can be obtained by enabling the
HIGH_NOT_PRECEDENCESQL mode.
mysql> [code]SET sql_mode = '';[/b]
mysql>
SELECT NOT 1 BETWEEN -5 AND 5;[/b]
-> 0
mysql>
SET sql_mode = 'HIGH_NOT_PRECEDENCE';[/b]
mysql>
SELECT NOT 1 BETWEEN -5 AND 5;[/b]
-> 1
[/code]
IGNORE_SPACE
Allow spaces between a function name and the “
(” character. This causes built-in function names to
be treated as reserved words. As a result, identifiers that are the same as
function names must be quoted as described in Section 8.2, “Schema Object
Names”. For example, because there is a
COUNT()
function, the use of
countas a table name in the
following statement causes an error:
mysql> [code]CREATE TABLE count (i INT);[/b]
ERROR 1064 (42000): You have an error in your SQL syntax
[/code]
The table name should be quoted:
mysql> [code]CREATE TABLE `count` (i INT);[/b]
Query OK, 0 rows affected (0.00 sec)
[/code]
The
IGNORE_SPACESQL mode applies to built-in functions,
not to user-defined functions or stored functions. It is always allowable to
have spaces after a UDF or stored function name, regardless of whether
IGNORE_SPACEis enabled.
For further discussion of
IGNORE_SPACE, see Section 8.2.4, “Function Name Parsing
and Resolution”.
NO_AUTO_CREATE_USER
Prevent the
GRANTstatement from automatically creating new users
if it would otherwise do so, unless a non-empty password also is specified.
NO_AUTO_VALUE_ON_ZERO
NO_AUTO_VALUE_ON_ZEROaffects handling of
AUTO_INCREMENTcolumns. Normally, you generate the next
sequence number for the column by inserting either
NULLor
0into it.
NO_AUTO_VALUE_ON_ZEROsuppresses this behavior for
0so that only
NULL
generates the next sequence number.
This mode can be useful if
0has been stored in a
table's
AUTO_INCREMENTcolumn. (Storing
0is not a recommended practice, by the way.) For example,
if you dump the table with mysqldump[/b] and then reload it, MySQL normally
generates new sequence numbers when it encounters the
0values, resulting in a table with contents different from
the one that was dumped. Enabling
NO_AUTO_VALUE_ON_ZERObefore reloading the dump file
solves this problem. mysqldump[/b] now automatically includes in its
output a statement that enables
NO_AUTO_VALUE_ON_ZERO, to avoid this problem.
NO_BACKSLASH_ESCAPES
Disable the use of the backslash character (“
\”) as an escape character within strings. With this
mode enabled, backslash becomes an ordinary character like any other.
NO_DIR_IN_CREATE
When creating a table, ignore all
INDEX DIRECTORY
and
DATA DIRECTORYdirectives. This option is useful
on slave replication servers.
NO_ENGINE_SUBSTITUTION
Control automatic substitution of the default storage engine when a statement
such as
CREATE TABLEor
ALTER TABLEspecifies a storage engine that is disabled
or not compiled in.
Up through MySQL 5.1.11, with
NO_ENGINE_SUBSTITUTIONdisabled, the default engine is
used and a warning occurs if the desired engine is known but disabled or not
compiled in. If the desired engine is invalid (not a known engine name), an
error occurs and the table is not created or altered.
With
NO_ENGINE_SUBSTITUTIONenabled, an error occurs and the
table is not created or altered if the desired engine is unavailable for any
reason (whether disabled or invalid).
As of MySQL 5.1.12, storage engines can be pluggable at runtime, so the
distinction between disabled and invalid no longer applies. All unavailable
engines are treated the same way:
With
NO_ENGINE_SUBSTITUTIONdisabled, for
CREATE TABLEthe default engine is used and a warning
occurs if the desired engine is unavailable. For
ALTER TABLE, a warning occurs and the table is not
altered.
With
NO_ENGINE_SUBSTITUTIONenabled, an error occurs and the
table is not created or altered if the desired engine is unavailable.
NO_FIELD_OPTIONS
Do not print MySQL-specific column options in the output of
SHOW CREATE TABLE. This mode is used by mysqldump[/b] in portability mode.
NO_KEY_OPTIONS
Do not print MySQL-specific index options in the output of
SHOW CREATE TABLE. This mode is used by mysqldump[/b] in portability mode.
NO_TABLE_OPTIONS
Do not print MySQL-specific table options (such as
ENGINE) in the output of
SHOW CREATE TABLE. This mode is used by mysqldump[/b] in portability mode.
NO_UNSIGNED_SUBTRACTION
In integer subtraction operations, do not mark the result as
UNSIGNEDif one of the operands is unsigned. In other
words, the result of a subtraction is always signed
whenever this mode is in effect, even if one of the operands is
unsigned. For example, compare the type of column
c2in table
t1with that of
column
c2in table
t2:
mysql> [code]SET SQL_MODE='';[/b]
mysql>
CREATE TABLE test (c1 BIGINT UNSIGNED NOT NULL);[/b]
mysql>
CREATE TABLE t1 SELECT c1 - 1 AS c2 FROM test;[/b]
mysql>
DESCRIBE t1;[/b]
+-------+---------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------------------+------+-----+---------+-------+
| c2 | bigint(21) unsigned | | | 0 | |
+-------+---------------------+------+-----+---------+-------+
mysql>
SET SQL_MODE='NO_UNSIGNED_SUBTRACTION';[/b]
mysql>
CREATE TABLE t2 SELECT c1 - 1 AS c2 FROM test;[/b]
mysql>
DESCRIBE t2;[/b]
+-------+------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------+------+-----+---------+-------+
| c2 | bigint(21) | | | 0 | |
+-------+------------+------+-----+---------+-------+
[/code]
Note that this means that
BIGINT UNSIGNEDis not
100% usable in all contexts. See Section 11.9, “Cast Functions and Operators”.
mysql> [code]SET SQL_MODE = '';[/b]
mysql>
SELECT CAST(0 AS UNSIGNED) - 1;[/b]
+-------------------------+
| CAST(0 AS UNSIGNED) - 1 |
+-------------------------+
| 18446744073709551615 |
+-------------------------+
mysql>
SET SQL_MODE = 'NO_UNSIGNED_SUBTRACTION';[/b]
mysql>
SELECT CAST(0 AS UNSIGNED) - 1;[/b]
+-------------------------+
| CAST(0 AS UNSIGNED) - 1 |
+-------------------------+
| -1 |
+-------------------------+
[/code]
NO_ZERO_DATE
In strict mode, don't allow
'0000-00-00'as a
valid date. You can still insert zero dates with the
IGNOREoption. When not in strict mode, the date is
accepted but a warning is generated.
NO_ZERO_IN_DATE
In strict mode, do not accept dates where the year part is non-zero but the
month or day part is 0 (for example,
'0000-00-00'is
legal but
'2010-00-01'and
'2010-01-00'are not). If used with the
IGNOREoption, MySQL inserts a
'0000-00-00'date for any such date. When not in strict
mode, the date is accepted but a warning is generated.
ONLY_FULL_GROUP_BY
Do not allow queries for which the
SELECTlist refers to
non-aggregated columns that are not named in the
GROUP BYclause. The following query is invalid with this mode enabled because
addressis not named in the
GROUP BYclause:
SELECT name, address, MAX(age) FROM t GROUP BY name;
As of MySQL 5.1.11, this mode also restricts references to non-aggregated
columns in the
HAVINGclause that are not named in
the
GROUP BYclause.
PAD_CHAR_TO_FULL_LENGTH
By default, trailing spaces are trimmed from
CHARcolumn values on
retrieval. If
PAD_CHAR_TO_FULL_LENGTHis enabled, trimming does not
occur and retrieved
CHARvalues are padded
to their full length. This mode does not apply to
VARCHARcolumns, for
which trailing spaces are retained on retrieval. This mode was added in MySQL
5.1.20.
mysql> [code]CREATE TABLE t1 (c1 CHAR(10));[/b]
Query OK, 0 rows affected (0.37 sec)
mysql>
INSERT INTO t1 (c1) VALUES('xy');[/b]
Query OK, 1 row affected (0.01 sec)
mysql>
SET sql_mode = '';[/b]
Query OK, 0 rows affected (0.00 sec)
mysql>
SELECT c1, CHAR_LENGTH(c1) FROM t1;[/b]
+------+-----------------+
| c1 | CHAR_LENGTH(c1) |
+------+-----------------+
| xy | 2 |
+------+-----------------+
1 row in set (0.00 sec)
mysql>
SET sql_mode = 'PAD_CHAR_TO_FULL_LENGTH';[/b]
Query OK, 0 rows affected (0.00 sec)
mysql>
SELECT c1, CHAR_LENGTH(c1) FROM t1;[/b]
+------------+-----------------+
| c1 | CHAR_LENGTH(c1) |
+------------+-----------------+
| xy | 10 |
+------------+-----------------+
1 row in set (0.00 sec)
[/code]
PIPES_AS_CONCAT
Treat
||as
a string concatenation operator (same as
CONCAT())
rather than as a synonym for
OR.
REAL_AS_FLOAT
Treat
REALas a synonym for
FLOAT. By default, MySQL
treats
REALas a synonym for
DOUBLE.
STRICT_ALL_TABLES
Enable strict mode for all storage engines. Invalid data values are rejected.
Additional detail follows.
STRICT_TRANS_TABLES
Enable strict mode for transactional storage engines, and when possible for
non-transactional storage engines. Additional details follow.
Strict mode controls how MySQL handles input values that are invalid or
missing. A value can be invalid for several reasons. For example, it might have
the wrong data type for the column, or it might be out of range. A value is
missing when a new row to be inserted does not contain a value for a non-
NULLcolumn that has no explicit
DEFAULTclause in its definition. (For a
NULLcolumn,
NULLis inserted if
the value is missing.)
For transactional tables, an error occurs for invalid or missing values in a
statement when either of the
STRICT_ALL_TABLESor
STRICT_TRANS_TABLESmodes are enabled. The statement is
aborted and rolled back.
For non-transactional 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 option is enabled:
For
STRICT_ALL_TABLES, MySQL returns an error and ignores
the rest of the rows. However, in this case, the earlier rows still have been
inserted or updated. This means that you might get a partial update, which might
not be what you want. To avoid this, it is best to use single-row statements
because these 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 insert 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. Implicit defaults are described in Section 10.1.4, “Data Type Default
Values”.
Strict mode disallows invalid date values such as
'2004-04-31'. It does not disallow dates with zero month or
day parts such as
'2004-04-00'or “zero” dates. To disallow these as well, enable the
NO_ZERO_IN_DATEand
NO_ZERO_DATESQL modes in addition to strict mode.
If you are not using strict mode (that is, neither
STRICT_TRANS_TABLESnor
STRICT_ALL_TABLESis enabled), MySQL inserts adjusted
values for invalid or missing values and produces warnings. In strict mode, you
can produce this behavior by using
INSERT IGNOREor
UPDATE IGNORE. See Section 12.5.5.42, “
SHOW WARNINGSSyntax”.
Strict mode does not affect whether foreign key constraints are checked.
foreign_key_checkscan be used for that. (See Section 5.1.4, “Session System
Variables”.)
The following special modes are provided as shorthand for combinations of
mode values from the preceding list.
The descriptions include all mode values that are available in the most
recent version of MySQL. For older versions, a combination mode does not include
individual mode values that are not available except in newer versions.
ANSI
Equivalent to
REAL_AS_FLOAT,
PIPES_AS_CONCAT,
ANSI_QUOTES,
IGNORE_SPACE.
As of MySQL 5.1.18,
ANSImode also causes the server to return an error for
queries where a set function
S[/i] with an
outer reference
S[/i](
outer_ref[/i])[/code] cannot be aggregated in
the outer query against which the outer reference has been resolved. This is
such a query:
SELECT * FROM t1 WHERE t1.a IN (SELECT MAX(t1.b) FROM t2 WHERE ...);
Here,
MAX(t1.b)cannot aggregated in the outer query because
it appears in the
WHEREclause of that query.
Standard SQL requires an error in this situation. If
ANSImode is not
enabled, the server treats
S[/i](
outer_ref[/i])[/code] in such queries the same
way that it would interpret
S[/i](
const[/i])[/code], as was always done prior to
5.1.18.
See Section 1.7.3, “Running MySQL in ANSI Mode”.
DB2
Equivalent to
PIPES_AS_CONCAT,
ANSI_QUOTES,
IGNORE_SPACE,
NO_KEY_OPTIONS,
NO_TABLE_OPTIONS,
NO_FIELD_OPTIONS.
MAXDB
Equivalent to
PIPES_AS_CONCAT,
ANSI_QUOTES,
IGNORE_SPACE,
NO_KEY_OPTIONS,
NO_TABLE_OPTIONS,
NO_FIELD_OPTIONS,
NO_AUTO_CREATE_USER.
MSSQL
Equivalent to
PIPES_AS_CONCAT,
ANSI_QUOTES,
IGNORE_SPACE,
NO_KEY_OPTIONS,
NO_TABLE_OPTIONS,
NO_FIELD_OPTIONS.
MYSQL323
Equivalent to
NO_FIELD_OPTIONS,
HIGH_NOT_PRECEDENCE.
MYSQL40
Equivalent to
NO_FIELD_OPTIONS,
HIGH_NOT_PRECEDENCE.
ORACLE
Equivalent to
PIPES_AS_CONCAT,
ANSI_QUOTES,
IGNORE_SPACE,
NO_KEY_OPTIONS,
NO_TABLE_OPTIONS,
NO_FIELD_OPTIONS,
NO_AUTO_CREATE_USER.
POSTGRESQL
Equivalent to
PIPES_AS_CONCAT,
ANSI_QUOTES,
IGNORE_SPACE,
NO_KEY_OPTIONS,
NO_TABLE_OPTIONS,
NO_FIELD_OPTIONS.
TRADITIONAL
Equivalent to
STRICT_TRANS_TABLES,
STRICT_ALL_TABLES,
NO_ZERO_IN_DATE,
NO_ZERO_DATE,
ERROR_FOR_DIVISION_BY_ZERO,
NO_AUTO_CREATE_USER.
相关文章推荐
- Mysql:命令选项、配置选项、(全局、会话)系统变量、状态变量:状态变量
- Mysql:命令选项、配置选项、(全局、会话)系统变量、状态变量:总揽
- Mysql:命令选项、配置选项、(全局、会话)系统变量、状态变量:和路径、文件名称相关的设置
- Mysql:命令选项、配置选项、(全局、会话)系统变量、状态变量:如何使用系统变量?
- Mysql:命令选项、配置选项、(全局、会话)系统变量、状态变量:命令选项
- Mysql:命令选项、配置选项、(全局、会话)系统变量、状态变量:各种应用程序及选项
- Mysql:命令选项、配置选项、(全局、会话)系统变量、状态变量:系统变量
- Mysql:命令选项、配置选项、(全局、会话)系统变量、状态变量:(仅会话)系统变量
- Mysql:命令选项、配置选项、(全局、会话)系统变量、状态变量:(动态)系统变量
- FIS--关于下载php后的配置(启动fis的调试服务器(注意添加 --no-rewrite 参数),如果报错 没有php-cgi环境,请 安装 它,并把php-cgi命令加到系统的环境变量)
- MySql服务器系统变量和状态变量介绍
- linux CentOS 系统下如何将php和mysql命令加入到环境变量中
- Mysql 系统参数 系统变量 状态变量
- MySQL中的会话变量与全局变量
- linux系统下将php和mysql命令加入到环境变量中的方法
- CISCO全局配置模式下常用命令手册
- HIVE的安装配置、mysql的安装、hive创建表、创建分区、修改表等内容、hive beeline使用、HIVE的四种数据导入方式、使用Java代码执行hive的sql命令
- 系统架构--设计模式之“全局变量”
- MySql服务器系统变量和状态变量介绍
- mysql变量之系统静态变量、状态变量