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:
This syntax inserts the row in the SQL table by specifying the column names of the table.
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:
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:
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:
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:
The data of the old table is shown in the following table:
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: