Alter Table
ALTER
clause is used to add, delete, or modify columns in an existing table.
Let's learn more about ALTER clause using the following database.Database The database consists of a
player
table that stores the details of players who are a part of a tournament. player
table stores the name, age and score of players.Add Column
Syntax
SQL
- You can usePRAGMA TABLE_INFO(table_name)command to check the updated schema of the table.
Example
Add a new column
jersey_num
of typeinteger
to theplayer
table.SQL
Note
Default values for newly added columns in the existing rows will be NULL.
Rename Column
Syntax
SQL
Example
Rename the column
jersey_num
in theplayer
table tojersey_number
.SQL
Drop Column
Syntax
SQL
Example
Remove the column
jersey_number
from theplayer
table.SQL
Note
DROP COLUMN is not supported in some DBMS, including SQLite.
Try it Yourself!
- Add a new columnidof typeintto the player table.
- Update the name of columnidtoplayer_idin the player table.