
- 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
For timestamp, which datatype is used in MySQL?
The TIMESTAMP data type is used for values containing both date and time parts. Let us first create a table −
mysql> create table DemoTable662( UserId int NOT NULL AUTO_INCREMENT PRIMARY KEY,UserName varchar(100),UserPostDate datetime ); Query OK, 0 rows affected (0.50 sec)
Following is the query for valid default timestamp values −
mysql> alter table DemoTable662 MODIFY COLUMN UserPostDate TIMESTAMP NOT NULL DEFAULT current_timestamp; Query OK, 0 rows affected (1.81 sec) Records: 0 Duplicates: 0 Warnings: 0
Let us check the description of table once again −
mysql> desc DemoTable662;
This will produce the following output −
+--------------+--------------+------+-----+-------------------+----------------+ | Field | Type | Null | Key | Default | Extra | +--------------+--------------+------+-----+-------------------+----------------+ | UserId | int(11) | NO | PRI | NULL | auto_increment | | UserName | varchar(100) | YES | | NULL | | | UserPostDate | timestamp | NO | | CURRENT_TIMESTAMP | | +--------------+--------------+------+-----+-------------------+----------------+ 3 rows in set (0.10 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable662(UserName) values('Chris'); Query OK, 1 row affected (0.24 sec) mysql> insert into DemoTable662(UserName) values('Robert'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable662(UserName,UserPostDate) values('Robert','2018-01-11'); Query OK, 1 row affected (0.27 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable662;
This will produce the following output −
+--------+----------+---------------------+ | UserId | UserName | UserPostDate | +--------+----------+---------------------+ | 1 | Chris | 2019-07-20 13:06:13 | | 2 | Robert | 2019-07-20 13:06:18 | | 3 | Robert | 2018-01-11 00:00:00 | +--------+----------+---------------------+ 3 rows in set (0.00 sec)
- Related Articles
- Which MySQL Datatype should be used for storing BloodType?
- Change a MySQL Column datatype from text to timestamp?
- Which MySQL datatype to used to store an IP address?
- Which datatype should I use for flag in MySQL?
- MySQL isn’t inserting binary data properly? Which datatype should be used?
- What is the best datatype for currencies in MySQL?
- MySQL BIT_LENGTH() function is used for which purpose?
- What is the smallest datatype for one bit in MySQL?
- Which MySQL data type is used for long decimal?
- Convert MySQL timestamp to UNIX Timestamp?
- How to search for a date in MySQL timestamp field?
- How to set NOW() as default value for datetime datatype in MySQL?
- How to sum rows of VARCHAR datatype or TIME datatype in MySQL?
- What is MySQL TRUNCATE command used for?
- What is MySQL DROP command used for?

Advertisements