Insert data in mysql, in a field of type timestamp

1

How do I enter information in a field of this type? The field name is duration, I would like an example ..

    
asked by anonymous 28.04.2016 / 01:02

1 answer

2

Two syntaxes:

INSERT INTO meubanco.minhatabela ( duracao ) VALUES ( '2016-04-29 00:00:00' )

Or

INSERT INTO meubanco.minhatabela SET duracao = '2016-04-29 00:00:00'

(I hope your field is duracao , not duração , to avoid problems)

The important thing is to pass the date as string , in the format ano-mes-dia hora:minuto:segundo.centesimos

Ideally you will use 4 digits for the year, and 2 for all other fields, but if you pass in another format, in most cases MySQL does a guess and rounds values or expand as needed.

Some comments:

  • Years with two digits has the base date in 1970. Thus, values from 00 to 69 are converted to 2000 to 2069 , and values from 70 to 99 for the range 1970 to 1999 respectively.

  • To insert the current time, according to the DB server, you can use CURRENT_TIMESTAMP or NOW() ;

  • If your column is the first TIMESTAMP of the table, by default MySQL inserts the current timestamp if the field is omitted, or explicitly given NULL and column specified as NOT NULL ;

  • When inserting fields with the timezone of MySQL configured for something other than UTC, there is an internal conversion. So, if you insert a date and change the timezone , the date returned will be different (always relative to UTC).


More details in the manual:

  

link


Entering dates in DD / MM / YYYY format

Ideally an application should provide the dates for MySQL in the right format to avoid conversions, but if you need a pre-conversion for insertion, you can do this:

INSERT INTO meubanco.minhatabela ( duracao ) VALUES (
   STR_TO_DATE('29/04/2016','%d/%m/%Y')
)

Similarly, to include the time:

INSERT INTO meubanco.minhatabela ( duracao ) VALUES (
   STR_TO_DATE('29/04/2016 13:49:20','%d/%m/%Y h:%i:%s')
)

More details of fields and other conversion functions in the manual:

  

p>
    
28.04.2016 / 01:20