Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Section 9: Data Type之日期时间类型 #19

Open
bestmem opened this issue Mar 15, 2023 · 0 comments
Open

Section 9: Data Type之日期时间类型 #19

bestmem opened this issue Mar 15, 2023 · 0 comments

Comments

@bestmem
Copy link

bestmem commented Mar 15, 2023

问题描述

MySQL 8中,默认datetime和timestamp都是可以为空,且默认值为NULL。
image

问题发生在第几章第几节

  1. DATETIME和TIMESTAMP
    image

有解决办法么?

在MySQL 7中,如果未指定TIMESTAMP列的默认值,则其默认为CURRENT_TIMESTAMP。这意味着,如果插入新行时未显式指定TIMESTAMP列的值,则该列将被自动设置为当前时间,同时EXTRA为ON UPDATE CURRENT_TIMESTAMP。
而在MySQL 8中,如果未指定TIMESTAMP列的默认值,则其默认为NULL。这意味着,如果插入新行时未显式指定TIMESTAMP列的值,则该列将被设置为NULL。
需要注意的是,虽然默认值如此,但是我们仍然可以通过在创建表时为TIMESTAMP列或DATETIME列指定参数来达到我们想要的效果。例如:
● NOT NULL:不为空
● DEFAULT CURRENT_TIMESTAMP:默认为当前数据库系统时间,可通过select now();查看。
● ON UPDATE CURRENT_TIMESTAMP:当该行有更新时,该时间字段自动更新为当前时间。

测试用的Mysql版本

MySQL8

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant