李艳玲
|
最近用 蝉知 的 CMS 建站比较多,感觉蛮顺手的,但在给客户安装的时候却会出现安装报错,其原因也很简单 查看了一下他们的install.sql文件中,有些时间字段的默认值是0000-00-00 00:00:00 ... 额,我个人习惯将时间值存int类型,这样程序处理上还有查询计算上会方便一些,可能他们为了在看数据库的时候方便一目了然的认出时间吧...不管它的初衷,要改程序不太可能了,以后也不好升级.我还是改我的数据库的兼容模式吧 在MySQL5.5以后,其默认使用的是一种严格模式,也就是说,像禅知用的这种默认值的方式就会报错.SO,我们今天来说一说这个SQL_MODEL的相关的东东 SQL_MODE:通过对其正确的设置可以完成一些约束检查的工作,设置时,可在配置文件my.cnf或my.ini中进行,也可在客户端中进行,并可分别进行全局的设置或当前会话的设置。 查看SQL_MODE设置情况():mysql> SHOW VARIABLES LIKE
'SQL_MODE';
+---------------+----------------------------------------------------------------+
| Variable_name | Value
|
+---------------+----------------------------------------------------------------+
| sql_mode
| STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION | +---------------+----------------------------------------------------------------+
1 row
in set (
0.
03 sec)
或者(全局) mysql> SELECT @@global.SQL_MODE;
+----------------------------------------------------------------+
| @@global.SQL_MODE | +----------------------------------------------------------------+
| STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION | +----------------------------------------------------------------+
1 row
in set (
0.
00 sec)
亦或(当前会话) mysql> SELECT @@session.SQL_MODE;
+----------------------------------------------------------------+
| @@session.SQL_MODE | +----------------------------------------------------------------+
| STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION | +----------------------------------------------------------------+
1 row
in set (
0.
00 sec)
亦或 mysql> SELECT @@SQL_MODE;
+----------------------------------------------------------------+
| @@session.SQL_MODE | +----------------------------------------------------------------+
| STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION | +----------------------------------------------------------------+
1 row
in set (
0.
00 sec)
设置方法很简单 mysql> SET
global sql_mode=
'STRICT_TRANS_TABLES';
Query OK,
0 rows affected (
0.00 sec)
或 mysql> SET sql_mode=
'STRICT_TRANS_TABLES';
Query OK, 0 rows affected (0.00 sec)
介绍一下几种模式
STRICT_TRANS_TALES(严格模式):
STRICT_ALL_TABLES(严格模式):
ALLOW_INVALID_DATES:
ANSI_QUOTES: mysql>
CREATE TABLE
a
(
a
char
(
5
));
Query OK,
0
rows
affected
(
0.26
sec
)
mysql> INSERT INTO a SELECT 'abc';
Query OK,
1
row
affected
(
0.05
sec
)
Records: 1 Duplicates: 0 Warnings: 0
mysql> SET SQL_MODE=
'ANSI_QUOTES';
Query OK,
0
rows
affected
(
0.00
sec
)
mysql> INSERT INTO a SELECT "abc";
ERROR
1054 (
42S22): Unknown column
'abc'
in
'field list' mysql> SELECT @@session.sql_mode;
+--------------------+
| @@session.sql_mode |
+--------------------+
| ANSI_QUOTES |
+--------------------+
1
row
in
set
(
0.00
sec
)
ERROR_FOR_DIVISION_BY_ZERO:
HIGH_NOT_PRECEDENCE:
IGNORE_SPACE: mysql> SELECT NOW();
ERROR
1630 (
42000): FUNCTION test.NOW does
not exist. Check the
'Function Name Parsing and Resolution' section
in the Reference Manual
mysql> SET SQL_MODE=
'IGNORE_SPACE';
Query OK,
0 rows affected (
0.
00 sec)
mysql> SELECT NOW();
+---------------------+
| NOW () | +---------------------+
| 2015-10-08 17:49:35 | +---------------------+
1 row
in set (
0.
00 sec)
NO_AUTO_CREATE_USER:
NO_AUTO_VALUE_ON_ZERO:
O_BACKSLASH_ESCAPES: mysql> SET SQL_MODE=
'';
Query OK,
0 rows affected (
0.
00 sec)
mysql> SELECT
'\\';
+---+
| \ | +---+
| \ | +---+
1 row
in set (
0.
00 sec)
mysql> SET SQL_MODE=
'NO_BACKSLASH_ESCAPES';
Query OK,
0 rows affected (
0.
00 sec)
mysql> SELECT
'\\';
+----+
| \\ | +----+
| \\ | +----+
1 row
in set (
0.
00 sec)
NO_DIR_IN_CREATE:
NO_ENGINE_SUBSTITUTION:
NO_UNSIGNED_SUBSTRACTION:
NO_ZERO_DATE:
NO_ZERO_IN_DATE:
ONLY_FULL_GROUP_BY:
PAD_CHAR_TO_FULL_LENGTH: mysql> CREATE TABLE a ( a char(
10), b varchar(
10));
Query OK,
0 rows affected (
0.
29 sec)
mysql> INSERT INTO a SELECT
'a',
'b';
Query OK,
1 row affected (
0.
03 sec)
Records:
1
Duplicates:
0
Warnings:
0;
mysql> SELECT CHAR_LENGTH(a), CHAR_LENGTH(b) FROM a;
+----------------+----------------+
| CHAR_LENGTH(a) | CHAR_LENGTH(b)
|
+----------------+----------------+
|
1
| 1 | +----------------+----------------+
1 row
in set (
0.
01 sec)
mysql> SET SQL_MODE=
'PAD_CHAR_TO_FULL_LENGTH';
Query OK,
0 rows affected (
0.
00 sec)
mysql> SELECT CHAR_LENGTH(a), CHAR_LENGTH(b) FROM a;
+----------------+----------------+
| CHAR_LENGTH(a) | CHAR_LENGTH(b)
|
+----------------+----------------+
|
10
| 1 | +----------------+----------------+
1 row
in set (
0.
00 sec)
PIPES_AS_CONCAT:
REAL_AS_FL
OA T: 蝉知相关产品的解决办法也就很明了啦~~:先查看本地的模式 mysql> SELECT @@global.SQL_MODE;
+---------------------------------------------------------------------------------------------+
| @@global.SQL_MODE | +---------------------------------------------------------------------------------------------+
| STRICT_TRANS_TABLES,NO_ZERO_DATE,NO_ZERO_IN_DATE,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION | +---------------------------------------------------------------------------------------------+
1 row
in set (
0.
00 sec)
然后去掉NO_ZERO_DATE和NO_ZERO_IN_DATE
SET
global sql_mode=
'STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
当然你也可以在my.cnf中配置
[mysqld]
sql-mode=
"STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"
这样就可以不用每次设置啦 |
2018-02-28 10:58:06
1/1