Tutorials

SQL INSERT


SQL INSERT INTO


In SQL, INSERT INTO statement is used to insert new records into the table.

Using INSERT we can:

  • insert records in all the columns of the table
  • insert records in specified columns of the table.

We will see both the ways with example.

 

Insert record in all columns

If we want to insert records in all the columns of the table, then we don't need to specify column name.

But we should ensure that the values are inserted in same order as the columns in the table.

 

Syntax for INSERT in all columns 

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

Example

 

We will insert values into the 'tih_Courses' table.

tih_Courses
Course_ID Course_Name Professor Mode_of_Training
       

We will insert:

Course id: 001

Course name: .NET

ProfessorMukesh Tiwari

Mode of Training: Online and Offline

 

To insert above records, sql INSERT query will be:

INSERT INTO tih_Courses
VALUES(001, '.NET', 'Mukesh Tiwari', 'Online and Offline');

On executing the above sql query, the record would be inserted. On executing SELECT statement we will see the table as follows.

 

tih_Courses
Course_ID Course_Name Professor Mode_of_Training
001 .NET Mukesh Tiwari Online and Offline

Syntax for INSERT in specific columns 

INSERT INTO table_name (column1, column2,...,columnN)
VALUES(value1, value2,...,valueN);

Example

 

We will insert:

Course id: 002

Course name: Java

Professor: Rajneesh Shukla

 

To insert above records, sql INSERT query will be:

INSERT INTO tih_Courses (Course_ID, CourseName, Professor)
VALUES (002, 'Java', 'Rajneesh Shukla')

On executing the above sql query, the record would be inserted. On executing SELECT statement we will see the table as follows.

 

tih_Courses
Course_ID Course_Name Professor Mode_of_Training
001 .NET Mukesh Tiwari Online and Offline
002 Java Rajneesh Shukla  

We see value is not inserted in mode of training column as we chose not to insert value into it.

 



Please Share this page
Views : 61
Like every other website we use cookies. By using our site you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Learn more Got it!