In This Article, You Will Know About SQL ALTER TABLE Statement.
Before moving ahead, let’s know a bit about the SQL DROP TABLE Statement.
Table of Contents
SQL ALTER TABLE Statement
The SQL ALTER TABLE statement is used to bring changes like add, delete, and modify columns in the existing table. It is also used to add and drop (delete) different terms on the existing table.
ALTER TABLE – ADD Column
To add a column in a table –
Syntax
ALTER TABLE table_name
ADD column_name column_datatype;
Example: Use ALTER TABLE statement to add a column to the existing table.
ALTER TABLE Student_Inforrmation
ADD Country char(20);
ALTER TABLE – DROP COLUMN
To delete a column in the table –
ALTER TABLE table_name
DROP COLUMN column_name;
Example: Use DROP COLUMN statement to delete a column to the existing table.
ALTER TABLE Student_Inforrmation
DROP COLUMN Country;
ALTER TABLE – ALTER/MODIFY COLUMN
To change the datatype of a column –
ALTER TABLE table_name
ALTER COLUMN column_name datatype;
Serial Number | Name | Class | Roll_No | Subject |
1 | A | I | 111 | Eco |
2 | B | II | 121 | Math |
3 | C | III | 131 | Sci |
4 | D | IV | 141 | Eng |
Example: Use ALTER TABLE statement to add a new column to the existing table “Student_Inforfmation”.
ALTER TABLE table_name
ADD Phone_No int;
Serial Number | Name | Class | Roll_No | Subject | Phone_No |
1 | A | I | 111 | Eco | |
2 | B | II | 121 | Math | |
3 | C | III | 131 | Sci | |
4 | D | IV | 141 | Eng |
Change Data Type Example
Now change the datatype of column “Phone_No”.
Example: Use ALTER TABLE statement to change the datatype of a column “Phone_No” in the existing table “Student_Inforfmation”.
ALTER TABLE Student_Inforfmation
ALTER COLUMN Phone_No char;
DROP COLUMN Example
Example: Use DROP COLUMN to delete the column “Phone_No”.
ALTER TABLE Student_Inforrmation
DROP COLUMN Phone_No;
Serial Number | Name | Class | Roll_No | Subject |
1 | A | I | 111 | Eco |
2 | B | II | 121 | Math |
3 | C | III | 131 | Sci |
4 | D | IV | 141 | Eng |
If you find anything incorrect in the above-discussed topic and have further questions, please comment below.
Connect on: