Mysql default value for Date and Datetime field

14,147

Solution 1

Use CURRENT_TIMESTAMP constant

also check this link

Solution 2

You can specify exact default values for DATE and DATETIME fields, e.g. -

CREATE TABLE table1(
  id INT(11) NOT NULL,
  date_column DATE DEFAULT '2012-01-01',
  datetime_column DATETIME DEFAULT '2010-05-05 10:30:00',
  PRIMARY KEY (id)
);

INSERT INTO table1 (id) VALUES (1);

SELECT * FROM table1;

+----+-------------+---------------------+
| id | date_column | datetime_column     |
+----+-------------+---------------------+
|  1 | 2012-01-01  | 2010-05-05 10:30:00 |
+----+-------------+---------------------+

If you want to use function to define DEFAULT values, then you can create a trigger.

Share:
14,147
Sumesh TG
Author by

Sumesh TG

Sr. Software Engineer. Mobile : 9446717047 E-mail : [email protected]

Updated on June 04, 2022

Comments

  • Sumesh TG
    Sumesh TG almost 2 years

    Can any one specify, how can be set default value for Date and Datetime field in Mysql from phpmyadmin interface?

  • Sumesh TG
    Sumesh TG almost 12 years
    Thank you. CURRENT_TIMESTAMP is only for 'timestamp' type, not for 'date' & 'datetime' type.
  • Sumesh TG
    Sumesh TG almost 12 years
    Thank you. Can you specify how to set default 'date' as 'current date' and 'datetime' as 'current date time', without using a trigger?.
  • Devart
    Devart almost 12 years
    No, there is no good ways. Unfortunately MySQL doesn't allow using functions to set default values for DATE, DATETIME fields. You also may implement this logic in a stored procedure, and use it to add new records.
  • mrsrinivas
    mrsrinivas almost 10 years
    @SumeshTG: FYI - storing date and time, as it is in database not suggestible.