Pr.Pg

INSERT Statement Tutorials

An INSERT statement adds a new row to a table or a view.

 

Syntax

INSERT INTO <Table name> (Column names) VALUES (Value List)

 

There are two ways to insert data into table

1)     Without Specifying Column names, just data.

2)     Specifying Column names and Data to be inserted.

 

1) Inserting Data without specifying Column names

If you want to insert row into ADDRESS table with all the columns without column name,

 

The Syntax would be

USE <your database name>

INSERT INTO Table_name

VALUES (Value1, Value2, Value3, Value4, . . . );

 

If following is selected Data from "Staff" Table before Insert command:

 

Staff_ID

FirstName

LastName

Address

Basic_Pay

101

Samir

Patel

Pent-house, Neapan Sea Road,

2000000

102

John

Abharam

14th Road, Khar

3000000

103

Ketrina

Kaif

Duplex, Worli Sea Face,

4000000

 

To insert data into above table use following command, without specifying Column names.

 

 

INSERT INTO Staff Values

(104, "Ashwariya", "Rai", "Pent-house, Juhu ", 5000000)

 

 

After above Insert Statement, Data from "Staff" table will have one more row (4th one to be specific) as follows.

 

Staff_ID

FirstName

LastName

Address

Basic_Pay

101

Samir

Patel

Pent-house, Neapan Sea Road,

2000000

102

John

Abharam

14 th Road, Khar

3000000

103

Ketrina

Kaif

Duplex, Worli Sea Face,

4000000

104

Ashwariya

Rai

Pent-house, Juhu

5000000

 

 

2) Inserting Data with Column names

If you want to insert row into ADDRESS table with all the columns with column name,

 

The Syntax would be

 

USE <your database name>

INSERT INTO Table_name (Column1, Column2, Column3, Column4, . . .)

VALUES (Value1, Value2, Value3, Value4, . . . );

 

If following is selected Data from "Staff" Table before Insert command:

 

Staff_ID

FirstName

LastName

Address

Basic_Pay

101

Samir

Patel

Pent-house, Neapan Sea Road,

2000000

102

John

Abharam

14th Road, Khar

3000000

103

Ketrina

Kaif

Duplex, Worli Sea Face,

4000000

 

To insert data into above table use following command, without specifying Column names.

 

 

INSERT INTO Staff

(Staff_ID,FirstName,LastName,Address,Basic_Pay)

Values (104, "Ashwariya", "Rai", "Pent-house, Juhu ", 5000000)

 

 

After above Insert Statement, Data from "Staff" table will have one more row (4th one to be specific) as follows.

 

Staff_ID

FirstName

LastName

Address

Basic_Pay

101

Samir

Patel

Pent-house, Neapan Sea Road,

2000000

102

John

Abharam

14 th Road, Khar

3000000

103

Ketrina

Kaif

Duplex, Worli Sea Face,

4000000

104

Ashwariya

Rai

Pent-house, Juhu

5000000

 

To Insert Multiple Rows, do as follows

Syntax would be

USE <your database name>

INSERT INTO Table_name

VALUES (Value1, Value2, Value3, Value4, . . . ),

VALUES (Value1, Value2, Value3, Value4, . . . ),

VALUES (Value1, Value2, Value3, Value4, . . . ) ;

 

For Example

 

Insert into Staff Values

(105, "Sachin", "Tendulkar","Tendulkar Bunglow, Neapan Sea Road", 9000000),

(106, "Virat", "Kohali", "Pent-house, Khar", 6000000),

(107, "Suresh", "Raina", "Raheja Complex", 5000000);

 

 

Pr.Pg border