简体   繁体   中英

Create temporary table from select with date time cause invalid default value error

I have a table with a field ts which is a datetime and default value current_timestamp.

If I try to create a temporary table from this table via

CREATE TEMPORARY TABLE tblTmp (
    tmpid INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY,
    INDEX(tmpid)
) AS
    SELECT * FROM myTable;

I get error because of invalid default value of column ts.

I know this is related to the strict mode in mysql 5.7.

Is there any way do deal with this because I quite not understand why this error occurs with curr_timestamp as default value.

UPDATE: Just found out that the problem is using NOW() or CURRENT_TIMESTAMP with the select. I remove the line and it works.

So:

SELECT col1, col2, ... FROM myTable 

is working

SELECT col1, col2, NOW() as currDatetime FROM myTable

is not working.

Any idea why this happens?

If you select the fields you want you may be able to circumvent the issue or code for it in the select. for example given

CREATE TABLE `users` (
  `id` int(11) NOT NULL,
  `userName` varchar(60) NOT NULL,
  `photo` varchar(50) NOT NULL,
  `status` int(11) NOT NULL,
  `ts` datetime DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=latin1

containing

MariaDB [sandbox]> select * from users;
+-----+----------+--------------+--------+---------------------+
| id  | userName | photo        | status | ts                  |
+-----+----------+--------------+--------+---------------------+
|   1 | John     | john.png     |      1 | 2016-12-08 13:14:24 |
|   2 | Jane     | jane.png     |      1 | 2016-12-08 13:14:24 |
|   3 | Ali      |              |      1 | 2016-12-08 13:14:24 |
|   6 | Bruce    | bruce.png    |      1 | 2016-12-08 13:14:24 |
|   7 | Martha   |              |      1 | 2016-12-08 13:14:24 |
|   8 | Sidney   |              |      1 | 2016-12-08 13:14:24 |
|  10 | Charlie  | charlie.png  |      1 | 2016-12-08 13:14:24 |
|  12 | Elisa    |              |      1 | 2016-12-08 13:14:24 |
|  14 | Samantha | samantha.png |      1 | 2016-12-08 13:14:24 |
|  15 | Hannah   | hannah.png   |      1 | 2016-12-08 13:14:24 |
|  16 | Hannah   |              |      1 | 2016-12-08 13:14:24 |
|  17 | Kevin    | kevin1.png   |      1 | 2016-12-08 13:14:24 |
|  18 | Kevin    | kevin2.png   |      1 | 2016-12-08 13:14:24 |
|  19 | Ruth     |              |      1 | 2016-12-08 13:14:24 |
| 999 | xxx      | photo        |      1 | 2016-12-08 13:16:41 |
+-----+----------+--------------+--------+---------------------+
15 rows in set (0.00 sec)

MariaDB [sandbox]> drop  table if exists tbltmp;
Query OK, 0 rows affected, 1 warning (0.00 sec)

MariaDB [sandbox]>
MariaDB [sandbox]> CREATE TEMPORARY TABLE tblTmp (
    ->     tmpid INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY,
    ->     INDEX(tmpid)
    -> ) AS
    ->     SELECT id,ts FROM users;
Query OK, 15 rows affected (0.19 sec)
Records: 15  Duplicates: 0  Warnings: 0

Results in

MariaDB [sandbox]>
MariaDB [sandbox]> select * from tbltmp;
+-------+-----+---------------------+
| tmpid | id  | ts                  |
+-------+-----+---------------------+
|     1 |   1 | 2016-12-08 13:14:24 |
|     2 |   2 | 2016-12-08 13:14:24 |
|     3 |   3 | 2016-12-08 13:14:24 |
|     4 |   6 | 2016-12-08 13:14:24 |
|     5 |   7 | 2016-12-08 13:14:24 |
|     6 |   8 | 2016-12-08 13:14:24 |
|     7 |  10 | 2016-12-08 13:14:24 |
|     8 |  12 | 2016-12-08 13:14:24 |
|     9 |  14 | 2016-12-08 13:14:24 |
|    10 |  15 | 2016-12-08 13:14:24 |
|    11 |  16 | 2016-12-08 13:14:24 |
|    12 |  17 | 2016-12-08 13:14:24 |
|    13 |  18 | 2016-12-08 13:14:24 |
|    14 |  19 | 2016-12-08 13:14:24 |
|    15 | 999 | 2016-12-08 13:16:41 |
+-------+-----+---------------------+
15 rows in set (0.00 sec)

You can use an alias in your select to assign a new name to a column coming from your source table.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM