0% found this document useful (0 votes)
45 views

21may2020 DML Logs

1. The document describes the INSERT, UPDATE, and DELETE data manipulation language (DML) commands in SQL. 2. INSERT is used to add new rows to a table and has two syntax forms - specifying all column values or specifying only some column values. 3. UPDATE is used to modify existing rows in a table by setting column values, either for all rows or rows matching a condition. 4. Examples demonstrate using INSERT to add rows to multiple tables at once conditionally based on column values.

Uploaded by

Manjunath Jituri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views

21may2020 DML Logs

1. The document describes the INSERT, UPDATE, and DELETE data manipulation language (DML) commands in SQL. 2. INSERT is used to add new rows to a table and has two syntax forms - specifying all column values or specifying only some column values. 3. UPDATE is used to modify existing rows in a table by setting column values, either for all rows or rows matching a condition. 4. Examples demonstrate using INSERT to add rows to multiple tables at once conditionally based on column values.

Uploaded by

Manjunath Jituri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 16

Data Manipulation Language:

------------------------------------
INSERT
UPDATE
DELETE

INSERT:
Syntax:

1)INSERT INTO table_name VALUES(val1,val2,val3,.....valn);


In this case,values should be mentioned for all columns of the table

2)INSERT INTO table_name(col1,col2,col3) VALUES(val1,val2,val3);


In this case,values should be mentioned only for specific columns

SQL> select * from tab;

TNAME TABTYPE CLUSTERID


------------------------------ ------- ----------
BIN$iMVFuP9LBTDgU2XICcB9Pg==$0 TABLE
BONUS TABLE
DEPT TABLE
EMP TABLE
EMPC TABLE
EMPLOYEE TABLE
SALGRADE TABLE

7 rows selected.

SQL> desc employee;


Name Null? Type
----------------------------------------- -------- ----------------------------
EMPID NUMBER(4)
NAME VARCHAR2(15)
YEAR_OF_EXP INTERVAL YEAR(2) TO MONTH

SQL> desc test1;


Name
Null? Type

-----------------------------------------------------------------------------------
----------------------------- --------
----------------------------------------------------------------------------
ID
VARCHAR2(10)
EMPNAME
VARCHAR2(20)
SAL
NUMBER(5)
DEPTNO
NUMBER(5)
JOB
VARCHAR2(10)
COMM
NUMBER(4)

SQL>create table employee(empid number(4),name varchar(15),year_of_exp interval


year to month);
create table emps(empno number(4),ename varchar(10),year_of_exp interval day to
second); day to hour/day to minute/day to second

SQL> insert into emps values (1001,'Aalok',interval '10 5:40:40' day to second);

1 row created.

SQL> select * from emps;

EMPNO ENAME YEAR_OF_EXP


---------- ----------
---------------------------------------------------------------------------
1001 Aalok +10 05:40:40.000000

SQL> commit;

Commit complete.

SQL>

set lines 199 pagesize 1999

SQL> select * from employee;

no rows selected

SQL> insert into employee values(101,'AA',interval '2-5' year to month);--->values


for all the columns
insert into employee(empid,name) values(102,'BB');-------------------->values
for respective columns

insert into test1 values(101,'Aalok','20000',10,'DBA',NULL);

SQL> select * from employee;

EMPID NAME
---------- ---------------
YEAR_OF_EXP
---------------------------------------------------------------------------
101 AA
+02-05

SQL> clear scr


SQL> set lines 200 pagesize 200
SQL> r
1* select * from employee

EMPID NAME YEAR_OF_EXP


---------- ---------------
---------------------------------------------------------------------------
101 AA +02-05

col year_of_exp for a10

SQL> desc employee;


Name
Null? Type

-----------------------------------------------------------------------------------
------------------------------ --------
----------------------------------------------------------------------------
EMPID
NUMBER(4)
NAME
VARCHAR2(15)
YEAR_OF_EXP
INTERVAL YEAR(2) TO MONTH

SQL> insert into employee(empid,name) values(102,'BB');

1 row created.

SQL> select * from employee;

EMPID NAME YEAR_OF_EXP


---------- ---------------
---------------------------------------------------------------------------
101 AA +02-05
102 BB

SQL> commit;

Commit complete.

SQL> select * from employee;

EMPID NAME YEAR_OF_EXP


---------- ---------------
---------------------------------------------------------------------------
101 AA +02-05
102 BB

INSERT ALL:

1)Unconditional insert all


*Here we wont use where clause to apply condition
2)Conditional insert all
*Here we use where clause to apply condition

Unconditional Insert All Syntax:


----------------------------------------------------------
INSERT ALL
into table_name1...values....
into table_name2...values....
select * from dual;

Note:
DUAL--->Dummy table(DUAL table has only one row and one column)

1)INSERT INTO table_name VALUES(val1,val2,val3,.....valn); In this case,values


should be mentioned for all columns of the table

2)INSERT INTO table_name(col1,col2,col3) VALUES(val1,val2,val3); In this


case,values should be mentioned only for specific columns

insert all
into employee values(104,'DD',interval '1-5' year to month)--->method1
into employee(empid,name) values(103,'CC')-------------------->method2
into test1 values(104,'John',15000,30,'NETWORK',1000)
into test1 (id,sal,comm) values(105,20000,150)
select * from dual;

SQL> commit;

Commit complete.

SQL> select * from test1;

ID EMPNAME SAL DEPTNO JOB COMM


---------- -------------------- ---------- ---------- ---------- ----------
101 Aalok 20000 10 DBA
102 MJ 30000
20 DEVELOPER 500
104 John 15000 30 NETWORK 1000
105 20000 150

SQL> select * from employee;

EMPID NAME YEAR_OF_EXP


---------- ---------------
---------------------------------------------------------------------------
101 AA +02-05
102 BB
103 CC
104 DD +01-05

2)Conditional Insert All:


-------------------------------------------------------
INSERT ALL/FIRST
WHEN condition1 THEN
into table_name....values.....
WHEN condition2 THEN
into table_name....values.....
select ...from table_name;

INSERT ALL:All when conditions are checked, which are true those are executed

INSERT FIRST:First when condition which becomes true only that will be executed and
remaining when conditions are skipped

oredrs------->order info

small_orders
medium_orders
big_orders

create table orders(order_id number(4),order_name varchar(15),amount number(5));

insert all
into orders values(1001,'SQL',10000)
into orders values(1002,'PLSQL',10000)
into orders values(1003,'DBA',20000)
into orders values(1004,'JAVA',20000)
into orders values(1005,'Python',25000)
into orders values(1006,'C++',15000)
into orders values(1007,'DataScience',30000)
into orders values(1008,'DevOps',50000)
into orders values(1009,'Cloud',60000)
into orders values(1010,'ML',80000)
select * from dual;

SQL> select * from orders;

ORDER_ID ORDER_NAME AMOUNT


---------- --------------- ----------
1001 SQL 10000
1002 PLSQL 10000
1003 DBA 20000
1004 JAVA 20000
1005 Python 25000
1006 C++ 15000
1007 DataScience 30000
1008 DevOps 50000
1009 Cloud 60000
1010 ML 80000

10 rows selected.

create table small_orders as select * from orders where 1=0;


create table medium_orders as select * from orders where 1=0;
create table big_orders as select * from orders where 1=0;

small orders--->amount<30000
medium orders-->amount between 30000 and 50000
big orders----->amount>50000

insert all
when amount<30000 then
into small_orders values(order_id,order_name,amount)
when amount between 30000 and 50000 then
into medium_orders values(order_id,order_name,amount)
when amount>50000 then
into big_orders values(order_id,order_name,amount)
select * from orders;

SQL> select * from small_orders;

ORDER_ID ORDER_NAME AMOUNT


---------- --------------- ----------
1001 SQL 10000
1002 PLSQL 10000
1003 DBA 20000
1004 JAVA 20000
1005 Python 25000
1006 C++ 15000

6 rows selected.

SQL> select * from medium_orders;


ORDER_ID ORDER_NAME AMOUNT
---------- --------------- ----------
1007 DataScience 30000
1008 DevOps 50000

SQL> select * from big_orders;

ORDER_ID ORDER_NAME AMOUNT


---------- --------------- ----------
1009 Cloud 60000
1010 ML 80000

insert first
when amount<30000 then
into small_orders values(order_id,order_name,amount)
when amount between 30000 and 50000 then
into medium_orders values(order_id,order_name,amount)
when amount>50000 then
into big_orders values(order_id,order_name,amount)
select * from orders;

2)UPDATE:
----------
syntax:
1)UPDATE table_name SET col_name=value; Update all records in column to a new value

2)UPDATE table_name SET col_name=value where col_name=value; Updates only a


perticular record in a column to new value by taking reference of other column

SQL> select * from employee;

EMPID NAME YEAR_OF_EXP


---------- ---------------
---------------------------------------------------------------------------
101 AA +02-05
102 BB
+10-02

SQL> update employee set year_of_exp=(interval '3-5' year to month);

3 rows updated.

SQL> select * from employee;

EMPID NAME YEAR_OF_EXP


---------- ---------------
---------------------------------------------------------------------------
101 AA +03-05
102 BB +03-05
+03-05

SQL> rollback;

Rollback complete.

SQL> select * from employee;

EMPID NAME YEAR_OF_EXP


---------- ---------------
---------------------------------------------------------------------------
101 AA +02-05
102 BB

SQL> update employee set name='AB',year_of_exp=(interval '3-3' year to month) where


empid=102;

1 row updated.

SQL> select * from employee;

EMPID NAME YEAR_OF_EXP


---------- ---------------
---------------------------------------------------------------------------
101 AA +02-05
102 AB +03-03

===================================================================================
=
3)DELETE:

syntax:

1)DELETE FROM table_name; delete all records from the table

2)DELETE FROM table_name WHERE col_name=value; delete only specific record from the
table by taking reference of other column value

SQL> select * from employee;

EMPID NAME YEAR_OF_EXP


---------- ---------------
---------------------------------------------------------------------------
101 AA +02-05
102 BB
103 CC
104 DD
105 EE

SQL> clear scr


SQL> delete from employee where empid in(103,104,105);

3 rows deleted.

SQL> select * from emp;

EMPNO ENAME JOB MGR HIREDATE SAL COMM


DEPTNO
---------- ---------- --------- ---------- --------- ---------- ----------
----------
7369 SMITH CLERK 7902 17-DEC-80 800
20
7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300
30
7521 WARD SALESMAN 7698 22-FEB-81 1250 500
30
7566 JONES MANAGER 7839 02-APR-81 2975
20
7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400
30
7698 BLAKE MANAGER 7839 01-MAY-81 2850
30
7782 CLARK MANAGER 7839 09-JUN-81 2450
10
7788 SCOTT ANALYST 7566 19-APR-87 3000
20
7839 KING PRESIDENT 17-NOV-81 5000
10
7844 TURNER SALESMAN 7698 08-SEP-81 1500 0
30
7876 ADAMS CLERK 7788 23-MAY-87 1100
20
7900 JAMES CLERK 7698 03-DEC-81 950
30
7902 FORD ANALYST 7566 03-DEC-81 3000
20
7934 MILLER CLERK 7782 23-JAN-82 1300
10

14 rows selected.

SQL> select * from employee;

EMPID NAME YEAR_OF_EXP


---------- ---------------
---------------------------------------------------------------------------
101 AA +02-05
102 BB

SQL> update employee set name=null where empid=101;

1 row updated.

SQL> select * from employee;

EMPID NAME YEAR_OF_EXP


---------- ---------------
---------------------------------------------------------------------------
101 +02-05
102 BB

SQL> update employee set name='AALOK' where empid=101;

1 row updated.

SQL> select * from employee;

EMPID NAME YEAR_OF_EXP


---------- ---------------
---------------------------------------------------------------------------
101 AALOK +02-05
102 BB
---------------------------------------------------------------------------------

Creating table from other table


-------------------------------
syntax:
1)CREATE TABLE table_name AS SELECT * FROM table_name WHERE 1=0;
Create new table only with structure(columns,not null constraints)

2)CREATE TABLE table_name AS SELECT * FROM table_name [WHERE 1=1];


Creates new table along with structure and data

emp----------->empcopy
pk
u
nn------------>nn
fk

create table empcopy as select * from employees;


select * from empcopy;
desc empcopy;
drop table empcopy purge;

create table empcopy as select * from employees where 1=0;


select * from empcopy;
desc empcopy;

How to insert records in one table by taking records from other table
---------------------------------------------------------------------
Min req:Both table structure must be same
syntax:
insert into target_table_name select * from source_table_name;

ex:
insert into empcopy2 select * from emp;

MERGE:
------
One table retrieves data from other table by comparing which records are present
and which records are not present

present-------->update
not present---->insert

table1(most used) --------------------------------------->


table2
c1 c2 c1 c2
1 AALOK 1
HARSH
2 BB

Syntax:

MERGE INTO tableA a


USING tableB b ON(join condition)
WHEN MATCHED THEN
update a.col1=b.col2,....
WHEN NOT MATCHED THEN
insert into tableA values(val1,val2,...);

SQL> select * from dept;

DEPTNO DNAME LOC


---------- -------------- -------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON

create table deptcp as select * from dept where 1=0;


insert into deptcp values(10,'DATABASE','IND');
commit;

SQL> select * from deptcp;

DEPTNO DNAME LOC


---------- -------------- -------------
10 DATABASE IND

target table deptcp


source table dept--------------->gives data to---------------->target table deptcp

merge into deptcp t using dept s on (t.deptno=s.deptno)


when matched then
update set t.dname=s.dname,t.loc=s.loc
when not matched then
insert (t.deptno,t.dname,t.loc) values(s.deptno,s.dname,s.loc);

SQL> select * from deptcp;

DEPTNO DNAME LOC


---------- -------------- -------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON

-----------------------------------------------------------------------------------
--------------------------------------------------------------------------

SQL> insert into employee values(101,'AA',interval '2-5' year to month);

1 row created.

SQL> select * from employee;

EMPID NAME YEAR_OF_EXP


---------- ---------------
---------------------------------------------------------------------------
101 AA +02-05
SQL> rollback;

Rollback complete.

SQL> select * from employee;

no rows selected

SQL> insert into employee values(101,'AA',interval '2-5' year to month);

1 row created.

SQL> commit;

Commit complete.

SQL> select * from employee;

EMPID NAME YEAR_OF_EXP


---------- ---------------
---------------------------------------------------------------------------
101 AA +02-05

SQL> rollback;

Rollback complete.

SQL> select * from employee;

EMPID NAME YEAR_OF_EXP


---------- ---------------
---------------------------------------------------------------------------
101 AA +02-05

SQL> insert into test1 values(101,'Aalok','20000',10,'DBA',NULL);

1 row created.

SQL> select * from test1;

ID EMPNAME SAL DEPTNO JOB COMM


---------- -------------------- ---------- ---------- ---------- ----------
101 Aalok 20000 10 DBA

SQL> commit;

Commit complete.

SQL>

SQL> insert into test1(id,empname,sal) values(102,'MJ',30000);

1 row created.

SQL> commit;

Commit complete.
SQL> select * from test1;

ID EMPNAME SAL DEPTNO JOB COMM


---------- -------------------- ---------- ---------- ---------- ----------
101 Aalok 20000 10 DBA
102 MJ 30000

SQL> insert into test1(deptno,job,comm) values(20,'DEVELOPER',500);

1 row created.

SQL> commit;

Commit complete.

SQL> select * from test1;

ID EMPNAME SAL DEPTNO JOB COMM


---------- -------------------- ---------- ---------- ---------- ----------
101 Aalok 20000 10 DBA
102 MJ 30000
20 DEVELOPER 500

SQL> select * from employee;

EMPID NAME YEAR_OF_EXP


---------- ---------------
---------------------------------------------------------------------------
101 AA +02-05

SQL> insert into employee(empid,name) values(102,'BB');

1 row created.

SQL> select * from employee;

EMPID NAME YEAR_OF_EXP


---------- ---------------
---------------------------------------------------------------------------
101 AA +02-05
102 BB

SQL> commit;

Commit complete.

SQL>

create table emps(empno number(4),ename varchar(10),year_of_exp interval day to


second);

SQL> insert into emps values (1001,'Aalok',interval '10 5:40:40' day to second);

1 row created.

SQL> select * from emps;

EMPNO ENAME YEAR_OF_EXP


---------- ----------
---------------------------------------------------------------------------
1001 Aalok +10 05:40:40.000000

SQL> commit;

SQL> update test1 set sal=25000;

5 rows updated.

SQL> select * from test1;

ID EMPNAME SAL DEPTNO JOB COMM


---------- -------------------- ---------- ---------- ---------- ----------
101 Aalok 25000 10 DBA
102 MJ 25000
25000 20 DEVELOPER 500
104 John 25000 30 NETWORK 1000
105 25000 150

SQL> rollback;

Rollback complete.

SQL> select * from test1;

ID EMPNAME SAL DEPTNO JOB COMM


---------- -------------------- ---------- ---------- ---------- ----------
101 Aalok 20000 10 DBA
102 MJ 30000
20 DEVELOPER 500
104 John 15000 30 NETWORK 1000
105 20000 150

SQL> update test1 set sal=25000,deptno=20,job='ADMIN' where id=102;

1 row updated.

SQL> select * from test1;

ID EMPNAME SAL DEPTNO JOB COMM


---------- -------------------- ---------- ---------- ---------- ----------
101 Aalok 20000 10 DBA
102 MJ 25000 20 ADMIN
20 DEVELOPER 500
104 John 15000 30 NETWORK 1000
105 20000 150

SQL> commit;

Commit complete.

SQL>

SQL> select * from employee;

EMPID NAME YEAR_OF_EXP


---------- ---------------
---------------------------------------------------------------------------
101 AA +02-05
102 BB
103 CC
104 DD +01-05

SQL> update employee set year_of_exp=interval '3-5' year to month;

4 rows updated.

SQL> update employee set year_of_exp=(interval '3-5' year to month);

4 rows updated.

SQL> select * from employee;

EMPID NAME YEAR_OF_EXP


---------- ---------------
---------------------------------------------------------------------------
101 AA +03-05
102 BB +03-05
103 CC +03-05
104 DD +03-05

SQL> rollback;

Rollback complete.

SQL> select * from employee;

EMPID NAME YEAR_OF_EXP


---------- ---------------
---------------------------------------------------------------------------
101 AA +02-05
102 BB
103 CC
104 DD +01-05

SQL> update employee set name='AALOK',year_of_exp=(interval '3-5' year to month)


where empid=103;

1 row updated.

SQL> select * from employee;

EMPID NAME YEAR_OF_EXP


---------- ---------------
---------------------------------------------------------------------------
101 AA +02-05
102 BB
103 AALOK +03-05
104 DD +01-05

SQL> select * from employee;

EMPID NAME YEAR_OF_EXP


---------- ---------------
---------------------------------------------------------------------------
101 AA +02-05
102 BB
103 AALOK +03-05
104 DD +01-05

SQL> delete from employee;

4 rows deleted.

SQL> select * from employee;

no rows selected

SQL> rollback;

Rollback complete.

SQL> select * from employee;

EMPID NAME YEAR_OF_EXP


---------- ---------------
---------------------------------------------------------------------------
101 AA +02-05
102 BB
103 CC
104 DD +01-05

SQL> delete from employee where empid=104;

1 row deleted.

SQL> select * from employee;

EMPID NAME YEAR_OF_EXP


---------- ---------------
---------------------------------------------------------------------------
101 AA +02-05
102 BB
103 CC

SQL> delete from employee where empid in (102,103);

2 rows deleted.

SQL> select * from employee;

EMPID NAME YEAR_OF_EXP


---------- ---------------
---------------------------------------------------------------------------
101 AA +02-05

SQL> rollback;

Rollback complete.

SQL> select * from test1;

ID EMPNAME SAL DEPTNO JOB COMM


---------- -------------------- ---------- ---------- ---------- ----------
101 Aalok 20000 10 DBA
102 MJ 25000 20 ADMIN
20 DEVELOPER 500
104 John 15000 30 NETWORK 1000
105 20000 150

SQL> update test1 set comm=NULL;

5 rows updated.

SQL> select * from test1;

ID EMPNAME SAL DEPTNO JOB COMM


---------- -------------------- ---------- ---------- ---------- ----------
101 Aalok 20000 10 DBA
102 MJ 25000 20 ADMIN
20 DEVELOPER
104 John 15000 30 NETWORK
105 20000

SQL> rollback;

Rollback complete.

SQL> select * from test1;

ID EMPNAME SAL DEPTNO JOB COMM


---------- -------------------- ---------- ---------- ---------- ----------
101 Aalok 20000 10 DBA
102 MJ 25000 20 ADMIN
20 DEVELOPER 500
104 John 15000 30 NETWORK 1000
105 20000 150

SQL>

You might also like