How to Add Columns to a Table in MySQL
In this article, you will learn how to add columns to a table in MySQL.
Adding columns to a table in MySQL
In MySQL, you may need to add new columns to an existing table to store additional data.
Syntax for adding columns to a table in MySQL
The syntax for adding columns to a table in MySQL is as follows:
ALTER TABLE table_name ADD COLUMN column_name datatype constraints;
- “table_name” is the name of the table you want to add a column to.
- “column_name” is the name of the new column you want to add.
- “datatype” specifies the data type of the new column.
- “constraints” are optional and can be used to define additional rules for the column (e.g. NOT NULL, UNIQUE, etc.).
Example of adding columns to a table in MySQL
Let’s look at an example of how to add two new columns called “phone_number” and “hire_date” to the “employees” table:
ALTER TABLE employees ADD COLUMN phone_number VARCHAR(20), ADD COLUMN hire_date DATE;
This command adds two new columns to the “employees” table. The “phone_number” column is a string with a maximum length of 20 characters, and the “hire_date” column is a date value.