Header Ads Widget

SQL INSERT Statement

SQL INSERT Statement

The SQL INSERT statement is used to insert a single or multiple data in a table. In SQL, You can insert the data in two ways:

  1. Without specifying column name
  2. By specifying column name

Sample Table

EMPLOYEE

EMP_IDEMP_NAMECITYSALARYAGE
1AngelinaChicago20000030
2RobertAustin30000026
3ChristianDenver10000042
4KristenWashington50000029
5RussellLos angels20000036

1. Without specifying column name

If you want to specify all column values, you can specify or ignore the column values.

Syntax

  1. INSERT INTO TABLE_NAME    
  2. VALUES (value1, value2, value 3, .... Value N);    

Query

  1. INSERT INTO EMPLOYEE VALUES (6, 'Marry', 'Canada', 600000, 48);  

Output: After executing this query, the EMPLOYEE table will look like:

EMP_IDEMP_NAMECITYSALARYAGE
1AngelinaChicago20000030
2RobertAustin30000026
3ChristianDenver10000042
4KristenWashington50000029
5RussellLos angels20000036
6MarryCanada60000048

2. By specifying column name

To insert partial column values, you must have to specify the column names.

Syntax

  1. INSERT INTO TABLE_NAME    
  2. [(col1, col2, col3,.... col N)]    
  3. VALUES (value1, value2, value 3, .... Value N);    

Query

  1. INSERT INTO EMPLOYEE (EMP_ID, EMP_NAME, AGE) VALUES (7, 'Jack', 40);  

Output: After executing this query, the table will look like:

EMP_IDEMP_NAMECITYSALARYAGE
1AngelinaChicago20000030
2RobertAustin30000026
3ChristianDenver10000042
4KristenWashington50000029
5RussellLos angels20000036
6MarryCanada60000048
7Jacknullnull40

Note: In SQL INSERT query, if you add values for all columns then there is no need to specify the column name. But, you must be sure that you are entering the values in the same order as the column exists.

Post a Comment

0 Comments