CodesJava

Easy learning with example program codes

SQL INSERT statement

The INSERT statement is used to insert a new record into the table.

Ways to insert the data into a table:

  1. Inserting the data directly to a table.
  2. Inserting data to a table through a select statement.

1. Inserting the data directly to a table.

In case when we insert the values in the specified columns.
Syntax:

INSERT INTO tableName [(column1, column2,..., columnN)] VALUES (value1, value2,...,valueN);

Example:

INSERT INTO EMPLOYEE (EMP_ID, EMP_NAME, AGE, SALARY)
 VALUES (1, 'Parbhjot',28,35000);

Output:

1 row(s) inserted.

2. In case when we insert the values in all columns.
Syntax:

INSERT INTO tableName VALUES (value1, value2,...,valueN);

We can ignore the column names if we want to insert the values in all columns of the table.

Example:

INSERT INTO EMPLOYEE VALUES (5, 'Nidhi',28,45000);

Output:

1 row(s) inserted.

2. Inserting data to a table through a select statement.

In case when we insert the values in the specified columns.
Syntax:

INSERT INTO destinationTableName [(column1, column2, ... columnN)] 
SELECT column1, column2, ...columnN 
FROM sourceTableName WHERE [condition];

Example:

INSERT INTO EMPLOYEE1 (EMP_ID, EMP_NAME, AGE, SALARY) 
SELECT EMP_ID, EMP_NAME, AGE, SALARY 
FROM EMPLOYEE WHERE EMP_ID = '1';

Output:

1 row(s) inserted.

In case when we insert the values in all columns.
Syntax:

INSERT INTO destinationTableName SELECT * FROM sourceTableName;

Example:

INSERT INTO EMPLOYEE1 SELECT * FROM EMPLOYEE WHERE EMP_ID = '5';

Output:

1 row(s) inserted.

 
Next Topic: SQL UPDATE statement with example.
Previous Topic: SQL INDEX Constraint with example.

 

Please follow and like us:
Posted in SQL   








Copyright © 2019 CodesJava DMCA.com Protection Status