将列设置为MySql工作台中的时间戳? [英] Setting a column as timestamp in MySql workbench?

查看:104
本文介绍了将列设置为MySql工作台中的时间戳?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是一个非常基本的问题,但是我以前从未用TIMESTAMP()创建表,而且我对作为参数的内容感到困惑.例如,在这里:

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(20),但是20作为参数在这里表示什么?这里应该放什么?

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

我用谷歌搜索了这个问题,但实际上并没有提出任何建议...无论如何,我还是sql的新手,所以对您的帮助将不胜感激,谢谢!

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!!

推荐答案

EDIT

从MySQL 5.6.4开始,数据类型TIMESTAMP(n)指定精度为n(0到6)小数秒的小数位数.

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之前,MySQL不支持存储为TIMESTAMP数据类型一部分的小数秒.

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

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

我们不需要在TIMESTAMP上指定长度修饰符.我们可以单独指定TIMESTAMP.

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

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

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

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.

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

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.

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

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.

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

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.

编辑

TIMESTAMP数据类型不再接受长度修饰符. (如果您运行的是MySQL的真正旧版本,并且该版本已被接受,它将被忽略.)

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

这篇关于将列设置为MySql工作台中的时间戳?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆