Javatpoint Logo
Javatpoint Logo

How to Insert One or More rows in SQL

In this article, we will learn how to insert one or more records in the table of Structured Query Language.

The INSERT INTO command in SQL allows the database users to insert one or more records into the SQL table.

Following are the two syntaxes for inserting the single row in the table:

Syntax 1:

This syntax inserts the row in the SQL table by specifying the column names of the table.

Syntax 2:

In this syntax, we have no need to specify the column names for inserting the row into the SQL table.

If you want to insert the record into the table, you have to follow the following steps one by one in the given sequence:

  1. Create the Database in the system.
  2. Create the Table in the database.
  3. Insert one row into the table.
  4. View the table.

Now, we are going to explain each step with an example:

Step 1: Create a Database

In the Structured Query Language, creating a database is the first step for storing the structured tables in the database.

Use the following SQL syntax to create a database:

Suppose you want to create the School database. For this, you have to type the following command in Structured Query Language:

Step 2: Create a Table and Insert the data

Now, you have to use the following syntax for creating the table in the SQL database:

Suppose you want to create the Teacher table in the School database. For this, you have to type the following query in your SQL application:

Step 3: Insert one row into the table

The following query inserts the record of only one teacher into the Teacher table:

Step 4: View the table

If you want to view the data of the Teacher table, then you have to write the following query in your SQL application:


Teacher_ID Teacher_Name Teacher_Subject Teacher_Address Teacher_Age
2001 Manoj Maths Delhi 27

Table: Teacher

Insert Multiple Rows into the table

In SQL, you can easily insert more than one row in the table using a single query.

The following Insert Into statement inserts the record of five teachers into the Teacher table:

To check the result of the above INSERT query, you have to write the following SELECT query in SQL:

Output:

Teacher_ID Teacher_Name Teacher_Subject Teacher_Address Teacher_Age
2001 Manoj Maths Delhi 27
2002 Anita Hindi Ghaziabad 28
2003 Vishal English Gorakhpur 26
2004 shobhit SST Kanpur 27
2005 Rohit Science Lucknow 29
2006 Yogesh Computer Jaipur 28

Table: Teacher

Insert Rows from the SELECT statement

If you want to insert the data of another table in the new table, use the INSERT statement with the SELECT statement.

The following statement passes the data of one table into another table:

Example

The data of the old table is shown in the following table:

Teacher_ID Teacher_Name Teacher_Subject Teacher_Address Teacher_Age
2001 Manoj Maths Delhi 27
2002 Anita Hindi Delhi 28
2003 Vishal English Gorakhpur 26
2004 shobhit SST Delhi 27
2005 Rohit Science Lucknow 29
2006 Yogesh Computer Delhi 28

Table: Old_Teacher

Suppose you have a New_Teacher table whose structure is the same as the Old_Teacher table, and you want to transfer the data of those teachers into the new table from the old table whose address is Delhi. For this operation, you have to use the following INSERT statement with the SELECT query:

To check the result of the above query, use the following SELECT statement:


Teacher_ID Teacher_Name Teacher_Subject
2001 Manoj Maths
2002 Anita Hindi
2004 shobhit SST
2006 Yogesh Computer

Table: New_Teacher







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA