MySQL Add/Delete Column
A column is a series of cells in a table that may contain text, numbers, and images. Every column stores one value for each row in a table. In this section, we are going to discuss how to add or delete columns in an existing table.
How can we add a column in MySQL table?
MySQL allows the ALTER TABLE ADD COLUMN command to add a new column to an existing table. The following are the syntax to do this:
In the above,
- First, we need to specify the table name.
- Next, after the ADD COLUMN clause, we have to specify the name of a new column along with its definition.
- Finally, we need to specify the FIRST or AFTER keyword. The FIRST Keyword is used to add the column as the first column of the table. The AFTER keyword is used to add a new column after the existing column. If we have not provided these keywords, MySQL adds the new column as the last column in the table by default.
Sometimes it is required to add multiple columns into the existing table. Then, we can use the syntax as follows:
MySQL ADD COLUMN Example
Let us understand it with the help of various examples. Here, we will create a table named “Test” using the following statements:
The table structure looks like the below image:
After creating a table, we want to add a new column named City to the Test table. Since we have not specified the new column position explicitly after the column name, MySQL will add it as the last column.
Next, we want to add a new column named Phone_number to the Test table. This time, we will explicitly specify the new column position so that MySQL adds the column to the specified place.
In the below output, we can see that the two columns are added successfully at the specified position.
Let us add some data into the Test table using the INSERT statement as follows:
It will look like this.
Suppose we want to add more than one column ,(Branch, Email) in the Test table. In that case, execute the statement as follows:
It is to note that columns Branch and Email are assigned to default value NULL. However, the Test table already has data so that MySQL will use null values for those new columns.
We can verify the record in the Test table as below:
If we accidentally add a new column with the existing column name, MySQL will throw an error. For example, execute the below statement that issues an error:
We will get the following error message.
How can we rename a column in MySQL table?
MySQL allows the ALTER TABLE CHANGE COLUMN statement to change the old column with a new name. The following are the syntax to do this:
In the above,
- First, we need to specify the table name.
- Next, after the CHANGE COLUMN clause, we have to specify the old column name and new column name along with its definition. We must have to specify the column definition even it will not change.
- Finally, we need to specify the FIRST or AFTER keyword. It is optional that specified when we need to change the column name at the specific position.
MySQL RENAME COLUMN Example
This example shows how we can change the column name in the MySQL table:
This statement will change the column name Phone_number with the new name Mobile_number in the Test table. The below output explains it more clearly.
How can we drop a column from MySQL table?
Sometimes, we want to remove single or multiple columns from the table. MySQL allows the ALTER TABLE DROP COLUMN statement to delete the column from the table. The following are the syntax to do this:
In the above,
- First, we need to specify the table name from which we want to remove the column.
- Next, after the DROP COLUMN clause, we have to specify the column name that we want to delete from the table. It is to note that the COLUMN keyword is optional in the DROP COLUMN clause.
If we want to remove multiple columns from the table, execute the following statements:
MySQL DROP COLUMN Example
This example explains how we can delete a column from the MySQL table. Here, we will take a table “Test” that we have created earlier and look like the below image:
Suppose we want to delete a column name “Branch” from the Test table. To do this, execute the below statement:
After successful execution, we can verify the result below where a column Branch is deleted from the table:
In some cases, it is required to remove multiple columns from the table. To do this, we need to execute the below statement:
The command will delete both columns. We can verify it using the queries given in the below image.
Remember the following key points before deleting a column from the table:
MySQL works with relational databases where the schema of one table can depend on the columns of another table. So when we remove a column from one table, it will effects all dependent tables also. Consider the below points while removing column:
- When we remove columns from a table, it will affect all associated objects such as triggers, stored procedures, and views. Suppose we delete a column that is referencing in the trigger. After removing the column, the trigger becomes invalid.
- The dropped column depends on other applications code, must also be changed, which takes time and effort.
- When we remove a column from the large table, it will affect the database’s performance during removal time.