SQL ALTER
ALTER TABLE statement
The ALTER TABLE statement is used to add, delete, or modify columns in an existing table.
SQL ALTER TABLE syntax
To add columns to the table, please use the following syntax:
ADD column_name datatype
To delete a column in a table, use the following syntax (please note that some database systems do not allow this method of deleting columns in a database table):
DROP COLUMN column_name
To change the data type of a column in a table, use the following syntax:
SQL Server/MS Access:
ALTER COLUMN column_name datatype
My SQL/Oracle:
MODIFY COLUMN column_name datatype
##SQL ALTER TABLE examplePlease see the "Persons" table:
LastName | FirstName | Address | City | |
---|---|---|---|---|
Hansen | Ola | Timoteivn 10 | Sandnes | |
Svendson | Tove | Borgvn 23 | Sandnes | |
Pettersen | Kari | Storgt 20 | Stavanger |
Data Types Reference.
Now, the "Persons" table will look like this:LastName | FirstName | Address | City | DateOfBirth | |||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Hansen | Ola | Timoteivn 10 | Sandnes | ||||||||||||||||||||||
Tove | Borgvn 23 | Sandnes | ##3 | ||||||||||||||||||||||
Kari | Storgt 20 | Stavanger | Change Data Type ExampleNow, we want to change the data type of the "DateOfBirth" column in the "Persons" table. We use the following SQL statement: ALTER TABLE Persons ALTER COLUMN DateOfBirth year Please note that now the "DateOfBirth" column The type is year, which can store the year in 2-digit or 4-digit format. DROP COLUMN ExampleNext, we want to delete the "DateOfBirth" column in the "Person" table. We use the following SQL statement: ALTER TABLE Persons DROP COLUMN DateOfBirth Now, the "Persons" table will look like this :
|