The difference between Timestamp, time, and datetime in mysql

Original address: https://www.cnblogs.com/mxh1099/p/5461311.html

 

1. TIMESTAMP[(M)]
  timestamp. The range is '1970-01-01 00:00:00' to 2037.
  TIMESTAMP columns are used to record the date and time during INSERT or UPDATE operations.
  If you don't assign a value, the first TIMESTAMP column in the table is automatically set to the date and time of the most recent operation.
  It is also possible to set a TIMESTAMP column to the current date and time by assigning a NULL value.
  After the TIMESTAMP value is returned, it is displayed as a string in the format of 'YYYY-MM-DD HH:MM:SS', and the
  display width is fixed to 19 characters. If you want to get a numeric value, you should add +0 to the TIMESTAMP column.
  create table demo(
    addtime timestamp
  );
  insert into demo values(null);
  to insert the current time.
2. TIME
  time. The range is '-838:59:59' to '838:59:59'.
  MySQL displays TIME values ​​in the 'HH:MM:SS' format, but allows strings or numbers to be used to assign values ​​to TIME columns.

  Use now() to display 'HH:MM:SS'

  Use UNIX_TIMESTAMP() to display '-838:59:59' to '838:59:59'.

  mysql> insert into pluralpoem(title, create_time) values("chunxiao", UNIX_TIMESTAMP());
  Query OK, 1 row affected, 1 warning (0.01 sec)

  mysql> insert into pluralpoem(title, create_time) values("hua",now());
  Query OK, 1 row affected (0.00 sec)

  mysql> select * from pluralpoem;
  +----+------------+-------------+
  | id | title | create_time |
  +----+------------+-------------+
  | 1 | denggaowan | NULL |
  | 2 | chunxiao | 838:59:59 |
  | 3 | hua | 22:45:33 |
  +----+------------+-------------+
  3 rows in set (0.00 sec)


Three, DATETIME
  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 the 'YYYY-MM-DD HH:MM:SS' format, but allows strings or numbers to be used to assign values ​​to DATETIME columns.

4. For now()., curdate(), curtime(), UNIX_TIMESTAMP()

  The NOW() function returns the current date and time as `'YYYY-MM-DD HH:MM:SS', which can be directly stored in the DATETIME field.
  CURDATE() returns today's date in the format 'YYYY-MM-DD', which can be directly stored in the DATE field.
  CURTIME() returns the current time in the format of 'HH:MM:SS', which can be directly stored in the TIME field.

  UNIX_TIMESTAMP()

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325032268&siteId=291194637