

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 Questions & Answers
- 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 store time created in a MySQL table?
- How to add subtotal to a table column displaying NULL in MySQL?
- Can we add a column to a table from another table in MySQL?
- How to create and fill a new column in an already created MySQL table?
- How to update a timestamp field of a MySQL table?
- How to add time in a MySQL column set with type DATETIME?
- Add a single year to all the date records in a MySQL table column
- How to check if a timestamp is set in MySQL?
- How to add NOT NULL constraint to an already created MySQL column?
- How to set the background color of a column in a matplotlib table?
- How to delete a column from a table in MySQL?
- Creating a table with a TIMESTAMP field in MySQL?