As your data changes over time, SQL provides a way for you to update your corresponding tables and
database schemas by using the ALTER TABLE
statement to add, remove, or modify columns and table
constraints.
The syntax for adding a new column is similar to the syntax when creating new rows in the CREATE TABLE
statement. You need to specify the data type of the column along with any potential table constraints
and default values to be applied to both existing and new rows. In some databases like MySQL,
you can even specify where to insert the new column using the FIRST
or AFTER
clauses, though this
is not a standard feature.
ALTER TABLE mytable
ADD column DataType OptionalTableConstraint
DEFAULT default_value;
Dropping columns is as easy as specifying the column to drop, however, some databases (including SQLite) don't support this feature. Instead you may have to create a new table and migrate the data over.
ALTER TABLE mytable
DROP column_to_be_deleted;
If you need to rename the table itself, you can also do that using the RENAME TO
clause of the statement.
ALTER TABLE mytable
RENAME TO new_table_name;
Each database implementation supports different methods of altering their tables, so it's always best to consult your database docs before proceeding: MySQL, Postgres, SQLite, Microsoft SQL Server.
Our exercises use an implementation that only support adding new columns, so give that a try below.