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.
Author by
Sumesh TG
Sr. Software Engineer. Mobile : 9446717047 E-mail : [email protected]
Updated on June 04, 2022Comments
-
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?
-
outis almost 12 yearsWhich version of phpMyAdmin? Also, dba.SE is a better fit for this question.
-
Vishwanath Dalvi almost 12 yearsYou can check here dev.mysql.com/doc/refman/5.0/en/timestamp-initialization.html
-
-
Sumesh TG almost 12 yearsThank you. CURRENT_TIMESTAMP is only for 'timestamp' type, not for 'date' & 'datetime' type.
-
Sumesh TG almost 12 yearsThank you. Can you specify how to set default 'date' as 'current date' and 'datetime' as 'current date time', without using a trigger?.
-
Devart almost 12 yearsNo, 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 almost 10 years@SumeshTG: FYI - storing date and time, as it is in database not suggestible.