SQL Tutorial
SQL Database
SQL Table
SQL Select
SQL Order By
SQL Insert
SQL Update
SQL Delete
Difference
SQL Injection
SQL String Functions
Miscl
- SQL Formatter
- SQL group by
- SQL add/drop/update column operation
- SQL CAST Function
- SQL Comments
- SQL CONCAT Function
- CTE (Common Table Expression)SQL
- How to use distinct in SQL?
- Joining Three or More Tables in SQL
- What is Web SQL?
- How to create functions in SQL?
- How to run SQL Script?
- How to Delete Duplicate Rows in SQL?
- Nth Highest salary
- 12 Codd's Rules
- SQL EXCEPT
- Types of SQL JOIN
- Change datatype of column in SQL
- SQL Auto Increment
- SQL Like
- Commit and Rollback in SQL
- SQL Concatenate
- SQL get month from the date
- Savepoint in SQL
- SQL ORDER BY DATE
- TIME Datatype in SQL
- SQL BETWEEN
- CRUD Operations in SQL
- SQL INDEX
- Scalar Functions in SQL
- SET Operators in SQL
- Types of SQL Commands
- TCL Commands in SQL
- SQL Subquery
- SQL View
- Constraints in SQL
- Pattern Matching in SQL
- SQL Date Functions
- DDL Commands in SQL
- DML Commands in SQL
- SQL CASE
- SQL Inner Join
- SQL IN Operator
- Check Constraint in SQL
- SQL CLAUSES
- SQL LOGICAL OPERATORS
- Delete Column from Table
- Add Column in the Table
- Delete one row in SQL
- Change the Column Value
- How to Add Foreign Key in SQL
- Add a Primary Key
- Insert One or More rows
- How to Use LIKE in SQL
- Cursor in SQL
- Difference Between DROP and Truncate
- SQL Comparison Operators
- SQL COUNT WHERE
- SQL SELECT MIN
- SQL Stored Procedure
- SQL SELECT AVG
- SQL SELECT MAX
- SQL ADD COLUMN
- How to use Auto-Increment in SQL
- SQL Languages
- SQL Arithmetic Operators
- How to Use GROUP BY in SQL
- How to Use ORDER BY in SQL
- Trigger in SQL
- What is Race Condition
- SQL COUNT DISTINCT
PL/SQL Tutorial
Sql Interview Question
SQl Quiz
SQL add/drop/update column operation
The statement ALTER TABLE is mainly used to delete, add, or modify the columns into an existing table. It is also used to add many constraints on the current table.
ADD COLUMN is used to add the particular table to the existing one. We can add additional information without creating the whole database again.
SQL add column is used to add column operation into the existing SQL table. We can drop the table and recreate it according to our need. In the production environment, it is destructive as it pertains to the data.
Syntax of ADD COLUMN
The add column operation is used before the table with the help of transact SQL command.
ADD column_name column_definition;
According to the syntax,
- Specify the table where we want to add the new column firstly.
- Then, we specify the column definition from the ADD Column
Syntax of column definition:
If we want to add multiple columns to the existing table using any single statement, we can use the below syntax:
ADD [COLUMN] column_definition, (for adding column)
ADD [COLUMN] column_definition,
...;
Many database support ALTER TABLE ADD COLUMN statement.
To add any one column to a table using SQL, we can specify that if we want to change the table structure by the ALTER TABLE command, which is followed by the ADD command in RDBMS.
Syntax:
The syntax for ALTER TABLE Add Column is,
ADD "column_name" "Data Type";
Examples:
Look at the below example. Assuming our starting point is the Student table created in the CREATE TABLE section:
Table Student
Column Name | Data Type |
---|---|
First_Name | char(30) |
Last_Name | char(25) |
Birth_Date | datetime |
Address | char(50) |
City | char(40) |
Country | char(20) |
Example 1: Add one column to the table
If we want to add any column named "Gender." Then, we write:
MySQL:
The result is shown below:
Table Student
Column Name | Data Type |
---|---|
First_Name | char(30) |
Last_Name | char(25) |
Birth_Date | datetime |
Address | char(50) |
City | char(40) |
Country | char(20) |
Gender | char(1) |
Note: The new column named "Gender" becomes the last column in the Student table.
Example 2: Add multiple columns to the table
It is possible to add multiple columns.
For example, if we want to add a column called "Telephone" and another column called "Email," we should type the following:
MySQL:
Now the table becomes:
Table Student
Column Name | Data Type |
---|---|
First_Name | char(30) |
Last_Name | char(25) |
Birth_Date | datetime |
Address | char(50) |
City | char(40) |
Country | char(20) |
Gender | char(1) |
Telephone | char(15) |
char(20) |
Drop-Table command
The drop column is used to drop the column in the table. It is used to delete the unnecessary columns from the table.
Syntax:
DROP COLUMN column_name;
ALTER TABLE- MODIFY
Modify function is used to modify the existing columns in a simple table. Multiple columns can be changed at once.
Syntax:
MODIFY column_name column_type;
TABLE EMPLOYEE
ROLL_NO | Name OF Employee |
---|---|
1 | Jessy |
2 | Trump |
3 | Stephan |
4 | Hawkins |
QUERY:
- To ADD 2 columns COURSE and AGE to table Student.
OUTPUT:
ROLL_NO | Name of employee | AGE | COURSE |
---|---|---|---|
1 | Jessy | ||
2 | Trump | ||
3 | Stephan | ||
4 | Hawkins |
- To "MODIFY" column AGE in table Employee
After running the above query maximum size, of Course, Column is reduced to 15 from 20.
- To DROP column in the table Employee.
OUTPUT:
ROLL_NO NAME | COURSE | ||
---|---|---|---|
1 | Ram | ||
2 | Abhi | ||
3 | Rahul | ||
4 | Tanu |