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
Release: 2023-09-08 10:25:11
forward
1553 people have browsed it

如何在表中添加“创建于”列来设置 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)
Copy after login

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
Copy after login

Now look at the description of the table again with the help of DESC command.

The query is as follows

mysql> DESC formDemo;
Copy after login

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)
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template