mysql

Add columns, alter table SQL query

An SQL query is a query that is used to alter a table. This query is used to add columns to a table, alter the data type of a column, or to change the name of a column.

ALTER TABLE 
    table_name 
ADD (
    Column_name1 data_type, 
    Column_name2 data_type (size)
);

ALTER table SQL query is used for adding new columns, constraints, and indexes to a table which already exists in the database. It is also used for changing the behavior of columns of a table.

For example, if you want to add a new column named 'Address' in the 'Employee' table, you can execute below SQL query for that.

ALTER TABLE Employee ADD COLUMN Address varchar(255)
or
ALTER TABLE Employee ADD(Address varchar(255))

Modifying existing column

Syntax:

ALTER TABLE table_name MODIFY (column_name new_data_type(new_size));

Example:

ALTER TABLE Employee MODIFY (Address varchar(255));

Removing column from table

ALTER query can be used to remove a column from the table using below command

ALTER TABLE table_name DROP COLUMN column_name;
Was this helpful?