createtableformDemo->(->IdintNOTNULLAUTO_INCREMENTPRIMARYKEY,->Emailvarchar(128),->PhoneNumbervarchar(15),->Countryvarchar(30),->Platformvarchar(40)->) ;"/> createtableformDemo->(->IdintNOTNULLAUTO_INCREMENTPRIMARYKEY,->Emailvarchar(128),->PhoneNumbervarchar(15),->Countryvarchar(30),->Platformvarchar(40)->) ;">

Home >Database >Mysql Tutorial >How to add 'Created on' column to table to set timestamp in MySQL?

How to add 'Created on' column to table to set timestamp in MySQL?

WBOY
WBOYforward
2023-09-08 10:25:111551browse

如何在表中添加“创建于”列来设置 MySQL 中的时间戳?

You need to use the ALTER command to add the created at column to the table that has been created in MySQL.

Let us first create a table. The query to create the table is as follows. This is the table without the "Created on" 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 and add a "Created on" column with type Timestamp and default value CURRENT_TIMESTAMP.

NOTE - Remember that if you leave spaces between , then you need to use backticks.

The following is the query to add the "Created in" column to the above table. The "created_at" column is a TIMESTAMP column and its default value is set to CURRENT_TIMESTAMP as shown in the following 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 look at the description of the table again with the help of DESC command.

The query is as follows

mysql> DESC formDemo;

The following is the output showing the "Created on" 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)

Looking at the sample output above, the "Created on" column has been added successfully.

The above is the detailed content of How to add 'Created on' column to table to set timestamp in MySQL?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete