当前位置:首页 > PHP教程 > PHP总结归纳

MySQL中DATETIME跟TIMESTAMP的区别

mysql中datetime和timestamp的区别 先copy一份文档给大家看: datetime a date and time combination. the supported range is '1000-01-01 00:00:00' to '9999-12-31 23:59:59'. mysql displays datetime values in 'yyyy-mm-dd hh:mm:ss' format, but allow

mysql中datetime和timestamp的区别

先copy一份文档给大家看:

datetime
a date and time combination. the supported range is '1000-01-01 00:00:00' to '9999-12-31 23:59:59'.
mysql displays datetime values in 'yyyy-mm-dd hh:mm:ss' format, but allows you to assign values to datetime columns using either strings or numbers.
timestamp
a timestamp. the range is '1970-01-01 00:00:00' to partway through the year 2037.
a timestamp column is useful for recording the date and time of an insert or update operation.
the first timestamp column in a table is automatically set to the date and time of the most recent operation if you don't assign it a value yourself.
you can also set any timestamp column to the current date and time by assigning it a null value.

现在开始具体比较:

datetime,字节数为8,取值范围为“1000-01-01 00:00:00——9999-12-31 23:59:59”

对应java类型为java.sql.timestamp

insert或update操作时系统不会自动修改其值,不可以设定默认值,为必须字段时必须手动插入,建议使用:new()

mysql按照yyyy-mm-dd hh:mm:ss对数据进行格式化,允许以字符串和数字的方式提交

eg:insert into time_table(createdate) values(‘2014-06-09 15:01:01’)

或insert into time_table(createdate) values(‘20140609150101’)

timestamp,字节数为4,取值范围为“19700101080001——20380119111407”

对应java类型为java.sql.timestamp

insert或update操作时(且未手动赋值)系统会自动更新、插入当前系统时间,默认值为current_timestamp()

提交手动赋值为null时也会被赋值为当前系统时间,错误时会填入0

使用timestamp一定要注意他的时间范围(见上)。

.syntaxhighlighter{padding-top:20px;padding-bottom:20px;}

【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!