
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
How to set NOW() as default value for datetime datatype in MySQL?
We can set the now() function as a default value with the help of dynamic default. First, we will create a table with data type” datetime”. After that, we will set now() as the default value for column “MyTime” as shown below.
Creating a table.
mysql> create table DefaultDateTimeDemo -> ( -> MyTime datetime default CURRENT_TIMESTAMP -> ); Query OK, 0 rows affected (0.59 sec)
After creating the above table, we won’t insert any value while using the insert command. This is done so that we can get the default date time with the help of dynamic value default.
Here is the query to insert records.
mysql> insert into DefaultDateTimeDemo values(); Query OK, 1 row affected (0.13 sec)
Now, we can check whether the default value now() is added or not. Here is the query to display records.
mysql> select *from DefaultDateTimeDemo;
The following is the output that shows the current date and time.
+---------------------+ | MyTime | +---------------------+ | 2018-11-09 11:58:47 | +---------------------+ 1 row in set (0.00 sec)
Now, we can verify whether the result is correct or not with the help of now() method. Here is the query to check the result.
mysql> select now();
The following is the output.
+---------------------+ | now() | +---------------------+ | 2018-11-09 11:58:40 | +---------------------+ 1 row in set (0.00 sec)
Look at the sample output above. Both of them gives the same result.
- Related Articles
- How to set default value for empty row in MySQL?
- How to set default Field Value in MySQL?
- How to set MySQL default value NONE?
- How to set default value to NULL in MySQL?
- Query MongoDB for a datetime value less than NOW?
- How do I set the default value for a column in MySQL?
- How to set default date time as system date time in MySQL?
- How to use a function for default value in MySQL?
- Set default value to a JSON type column in MySQL?
- How to create boolean column in MySQL with false as default value?
- How to set a default parameter value for a JavaScript function?
- Return the default fill value for a masked array with float datatype in Numpy
- Return the default fill value for a masked array with complex datatype in Numpy
- Creating a Table in MySQL to set current date as default
- MySQL DateTime Now()+5 days/hours/minutes/seconds?
