How to get current date & time in MySQL?

后端 未结 10 1081
情深已故
情深已故 2020-11-29 17:18

Is there a value or command like DATETIME that I can use in a manual query to insert the current date and time?

INSERT INTO servers (
  server_name, online_         


        
相关标签:
10条回答
  • 2020-11-29 17:54

    Even though there are many accepted answers, I think this way is also possible:

    Create your 'servers' table as following :

    CREATE TABLE `servers`
    (
          id int(11) NOT NULL PRIMARY KEY auto_increment,
          server_name varchar(45) NOT NULL,
          online_status varchar(45) NOT NULL,
          _exchange varchar(45) NOT NULL, 
          disk_space varchar(45) NOT NULL,
          network_shares varchar(45) NOT NULL,
          date_time datetime NOT NULL DEFAULT CURRENT_TIMESTAMP
    );
    

    And your INSERT statement should be :

    INSERT INTO servers (server_name, online_status, _exchange, disk_space, network_shares)
    VALUES('m1', 'ONLINE', 'ONLINE', '100GB', 'ONLINE');
    

    My Environment:

    Core i3 Windows Laptop with 4GB RAM, and I did the above example on MySQL Workbench 6.2 (Version 6.2.5.0 Build 397 64 Bits)

    0 讨论(0)
  • 2020-11-29 17:54

    Yes, you can use the CURRENT_TIMESTAMP() command.

    See here: Date and Time Functions

    0 讨论(0)
  • 2020-11-29 17:55

    In database design, iIhighly recommend using Unixtime for consistency and indexing / search / comparison performance.

    UNIX_TIMESTAMP() 
    

    One can always convert to human readable formats afterwards, internationalizing as is individually most convenient.

    FROM_ UNIXTIME (unix_timestamp, [format ])
    
    0 讨论(0)
  • 2020-11-29 17:57

    İf you make change default value to CURRENT_TIMESTAMP it is more effiency,

    ALTER TABLE servers MODIFY COLUMN network_shares datetime NOT NULL DEFAULT CURRENT_TIMESTAMP;
    
    0 讨论(0)
提交回复
热议问题