• September 29, 2014
  • SQL
  • Comments Off on SQL INSERT statement

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. 1. Inserting the data directly to a table.
  2. 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.

Related Topics:

How to build java project using ant in eclipse?
JAXB marshalling – convert java object to xml example.
How to create pdf file in java using iText jar?
Generics class example.
OGNL in struts 2.
Hibernate One-to-One Mapping using xml.
Send inline image in email using JavaMail API.
Quartz 2 JobListener example.

 


Comments are closed.