Alter Table in SQL
ALTER TABLE IN SQL:- The ALTER TABLE statement is used to Add, Delete, Modify, Drop various existing columns in the table. This statement is also used to add and drop various constraints in the existing table.



- SQL ALTER TABLE ADD COLUMN:- This statement is used to add a new column in the existing table.
SYNTAX:-
ALTER TABLE table_name
ADD column_name datatype;
EXAMPLE:-
Consider the "STUDENTS" table having the following records −
Following is the example to ADD a new column to an existing table:-
ALTER TABLE STUDENTS
ADD Gender char (1);
Now, the "STUDENTS" table is changed and the New column "Gender" has been added to the existing table and following would be output from the SELECT statement.
OUTPUT:-
- ALTER TABLE DROP COLUMN:- This statement is used to DROP a column from the existing table.
SYNTAX:-
ALTER TABLE table_name
DROP COLUMN column_name;
The following SQL deletes the "Gender" column from the "STUDENTS" table:
EXAMPLE:-
ALTER TABLE STUDENTS DROP COLUMN Gender;
Now, the "STUDENTS" table is changed and the following would be the output from the SELECT statement.
OUTPUT:-
- ALTER TABLE MODIFY COLUMN:- This statement is used to change the Data Type of a column in the existing table.
SYNTAX:-
ALTER TABLE table_name
ALTER COLUMN column_name datatype;
EXAMPLE:-
Now we want to change the data type of the column named "Address" in the "STUDENTS" Table from "nvarchar" to "varchar" and column named "Age" from "nvarchar" to "int".
OUTPUT:-
- ALTER TABLE RENAME COLUMN:- This statement is used to set a new name to the old table name.
SYNTAX:-
ALTER TABLE table_name
RENAME COLUMN old_name to new_name;
Comments
Post a Comment