How to make a mysql table with date and time columns?

前端 未结 2 1853
长发绾君心
长发绾君心 2021-01-15 04:52

I am trying to create a table that has date and time columns used to store the date and time information when the entry is recorded in the database. In the MySQL documentati

相关标签:
2条回答
  • 2021-01-15 05:31
    CREATE TABLE t1 (
      ts TIME,
      dt DATE
    );
    

    Only a timestamp field can have DEFAULT CURRENT_TIMESTAMP. It contains both a date and time. TIME and DATE fields will need to be set by your application.

    0 讨论(0)
  • 2021-01-15 05:36

    There is no point in having separate columns for Date and Time. It does not make much sense

    You can create the table like this

    CREATE TABLE timeDate (
       id INT,
      ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    );
    

    And if you want the Date part use this query

    SELECT DATE(`ts`) FROM timedate WHERE id =someId
    

    And if you want the Time part use this query

    SELECT TIME(`ts`) FROM timedate WHERE id =someId
    
    0 讨论(0)
提交回复
热议问题