The SQL ALTER
statement is used to modify the structure of an existing database object, such as a table, index, or view.
Here are a few examples of using the ALTER
statement to modify different objects in SQL:
- Adding a column to a table: The syntax for adding a column to a table is as follows:
ALTER TABLE table_name
ADD COLUMN column_name data_type;
Here is an example of adding a column named department
to the employees
table:
ALTER TABLE employees
ADD COLUMN department VARCHAR(50);
- Modifying a column: The syntax for modifying a column is as follows:
ALTER TABLE table_name
ALTER COLUMN column_name SET data_type;
Here is an example of modifying the data type of the salary
column in the employees
table to INT
:
ALTER TABLE employees
ALTER COLUMN salary SET INT;
- Dropping a column: The syntax for dropping a column is as follows:
ALTER TABLE table_name
DROP COLUMN column_name;
Here is an example of dropping the department
column from the employees
table:
ALTER TABLE employees
DROP COLUMN department;
In conclusion, the SQL ALTER
statement is used to modify the structure of an existing database object. The syntax for each type of modification may vary slightly, but the basic structure is similar. With the ALTER
statement, you can modify the structure of your database objects to meet changing needs and requirements.
Also check WHAT IS GIT ? It’s Easy If You Do It Smart
You can also visite the Git website (https://git-scm.com/)