2014-05-15 18 views
9

Il mio database è MySql 5.6.MySQL - valore predefinito per TIMESTAMP (3)

voglio usare CURRENT_TIMESTAMP come valore predefinito un attributo che è di tipo di TIMESTAMP (3).

ma ho l'errore:

ERROR 1067 (42000): Invalid default value for 'updated'

Penso che sia perché CURRENT_TIMESTAMP è solo nella precisione del secondo.

Come posso impostare l'ora corrente come valore predefinito per un timestamp con parte frazionaria?

+0

Prova prima non avendo 3 in TI MESTAMP (3). Assicurati inoltre di avere un solo campo nella tabella che utilizza CURRENT_TIMESTAMP. –

+0

Il problema è dovuto ai microsecondi aggiunti nel valore predefinito nelle nuove versioni di mysql. Vedi http://tekina.info/default-datetime-timestamp-issue-mysql-upgrading-5-6/ per le soluzioni. –

risposta

12

Come da documentazione timestamp e datetime tipo colonne:

If a TIMESTAMP or DATETIME column definition includes an explicit fractional seconds precision value anywhere, the same value must be used throughout the column definition.

This is permitted:

CREATE TABLE t1 (
    ts TIMESTAMP(6) DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6) 
); 

Altri esempi:

mysql> create table tbl_so_q23671222_1(ts timestamp(3) default now()); 
ERROR 1067 (42000): Invalid default value for 'ts' 

mysql> create table tbl_so_q23671222_1(ts timestamp(3) default now(3)); 
Query OK, 0 rows affected (0.59 sec) 

mysql> create table tbl_so_q23671222_2(ts timestamp(3) default current_timestamp); 
ERROR 1067 (42000): Invalid default value for 'ts' 

mysql> create table tbl_so_q23671222_2(ts timestamp(3) default current_timestamp(3)); 
Query OK, 0 rows affected (0.38 sec) 

mysql> desc tbl_so_q23671222_1; 
+-------+--------------+------+-----+----------------------+-------+ 
| Field | Type   | Null | Key | Default    | Extra | 
+-------+--------------+------+-----+----------------------+-------+ 
| ts | timestamp(3) | NO |  | CURRENT_TIMESTAMP(3) |  | 
+-------+--------------+------+-----+----------------------+-------+ 
1 row in set (0.01 sec) 

mysql> desc tbl_so_q23671222_2; 
+-------+--------------+------+-----+----------------------+-------+ 
| Field | Type   | Null | Key | Default    | Extra | 
+-------+--------------+------+-----+----------------------+-------+ 
| ts | timestamp(3) | NO |  | CURRENT_TIMESTAMP(3) |  | 
+-------+--------------+------+-----+----------------------+-------+ 
1 row in set (0.01 sec) 

riferiscono a:
Initialization and Updating for TIMESTAMP and DATETIME

Problemi correlati