MySQL时间戳问题

时间:2009-08-24 10:47:49

标签: mysql timestamp

我在看一些练习题

Assume that you've just created this table:


CREATE TABLE timestamptest (
ts1 TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
i INT
);


When you look at its structure, you will notice that the TIMESTAMP column is declared NOT NULL. What happens if you insert these records:

mysql> INSERT INTO timestamptest SET ts1=NULL, i=10; 
mysql> INSERT INTO timestamptest SET ts1=0, i=11; 
mysql> INSERT INTO timestamptest SET ts1='', i=12;

ans是

Only the first statement succeeds, and the TIMESTAMP column is set to the current date and time. The other two statements give an error:

mysql> INSERT INTO timestamptest SET ts1=NULL, i=10;
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO timestamptest SET ts1=0, i=11;
ERROR 1292 (22007): Incorrect datetime value: '0' for column 'ts1' at row 1
mysql> INSERT INTO timestamptest SET ts1='', i=12;
ERROR 1292 (22007): Incorrect datetime value: '' for column 'ts1' at row 1

但是当我尝试插入ts1 = 0时,它会插入零值时间戳...答案是错误的吗?

1 个答案:

答案 0 :(得分:4)

取决于是否设置了NO_ZERO_DATE选项:

mysql> create table foo(x timestamp);
Query OK, 0 rows affected (0.01 sec)

mysql> set sql_mode='';
Query OK, 0 rows affected (0.00 sec)

mysql> insert into foo values(0);
Query OK, 1 row affected (0.00 sec)

mysql> set sql_mode='strict_all_tables,no_zero_date';
Query OK, 0 rows affected (0.00 sec)

mysql> insert into foo values(0);
ERROR 1292 (22007): Incorrect datetime value: '0' for column 'x' at row 1