Modify Existing Table Structure in SQL: ALTER TABLE Command
To modify the structure of an existing table in SQL, use the ALTER TABLE statement.
ALTER is an SQL command used in Relational DBMS and is a Data Definition Language (DDL) statement. ALTER can be used to update the table’s structure in the database (like add, delete, drop indexes, columns, and constraints, modify the attributes of the tables in the database).
ALTER command is most commonly used to improve SQL SELECT queries by adding and removing indexes.
Syntax:
Adding a column to the existing table:
ALTER TABLE tableName ADD columnName columnDefinition;
Example
In this example, we modify structure of an existing table by adding a new column.
ALTER TABLE Student ADD marks_obtained Number (3);
Before: Student Table
name | class | contact | city |
---|---|---|---|
ashu | 10 | 90000 | Delhi |
santosh | 10 | 90001 | Delhi |
pankaj | 10 | 90002 | Delhi |
deepak | 10 | 90003 | Delhi |
After: Student Table
name | class | contact | city |
marks_obtained |
---|---|---|---|---|
ashu | 10 | 90000 | Delhi | |
santosh | 10 | 90001 | Delhi | |
pankaj | 10 | 90002 | Delhi | |
deepak | 10 | 90003 | Delhi |
Modifying existing data in SQL : UPDATE and ALTER TABLE Command
To modify existing records in a table in SQL, use the UPDATE statement. The UPDATE command allows users to change a specific value in a table. However, this command cannot be used to modify the structure of a table.
To modify the structure of an existing table, use the ALTER TABLE command. In this guide, we will learn how to modify existing data in SQL and how to modify the structure of an existing table in SQL.