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 COLUMN
In this SQL article, you will learn about the ADD COLUMN command in detail with syntax and examples.
What is ADD COLUMN Statement?
In many SQL situations, you may require to add the new columns or fields in the existing table. So, SQL provides the ADD keyword with ALTER TABLE command to overcome this situation.
The ALTER TABLE and CREATE TABLE are the two different statements in Structured Query Language that allows you to add columns. But the difference between the both statements is that CREATE statement adds columns at the time of table creation, whereas ALTER statement adds one or more columns later in the existing table.
Syntax of ADD Column statement
This ALTER TABLE syntax allows us to add a single new field to the existing table.
If you want to add more than one new field to the table in a single SQL query, you have to use the following syntax:
ADD (column_Name_1 column-definition,
column_Name_2 column-definition,
.....,
column_Name_N column-definition);
Examples of ADD Column statement in SQL
Here, we took the following two different examples of Structured Query Language, which will help us to add single and multiple fields in the existing table:
Example 1:
Firstly, we create the Teacher_Details table and then insert the dummy records into the Teacher_Details table.
(
Teacher_ID INT PRIMARY KEY,
First_Name VARCHAR (80),
Course VARCHAR (30) NOT NULL,
Teacher_Address VARCHAR (30),
Teacher_Age INT
) ;
The following INSERT query inserts the record of teachers into the above table:
( 1002, Anita, MCA, Ghaziabad, 25),
( 1003, Vishal, MBA, Gorakhpur, 24),
( 1004, Shobhit, BCA, Dehradun, 26),
( 1005, Rohit, MCA, Lucknow, 28),
( 1006, Yogesh, MBA, Jaipur, 28) ;
The following query displays the record of the Teacher_Details table:
Teacher_ID | First_Name | Course | Teacher_Address | Teacher_Age |
---|---|---|---|---|
2001 | Arun | MBA | Goa | 26 |
1002 | Anita | MCA | Ghaziabad | 25 |
1003 | Vishal | MBA | Gorakhpur | 24 |
1004 | shobhit | BCA | Dehradun | 26 |
1005 | Rohit | MCA | Lucknow | 28 |
1006 | Yogesh | MBA | Jaipur | 28 |
The following query adds the new Teacher_MailID column into the above Teacher_Details table:
Example 2:
Let's take another example which adds multiple columns into the table. Let's start by creating another Customer table:
(
Cust_Id Int,
First_Name Varchar (20),
Gender Varchar (10),
City Varchar (20),
) ;
The following query inserts the multiple records into the Customer table:
(502, Arun, Male, Mumbai),
(503, Somya, Female, Shimla),
(504, Ajay, Male, Delhi),
(505, Ram, Male, Dehradun),
(506, Anaya, Female, Haridwar);
Cust_ID | First_Name | Gender | City |
---|---|---|---|
501 | Jones | Male | Goa |
502 | Arun | Male | Mumbai |
503 | Somya | Female | Shimla |
504 | Ajay | Male | Delhi |
505 | Ram | Male | Dehradun |
506 | Anaya | Female | Haridwar |
Table: Customer
The following query adds two new columns in the Customer table: