In SQL, you can update the structure of an existing table using the ALTER TABLE
command. This allows you to add or drop columns as needed.
To add a column to a table, you can use the following syntax:
ALTER TABLE table_name
ADD column_name data_type;
For example, let’s say we have a table named “people” with columns for age and name. If we want to add a new column called “born_year” of data type INT, we would execute the following query:
ALTER TABLE people
ADD born_year INT;
This will add the new column with empty values to all existing rows in the table.
To drop a column from a table, you can use the following syntax:
ALTER TABLE table_name
DROP COLUMN column_name;
Continuing with our example, if we want to remove the “born_year” column from the “people” table, we would execute the following query:
ALTER TABLE people
DROP COLUMN born_year;
This will remove the column from the table, and any data stored in that column will be lost.
By using the ALTER TABLE
command with the appropriate alterations, you can easily update the structure of a SQL table to meet your specific requirements.