MySQL时间戳问题

MySQL时间戳问题,mysql,timestamp,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

我在看一些练习题

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; 假设您刚刚创建了此表: 创建表时间戳测试( ts1时间戳更新当前时间戳时的默认当前时间戳, i INT ); 当您查看它的结构时,您会注意到TIMESTAMP列被声明为NOTNULL。如果插入这些记录,会发生什么情况: mysql>插入到时间戳测试集ts1=NULL,i=10; mysql>插入时间戳测试集ts1=0,i=11; mysql>插入时间戳测试集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 只有第一条语句成功,时间戳列设置为当前日期和时间。其他两条语句给出了一个错误: mysql>插入到时间戳测试集ts1=NULL,i=10; 查询正常,1行受影响(0.00秒) mysql>插入时间戳测试集ts1=0,i=11; 错误1292(22007):第1行的列“ts1”的日期时间值“0”不正确 mysql>插入时间戳测试集ts1='',i=12; 错误1292(22007):第1行的列“ts1”的日期时间值“”不正确 但当我尝试插入ts1=0时,它会插入一个零值时间戳。。。答案错了吗

这取决于是否设置了该选项:

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
这取决于是否设置了该选项:

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