SQL Alter – SQL TUTORIAL | Software Testing Material

SQL ALTER Query:

SQL ALTER TABLE statement is used to add, delete, or modify columns in an existing table.

T0 Add A Column In A Table – Syntax:

1
ALTER TABLE table_name ADD column_name datatype;

Example:

1
ALTER TABLE SCOREBOARD ADD DotBalls int;

In the above image, you could find total number of columns are 5 (such as Playername, Runs, Balls, Sixers, and fours) related to first SELECT query where as the total number of columns are 6 (such as Playername, Runs, Balls, Sixers, fours, and DotBalls) related to second SELECT query. We have executed ALTER statement to add a new column (DotBalls) in the table ‘SCOREBOARD’.

To Delete A Column In A Table – Syntax:

Note: Some database systems don’t allow deleting a column.

1
ALTER TABLE table_name DROP COLUMN column_name;

Example:

1
ALTER TABLE SCOREBOARD DROP COLUMN DotBalls;

 

To Change The Data Type Of A Column In A Table – Syntax:

1
ALTER TABLE table_name ALTER COLUMN column_name datatype;

In the next tutorial, we will see How To Use DROP Query in SQL

Check out the complete SQL Tutorial by clicking on below link:

SQL Tutorial – Complete