在 MySql 工作台中将列设置为时间戳?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30608264/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 21:03:59  来源:igfitidea点击:

Setting a column as timestamp in MySql workbench?

mysqlsqltimestampmysql-workbench

提问by ocean800

This might be a really elementary question, but I've never created a table with TIMESTAMP()before, and I'm confused on what to put as the parameters. For example, here:

这可能是一个非常基本的问题,但我以前从未创建过表格TIMESTAMP(),而且我对将什么作为参数感到困惑。例如,这里:

enter image description here

在此处输入图片说明

I just randomly put TIMESTAMP(20), but what does the 20as a parameter signify here? What should be put in here?

我只是随机放置了TIMESTAMP(20),但是20作为参数在这里意味着什么?这里应该放什么?

I googled the question, but didn't really come up with anything so... Anyway I'm new to sql, so any help would be greatly appreciated, thank you!!

我用谷歌搜索了这个问题,但并没有真正想出什么所以......无论如何,我是 sql 的新手,所以任何帮助将不胜感激,谢谢!

采纳答案by spencer7593

EDIT

编辑

As of MySQL 5.6.4, datatype TIMESTAMP(n)specifies n(0 up to 6) decimal digits of precision for fractional seconds.

从 MySQL 5.6.4 开始,数据类型TIMESTAMP(n)指定n(0 到 6)小数秒精度的十进制数字。

Before MySQL 5.6, MySQL did not support fractional seconds stored as part of a TIMESTAMPdatatype.

在 MySQL 5.6 之前,MySQL 不支持作为TIMESTAMP数据类型的一部分存储的小数秒。

Reference: https://dev.mysql.com/doc/refman/5.6/en/fractional-seconds.html

参考:https: //dev.mysql.com/doc/refman/5.6/en/fractional-seconds.html



We don't need to specify a length modifier on a TIMESTAMP. We can just specify TIMESTAMPby itself.

我们不需要在 a 上指定长度修饰符TIMESTAMP。我们可以TIMESTAMP自己指定。

But be aware that the first TIMESTAMPcolumn defined in the table is subject to automatic initialization and update. For example:

但请注意,TIMESTAMP表中定义的第一列会自动初始化和更新。例如:

create table foo (id int, ts timestamp, val varchar(2));

show create table foo; 

CREATE TABLE `foo` (
`id` INT(11) DEFAULT NULL,
`ts` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`val` VARCHAR(2) DEFAULT NULL
) 


What goes in parens following a datatype depends on what the datatype is, but for some datatypes, it's a length modifier.

数据类型后括号中的内容取决于数据类型是什么,但对于某些数据类型,它是长度修饰符。

For some datatypes, the length modifier affects the maximum length of values that can be stored. For example, VARCHAR(20)allows up to 20 characters to be stored. And DECIMAL(10,6)allows for numeric values with four digits before the decimal point and six after, and effective range of -9999.999999 to 9999.999999.

对于某些数据类型,长度修饰符会影响可以存储的值的最大长度。例如,VARCHAR(20)最多允许存储 20 个字符。并DECIMAL(10,6)允许小数点前四位后六位的数值,有效范围为-9999.999999至9999.999999。

For other types, the length modifier it doesn't affect the range of values that can be stored. For example, INT(4)and INT(10)are both integer, and both can store the full range of values for allowed for the integer datatype.

对于其他类型,长度修饰符不会影响可以存储的值的范围。例如,INT(4)INT(10)都是整数,并且都可以存储整数数据类型所允许的完整范围的值。

What that length modifier does in that case is just informational. It essentially specifies a recommended display width. A client can make use of that to determine how much space to reserve on a row for displaying values from the column. A client doesn't have to do that, but that information is available.

在这种情况下,长度修饰符所做的只是提供信息。它实质上指定了推荐的显示宽度。客户端可以利用它来确定在一行上保留多少空间来显示列中的值。客户不必这样做,但该信息是可用的。

EDIT

编辑

A length modifier is no longer accepted for the TIMESTAMPdatatype. (If you are running a really old version of MySQL and it's accepted, it will be ignored.)

TIMESTAMP数据类型不再接受长度修饰符。(如果您正在运行一个非常旧的 MySQL 版本并且它被接受,它将被忽略。)

回答by Diego Nieto

Thats the precision my friend, if you put for example (2) as a parameter, you will get a date with a precision like: 2015-12-29 00:00:00.00, by the way the maximum value is 6.

这就是我的朋友的精度,如果您将例如(2)作为参数,您将获得精度为:2015-12-29 00:00:00 的日期。00顺便说一下,最大值是 6。

回答by Limabean

MySQL 5.7 appears to support this syntax. The argument passed is the precision. TIMESTAMP(3) will allow millisecond precision. 6 is the highest amount of allowed precision.

MySQL 5.7 似乎支持这种语法。传递的参数是精度。TIMESTAMP(3) 将允许毫秒精度。6 是允许的最高精度。

reference: http://dev.mysql.com/doc/refman/5.7/en/datetime.html

参考:http: //dev.mysql.com/doc/refman/5.7/en/datetime.html

回答by Kim Ryan

This syntax seems to be from old version of MySQL, prior to 4.1. It has been removed completely from 5.5 https://dev.mysql.com/doc/refman/5.0/en/upgrading-from-previous-series.html

此语法似乎来自 MySQL 4.1 之前的旧版本。它已从 5.5 https://dev.mysql.com/doc/refman/5.0/en/upgrading-from-previous-series.html 中完全删除

So no point in specifying a width here, as it may be ignored. What version are you running?

所以在这里指定宽度没有意义,因为它可能会被忽略。你运行的是什么版本?

回答by MajkelEight

In MySQL workbench 8.0

在 MySQL 工作台 8.0 中

TIMESTAMP

doesn't work, you need to add wole statement (if u don't want to update timestamp in future)

不起作用,您需要添加 wole 语句(如果您以后不想更新时间戳)

TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP 

than u have e.g :

比你有,例如:

2020-01-08 19:10:05

2020-01-08 19:10:05

but if you want that TIMESTAMP could be modify with the record update than you use :

但是,如果您希望可以使用记录更新来修改 TIMESTAMP,而不是使用:

TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP