Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting a column as timestamp in MySql workbench?

Tags:

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:

enter image description here

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

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

like image 282
ocean800 Avatar asked Jun 02 '15 23:06

ocean800


People also ask

How do I add a timestamp to a column in MySQL?

Here is the SQL you can use to add the column in: ALTER TABLE `table1` ADD `lastUpdated` TIMESTAMP ON UPDATE CURRENT_TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ; This adds a column called 'lastUpdated' with a default value of the current date/time.

How do I create a timestamp in MySQL?

MySQL retrieves and displays DATETIME values in ' YYYY-MM-DD hh:mm:ss ' format. The supported range is '1000-01-01 00:00:00' to '9999-12-31 23:59:59' . The TIMESTAMP data type is used for values that contain both date and time parts. TIMESTAMP has a range of '1970-01-01 00:00:01' UTC to '2038-01-19 03:14:07' UTC.

How do I change a timestamp to a column in SQL?

Syntax – Update value to Current TimestampALTER TABLE table_name updates table schema. CHANGE column_name updates the column to. column_name TIMESTAMP NOT NULL defines the column as of datatype TIMESTAMP. DEFAULT CURRENT_TIMESTAMP sets the default value of the column to CURRENT_TIMESTAMP.


2 Answers

EDIT

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

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

Reference: 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 TIMESTAMP by itself.

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.

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.

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.

EDIT

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.)

like image 153
spencer7593 Avatar answered Oct 14 '22 21:10

spencer7593


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.

like image 33
Diego Nieto Avatar answered Oct 14 '22 19:10

Diego Nieto