我无法找到为什么这在MySQL 5.7中无效(用5.7.16测试)
CREATE TABLE TEST_TABLE (ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP);
INSERT INTO TEST_TABLE() VALUES ();
DROP TEMPORARY TABLE IF EXISTS TMP_TEST;
CREATE TEMPORARY TABLE TMP_TEST SELECT * FROM (SELECT ts FROM TEST_TABLE) wrap;
我得到了这个:错误代码:1067。'ts'
的默认值无效我错过了什么或是mysql错误吗?
答案 0 :(得分:0)
我无法重现这个问题。
mysql> SELECT VERSION();
+-----------+
| VERSION() |
+-----------+
| 5.7.16 |
+-----------+
1 row in set (0,00 sec)
mysql> CREATE TABLE TEST_TABLE (ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP);
Query OK, 0 rows affected (0,00 sec)
mysql> INSERT INTO TEST_TABLE() VALUES ();
Query OK, 1 row affected (0,00 sec)
mysql> SELECT * FROM (SELECT ts FROM TEST_TABLE) wrap;
+---------------------+
| ts |
+---------------------+
| 2016-11-04 00:00:01 |
+---------------------+
1 row in set (0,00 sec)
mysql> DROP TEMPORARY TABLE IF EXISTS TMP_TEST;
Query OK, 0 rows affected, 1 warning (0,00 sec)
mysql> CREATE TEMPORARY TABLE TMP_TEST;
ERROR 1113 (42000): A table must have at least 1 column
<强>更新强>
更新问题后:
mysql> SELECT ts FROM TEST_TABLE;
Field 1: `ts`
Catalog: `def`
Database: `test`
Table: `TEST_TABLE`
Org_table: `TEST_TABLE`
Type: TIMESTAMP
Collation: binary (63)
Length: 19
Max_length: 19
Decimals: 0
Flags: NOT_NULL BINARY TIMESTAMP
+---------------------+
| ts |
+---------------------+
| 2016-11-04 00:00:01 |
+---------------------+
1 row in set (0,00 sec)
mysql> SELECT * FROM (SELECT ts FROM TEST_TABLE) wrap;
Field 1: `ts`
Catalog: `def`
Database: `test`
Table: `wrap`
Org_table: `*`
Type: TIMESTAMP
Collation: utf8_general_ci (33)
Length: 57
Max_length: 19
Decimals: 0
Flags: NOT_NULL BINARY TIMESTAMP
+---------------------+
| ts |
+---------------------+
| 2016-11-04 00:00:01 |
+---------------------+
1 row in set (0,00 sec)
请注意整理的区别。
mysql> CREATE TEMPORARY TABLE TMP_TEST
-> SELECT * FROM (SELECT BINARY ts FROM TEST_TABLE) wrap;
Query OK, 1 row affected (0,00 sec)
Records: 1 Duplicates: 0 Warnings: 0
更新2
当查询尝试创建临时表时,子查询会将默认值ts
列定义为'0000-00-00 00:00:00'
,并且必须在变量SQL_MODE
中配置服务器{ {1}}值,导致错误。
NO_ZERO_DATE
从mysql> SELECT FIND_IN_SET('NO_ZERO_DATE', @@SQL_MODE);
+-----------------------------------------+
| FIND_IN_SET('NO_ZERO_DATE', @@SQL_MODE) |
+-----------------------------------------+
| 4 |
+-----------------------------------------+
1 row in set (0,00 sec)
删除NO_ZERO_DATE
。
SQL_MODE
您可以通过删除子查询来避免此问题。
mysql> SELECT FIND_IN_SET('NO_ZERO_DATE', @@SQL_MODE);
+-----------------------------------------+
| FIND_IN_SET('NO_ZERO_DATE', @@SQL_MODE) |
+-----------------------------------------+
| 0 |
+-----------------------------------------+
1 row in set (0,00 sec)
mysql> DROP TEMPORARY TABLE IF EXISTS TMP_TEST;
Query OK, 0 rows affected (0,00 sec)
mysql> CREATE TEMPORARY TABLE TMP_TEST
-> SELECT * FROM (SELECT ts FROM TEST_TABLE) wrap;
Query OK, 1 row affected (0,00 sec)
Records: 1 Duplicates: 0 Warnings: 0
mysql> SHOW CREATE TABLE TMP_TEST\G
*************************** 1. row ***************************
Table: TMP_TEST
Create Table: CREATE TEMPORARY TABLE `TMP_TEST` (
`ts` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00'
) ENGINE=InnoDB
1 row in set (0,00 sec)