Updating Tables
Over time, you may also need to modify a table by adding columns, adding new data, or updating existing rows. We will discuss how to do that in this section.
Adding and Removing Columns
To add new columns to an existing table, we use the ADD COLUMN
statement as in the following query:
ALTER TABLE {table_name} ADD COLUMN {column_name} {data_type};
Let's say, for example, that we wanted to add a new column to the products
table that we will use to store the products' weight in kilograms called weight
. We could do this by using the following query:
ALTER TABLE products ADD COLUMN weight INT;
This query will make a new column called weight
in the products
table and will give it the integer data type so that only numbers can be stored within it.
If you want to remove a column from a table, you can use the DROP
column statement:
ALTER TABLE {table_name} DROP COLUMN {column_name};
Here, {table_name}
is the name of the table you want to...