SQL Tutorial

16 / 68

Inserting data into a table

Data can be inserted into a table in many ways.

Syntax:

insert into table_name ( field1, field2,...fieldN )
   values
   ( value1, value2,...valueN );

Insert statement example:

insert into deptt (depname,depcity, depstreet,depopendate)
values('Accounts','Bengaluru','Daker Street',NOW());
select last_insert_id();
insert into deptt (depname,depcity, depstreet,depopendate)
values('HR','Delhi','Baker Street',NOW());
select last_insert_id();
insert into deptt (depname,depcity, depstreet,depopendate)
values('Finance','Mumbai','Laker Street',NOW());
select last_insert_id();

A value inside 'values' expression can be a SELECT statement as well.

insert into deptt (depname,depcity, depstreet,depopendate)
VALUES('Finance','Mumbai',(SELECT ... FROM <some_table>),NOW());

In above SQL, department street is being selected from some other table and is inserted into department table.

Whole 'values' clause can also be replaced with SELECT from a table which is essentially copying data from a table into another.

insert into deptt(depname,depcity, depstreet,depopendate) SELECT depname,depcity, depstreet,depopendate FROM deptt;

No hints are availble for this assesment

Answer is not availble for this assesment

Loading comments...