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.
ALTER TABLE table_name 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.
ALTER TABLE table_name DROP COLUMN column_name;
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.
ALTER TABLE table_name MODIFY column_name column_type;
ALTER TABLE table_name ALTER COLUMN column_name column_type;
- To ADD 2 columns AGE and COURSE to table Student.
ALTER TABLE Student ADD (AGE number(3),COURSE varchar(40));
- MODIFY column COURSE in table Student
ALTER TABLE Student MODIFY COURSE varchar(20);
After running the above query maximum size of Course Column is reduced to 20 from 40.
- DROP column COURSE in table Student.
ALTER TABLE Student DROP COLUMN COURSE;
This article is contributed by Shubham Chaudhary. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Create, Alter and Drop schema in MS SQL Server
- SQL | ALTER (RENAME)
- Difference between ALTER and UPDATE Command in SQL
- Alter login in SQL Server
- ALTER SCHEMA in SQL Server
- Difference between Structured Query Language (SQL) and Transact-SQL (T-SQL)
- SQL | DROP, TRUNCATE
- Difference between DELETE and DROP in SQL
- Difference between DROP and TRUNCATE in SQL
- Drop login in SQL Server
- CREATE and DROP INDEX Statement in SQL
- DROP SCHEMA in SQL Server
- SQL | Procedures in PL/SQL
- SQL | Difference between functions and stored procedures in PL/SQL
- Difference between SQL and T-SQL
- MySQL | DROP USER
- Difference between DELETE, DROP and TRUNCATE
- DROP FUNCTION and its Parameters
- Mitigation of SQL Injection Attack using Prepared Statements (Parameterized Queries)
- SQL | SELECT Query