SQL Tutorial

SQL Tutorial SQL Introduction SQL Syntax SQL Data Types SQL OPERATORS SQL COMMANDS SQL Queries

SQL Database

SQL Create Database SQL DROP Database SQL SELECT Database

SQL Table

SQL TABLE SQL CREATE TABLE SQL COPY TABLE SQL ALTER TABLE SQL DELETE SQL TRUNCATE TABLE SQL DROP TABLE SQL UPDATE TABLE SQL INSERT TABLE

SQL SELECT

SQL SELECT Statement SQL SELECT WHERE Clause SQL SELECT IN Operator SQL BETWEEN Operator SQL SELECT BETWEEN Operator SQL SELECT AND Operator SQL SELECT OR Operator SQL SELECT LIKE Operator SQL SELECT DISTINCT SQL SELECT SUM SQL SELECT MAX SQL SELECT MIN SQL SELECT AVG

SQL Clause

SQL WHERE Clause SQL GROUP BY CLAUSE SQL ORDER BY Clause SQL HAVING Clause

SQL INSERT

SQL INSERT Statement SQL INSERT INTO Statement SQL INSERT INTO Values SQL INSERT INTO SELECT SQL Insert multiple rows

SQL JOIN

SQL JOIN SQL Inner Join SQL Left Join SQL Right Join SQL Full Join SQL CROSS Join

SQL OPERATOR

SQL Comparison SQL LOGICAL Operator SQL Cast Operator SQL Arithmetic

Difference

SQL vs NOSQL WHERE vs HAVING DELETE vs DROP GROUP BY vs ORDER BY DROP vs TRUNCATE SQL IN vs SQL EXISTS Difference between Delete, Drop and Truncate in SQL

MISC

SQL SubQuery SQL CASE Commit and Rollback in SQL Pattern Matching in SQL DDL Commands in SQL DML Commands in SQL Types of SQL Commands SQL COUNT SQL Primary Key SQL FOREIGN KEY SET Operators in SQL Check Constraint in SQL SQL EXCEPT SQL VIEW SQL WHERE Statement SQL CRUD Operation Where Condition in SQL TCL Commands in SQL Types of SQL JOINS SQL Nth Highest Salary SQL NOT OPERATOR SQL UNION ALL SQL INTERSECT SQL Data Definition Language SQL Data Manipulation Language SQL Data Control Language SQL CONSTRAINTS SQL Aggregate Operators SQL KEYS Codd’s Rules in SQL What is SQL Injection? Trigger In SQL SQL WHERE Multiple Conditions Truncate function in SQL SQL Formatter WEB SQL SQL Auto Increment Save Point in SQL space() function in SQL SQL Aggregate Functions SQL Topological Sorting SQL Injection SQL Cloning Tables SQL Aliases SQL Handling Duplicate Update Query in SQL Grant Command in SQL SQL SET Keyword SQL Order BY LIMIT SQL Order BY RANDOM

How To

How to use the BETWEEN operator in SQL How To Use INNER JOIN In SQL How to use LIKE in SQL How to use HAVING Clause in SQL How to use GROUP BY Clause in SQL How To Remove Duplicates In SQL How To Delete A Row In SQL How to add column in table in SQL ? How to drop a column in SQL? How to create a database in SQL? How to use COUNT in SQL? How to Create Temporary Table in SQL? How to Add Foreign Key in SQL? How to Add Comments in SQL? How To Use Group By Clause In SQL How To Use Having Clause In SQL How To Delete Column In Table How To Compare Date In SQL How index works in SQL How to calculate age from Date of Birth in SQL How to Rename Column name in SQL What are single row and multiple row subqueries?

How to add column in table in SQL ?

How to add column in table in SQL 

Introduction

  • To add a column in already created table, one needs to use the ALTER command along with the ADD clause.
  • If in the query, it is not specified where the new column is to be added, then by default it will be added as a last column.
  • One can also add a new column at the first or even after a specific column of an already created table.
  • You can add one column or more than one columns at a time using a single SQL query.

(A). Adding a new column at the last of an existing table

Syntax:

ALTER TABLE tablename ADD (ColumnName datatype);

         where,

  1. Tablename is the name of an already existing table to which you have to add a new column.
  2. Column_name is the name of the column which is to be added to an already existing table.

Example:

First, we will create a database with name “studentdb”. Then in that database we will create a table “student” and insert records into the table. We will consider the same database and also the same table for subsequent examples.

Now, we will add a new column ‘City’ to an existing table.

 mysql> USE studentdb;
 Database changed
 mysql> SELECT *FROM student;
 +---------+-----------+-----------+-------------+
 | Stud_ID | Stud_Name | Course_ID | Course_Name |
 +---------+-----------+-----------+-------------+
 |       1 | Prajakta  |       101 | DBMS        |
 |       2 | Shweta    |       102 | CN          |
 |       3 | Nikita    |       103 | OS          |
 |       4 | Ankita    |       104 | C           |
 +---------+-----------+-----------+-------------+
 4 rows in set (0.00 sec)
 mysql> ALTER TABLE student ADD (City VARCHAR(20));
 Query OK, 4 rows affected (0.29 sec)
 Records: 4  Duplicates: 0  Warnings: 0
 mysql> SELECT *FROM student;
 +---------+-----------+-----------+-------------+------+
 | Stud_ID | Stud_Name | Course_ID | Course_Name | City |
 +---------+-----------+-----------+-------------+------+
 |       1 | Prajakta  |       101 | DBMS        | NULL |
 |       2 | Shweta    |       102 | CN          | NULL |
 |       3 | Nikita    |       103 | OS          | NULL |
 |       4 | Ankita    |       104 | C           | NULL |
 +---------+-----------+-----------+-------------+------+
 4 rows in set (0.00 sec) 
add column in table in SQL

New column ‘City’ is added to an existing student table. Since, in the query we haven’t specified where it is to be added so by default it is added as the last column.

(B)Adding more than one column to an existing table        

Syntax:

ALTER TABLE tablename ADD (ColumnName1 datatype, ColumnName2 datatype);

Example:

Now, we will add two new columns ‘City’ and ‘Marks’ to an existing table using a single query.

mysql> USE studentdb;

Output:

 Database changed
 mysql> SELECT *FROM student;
 +---------+-----------+-----------+-------------+
 | Stud_ID | Stud_Name | Course_ID | Course_Name |
 +---------+-----------+-----------+-------------+
 |       1 | Prajakta  |       101 | DBMS        |
 |       2 | Shweta    |       102 | CN          |
 |       3 | Nikita    |       103 | OS          |
 |       4 | Ankita    |       104 | C           |
 +---------+-----------+-----------+-------------+
 4 rows in set (0.00 sec)
 mysql> ALTER TABLE student ADD (City VARCHAR(20),Marks INT);
 Query OK, 4 rows affected (0.40 sec)
 mysql> SELECT *FROM student; 

Output:

 +---------+-----------+-----------+-------------+------+-------+
 | Stud_ID | Stud_Name | Course_ID | Course_Name | City | Marks |
 +---------+-----------+-----------+-------------+------+-------+
 |       1 | Prajakta  |       101 | DBMS        | NULL |  NULL |
 |       2 | Shweta    |       102 | CN          | NULL |  NULL |
 |       3 | Nikita    |       103 | OS          | NULL |  NULL |
 |       4 | Ankita    |       104 | C           | NULL |  NULL |
 +---------+-----------+-----------+-------------+------+-------+
 4 rows in set (0.00 sec) 
add column in table in SQL

New columns ‘City’ and ‘Marks’ are added to an existing student table. Since, in the query we haven’t specified where it is to be added so by default both the columns are added at the last using a single query.

(C) Adding a column at the first position of an existing table

Syntax:

ALTER TABLE tablename ADD ColumnName datatype FIRST;

Example: Now, we will add a new column ‘Sr_No’ to an existing table as a first column.

mysql> USE studentdb;

Output:

 Database changed
 mysql> SELECT *FROM student;
 +---------+-----------+-----------+-------------+
 | Stud_ID | Stud_Name | Course_ID | Course_Name |
 +---------+-----------+-----------+-------------+
 |       1 | Prajakta  |       101 | DBMS        |
 |       2 | Shweta    |       102 | CN          |
 |       3 | Nikita    |       103 | OS          |
 |       4 | Ankita    |       104 | C           |
 +---------+-----------+-----------+-------------+
 4 rows in set (0.00 sec)
 mysql> ALTER TABLE student ADD Sr_No INT FIRST; 

Output:

 Query OK, 4 rows affected (0.24 sec)
 Records: 4  Duplicates: 0  Warnings: 0
 mysql> SELECT *FROM student; 

Output:

 +-------+---------+-----------+-----------+-------------+
 | Sr_No | Stud_ID | Stud_Name | Course_ID | Course_Name |
 +-------+---------+-----------+-----------+-------------+
 |  NULL |       1 | Prajakta  |       101 | DBMS        |
 |  NULL |       2 | Shweta    |       102 | CN          |
 |  NULL |       3 | Nikita    |       103 | OS          |
 |  NULL |       4 | Ankita    |       104 | C           |
 +-------+---------+-----------+-----------+-------------+
 4 rows in set (0.00 sec) 
add column in table in SQL

New column ‘Sr_No’ is added to an existing student table. Since, in the query we have specified ‘FIRST’ keyword so ‘Sr_No’ is added as a first column.

(D) Adding a new column after a specific column of an existing table

Syntax:

ALTER TABLE tablename ADD ColumnName datatype AFTER column_name;

Example:

Now, we will add a new column ‘Marks’ to an existing table after the  ‘Course_Name’ .

mysql> USE studentdb;

Output:

 Database changed
 mysql> SELECT *FROM student; 

Output:

 +---------+-----------+-----------+-------------+------+
 | Stud_ID | Stud_Name | Course_ID | Course_Name | City |
 +---------+-----------+-----------+-------------+------+
 |       1 | Prajakta  |       101 | DBMS        | NULL |
 |       2 | Shweta    |       102 | CN          | NULL |
 |       3 | Nikita    |       103 | OS          | NULL |
 |       4 | Ankita    |       104 | C           | NULL |
 +---------+-----------+-----------+-------------+------+
 4 rows in set (0.00 sec)
 mysql> ALTER TABLE student ADD Marks INT AFTER Course_Name; 

Output:

 Query OK, 4 rows affected (0.28 sec)
 Records: 4  Duplicates: 0  Warnings: 0 
mysql> SELECT *FROM student;

Output:

 +---------+-----------+-----------+-------------+-------+------+
 | Stud_ID | Stud_Name | Course_ID | Course_Name | Marks | City |
 +---------+-----------+-----------+-------------+-------+------+
 |       1 | Prajakta  |       101 | DBMS        |  NULL | NULL |
 |       2 | Shweta    |       102 | CN          |  NULL | NULL |
 |       3 | Nikita    |       103 | OS          |  NULL | NULL |
 |       4 | Ankita    |       104 | C           |  NULL | NULL |
 +---------+-----------+-----------+-------------+-------+------+
 4 rows in set (0.00 sec) 
add column in table in SQL

New column ‘Marks’ is added to an existing student table. Since, in the query we have specified ‘AFTER’ keyword with the column name after which the new column is to be added. Hence, ‘Marks’ is added after ‘Course_Name’.