SQL Insert Command | SQL Tutorial | Minigranth

SQL INSERT Command : Introduction

  • Once the tables in the databases are created, it’s now time to insert values into these tables. This can be done using SQL INSERT command. INSERT command can be used in two different types. Both of them are explained below. Let’s look at its syntax first :

SQL INSERT Command : Syntax

This image describes the basic syntax of any insert command which is used in sql insert command.
SQL INSERT Command : Syntax

This image describes the basic syntax of any insert command which is used in sql.
SQL INSERT Command : Syntax

SQL INSERT Command : Example

  • Inserting two values in each tables student_details and teacher_details of the database db_school.

For teacher_details Table

Query-1 : INSERT Into teacher_details Values(101, ‘Anurag’, ‘TGT’);

Query-2 : INSERT Into teacher_details Values(102, ‘Anoop’, ‘PGT’);

For  student_details Table

Query-1 : INSERT Into student_details Values(1, ‘Saurav’, 50);

Query-2 : INSERT Into student_details Values(2, ‘Rakesh’, 50);

To display the data, execute the below command.

Query : SELECT * From student_details;

  • In this way, one can insert as much data as possible in the database.
  • The thing one must remember while inserting the data in the database is the order of columns. If value of roll_no is inserted into name or vice-versa, the query will not execute.
  • Also, values should be inserted according to the data types mentioned while creating the tables.

NOTE : student_details table has the following structure

Example Query : CREATE Table student_details(Roll_no int, Name varchar(20), Marks int);

If while entering the values in the tables created using above query, data is inserted without keeping the data type in mind, an un-executable query will be generated.

Example Query : INSERT Into student_details Values(‘Shayam’,’Ram’,30);

Here, in place of integer data type, character(‘Shyam’) is used which is not acceptable.