
- 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 add a “created at” column in a table to set the timestamp in MySQL?
You need to use ALTER command to add a created at column to an already created table in MySQL.
Let us first create a table. The query to create a table is as follows. Here is your table without the “created at” column
mysql> create table formDemo - > ( - > Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, - > Email varchar(128), - > PhoneNumber varchar(15), - > Country varchar(30), - > Platform varchar(40) - > ); Query OK, 0 rows affected (0.53 sec)
Now implement the above syntax to add a “created at” column with type timestamp and default with CURRENT_TIMESTAMP.
Note - Keep in mind if you are giving a space between created at then you need to use the backtick symbol.
The following is the query to add a “created at” column for the above table. The “created_at” column is a TIMESTAMP column whose default value is set to CURRENT_TIMESTAMP as you can see in the below query
mysql> alter table formDemo - > add column `created at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP; Query OK, 0 rows affected (0.42 sec) Records: 0 Duplicates: 0 Warnings: 0
Now check the description of table once again with the help of DESC command.
The query is as follows
mysql> DESC formDemo;
The following is the output displaying the “created at” column in MySQL
+-------------+--------------+------+-----+-------------------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------------+--------------+------+-----+-------------------+----------------+ | Id | int(11) | NO | PRI | NULL | auto_increment | | Email | varchar(128) | YES | | NULL | | | PhoneNumber | varchar(15) | YES | | NULL | | | Country | varchar(30) | YES | | NULL | | | Platform | varchar(40) | YES | | NULL | | | created at | timestamp | NO | | CURRENT_TIMESTAMP | | +-------------+--------------+------+-----+-------------------+----------------+ 6 rows in set (0.01 sec)
Look at the above sample output, the column “created at” has been added successfully.
- Related Articles
- How to add a column using MySQL SELECT in an already created table?
- How to add a column in a table in MySQL?
- How to add a column to a MySQL table in Python?
- How to add subtotal to a table column displaying NULL in MySQL?
- How to create and fill a new column in an already created MySQL table?
- How to store time created in a MySQL table?
- Can we add a column to a table from another table in MySQL?
- How to add time in a MySQL column set with type DATETIME?
- How to check if a timestamp is set in MySQL?
- How to add NOT NULL constraint to an already created MySQL column?
- How to update a timestamp field of a MySQL table?
- Add a single year to all the date records in a MySQL table column
- How can we add a time interval to date stored in a column of MySQL table?
- How to add a NOT NULL column in MySQL?
- How to delete a column from a table in MySQL?
