What is the use of ADD, DROP and MODIFY Commands?
Question
Share
Sign Up to our social questions and Answers to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers to ask questions, answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
ALTER TABLE is used to add, delete/drop or modify columns in the existing table. It is also
used to add and drop various constraints on the existing table.
ALTER TABLE – ADD
ADD is used to add columns into the existing table. Sometimes we may require to add
additional information, in that case we do not require to create the whole database
again, ADD comes to our rescue.
Syntax:
ADD (Columnname_1 datatype,
Columnname_2 datatype,
Columnname_n datatype);
ALTER TABLE – DROP
DROP COLUMN is used to drop column in a table. Deleting the unwanted columns from
the table.
Syntax:
DROP COLUMN column_name;
ALTER TABLE-MODIFY
It is used to modify the existing columns in a table. Multiple columns can also be modified
at once.
Syntax may vary slightly in different databases. Syntax(Oracle,MySQL,MariaDB):
MODIFY column_name column_type;
Syntax(SQL Server):
ALTER COLUMN column_name column_type;
Queries
Sample Table:
Student
1 Ram
2 Abhi
3 Rahul
4 Tanu
QUERY:•
To ADD 2 columns AGE and COURSE to table Student
OUTPUT:
1 Ram
2 Abhi
3 Rahul
4 Tanu
• MODIFY column COURSE in table Student
After running the above query maximum size of Course Column is reduced to 20
from 40.
ALTER TABLE Student DROP COLUMN COURSE;
ROLL_NONAMEAGE
1 Ram
2 Abhi
3 Rahul
4 Tanu