Home > Database > Mysql Tutorial > How can we add a column with default value to an existing MySQL table?

How can we add a column with default value to an existing MySQL table?

王林
Release: 2023-08-26 18:37:02
forward
1281 people have browsed it

我们如何向现有 MySQL 表添加具有默认值的列?

We can also specify a default value while adding a column to an existing table with the help of ALTER command.

Syntax

Alter table table-name ADD (column-name datatype default data);
Copy after login

Example

In the following example, the "City" column (default value "DELHI") is added to the "Student" table with the help of the ALTER command .

mysql> Alter table Student ADD(City Varchar(10) Default 'DELHI');

Query OK, 5 rows affected (0.33 sec)
Records: 5 Duplicates: 0 Warnings: 0
Copy after login

Now, through the DESCRIBE command, we can check the default value of the "City" column.

mysql> describe Student\g

+---------+--------------+------+-----+---------+-------+
| Field   | Type         | Null | Key | Default | Extra |
+---------+--------------+------+-----+---------+-------+
| RollNO  | int(11)      | YES  |     | NULL    |       |
| Name    | varchar(20)  | YES  |     | NULL    |       |
| Class   | varchar(15)  | YES  |     | NULL    |       |
| Grade   | varchar(10)  | YES  |     | NULL    |       |
| Address | varchar(25)  | YES  |     | NULL    |       |
| Phone   | int(11)      | YES  |     | NULL    |       |
| Email   | varchar(20)  | YES  |     | NULL    |       |
| City    | varchar(10)  | YES  |     | DELHI   |       |
+---------+-------------+------+-----+---------+--------+

8 rows in set (0.04 sec)
Copy after login

The above is the detailed content of How can we add a column with default value to an existing MySQL table?. 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