INSERT INTO

INSERT INTO statement is used to add data into the table.

Data in the table can be inserted into two ways:

  1. By specifying the both column and the values to be inserted.
  2. Only adding the values without specifying the columns.

By specifying the both column and the values to be inserted.

  • In this we need to first specify the columns in which we need to insert data and then enter the values.
  • This gives advantage of inserting the data into the specified columns only, leaving rest of the columns empty.

Syntax:

INSERT INTO table_name (column1, column2, column3, ... ColumnN)
VALUES (value1, value2, value3, ... ValueN);

Example: To add Roll_No and Name only leaving the rest of the columns empty in the table students.

INSERT INTO students (Roll_NO, Name)
VALUES (1, 'Arun');

This will insert the values as shown below:

Only adding the values without specifying the columns.

  • In this there is no need to specify the column name but the values should be in the same order as the columns.
  • All the values need to be entered i.e. we can not skip values.
  • If you want to keep a null value in a column, then you must insert a null in that column.

Syntax:

INSERT INTO table_name (column1, column2, column3, ... columnN );

Example:

INSERT INTO students VALUES (2, 'Rahul', 68, 2147483647);

This will add a new row as follows: