21may2020 DML Logs
21may2020 DML Logs
------------------------------------
INSERT
UPDATE
DELETE
INSERT:
Syntax:
7 rows selected.
-----------------------------------------------------------------------------------
----------------------------- --------
----------------------------------------------------------------------------
ID
VARCHAR2(10)
EMPNAME
VARCHAR2(20)
SAL
NUMBER(5)
DEPTNO
NUMBER(5)
JOB
VARCHAR2(10)
COMM
NUMBER(4)
SQL> insert into emps values (1001,'Aalok',interval '10 5:40:40' day to second);
1 row created.
SQL> commit;
Commit complete.
SQL>
no rows selected
EMPID NAME
---------- ---------------
YEAR_OF_EXP
---------------------------------------------------------------------------
101 AA
+02-05
-----------------------------------------------------------------------------------
------------------------------ --------
----------------------------------------------------------------------------
EMPID
NUMBER(4)
NAME
VARCHAR2(15)
YEAR_OF_EXP
INTERVAL YEAR(2) TO MONTH
1 row created.
SQL> commit;
Commit complete.
INSERT ALL:
Note:
DUAL--->Dummy table(DUAL table has only one row and one column)
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.
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
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;
10 rows selected.
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;
6 rows selected.
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
3 rows updated.
SQL> rollback;
Rollback complete.
1 row updated.
===================================================================================
=
3)DELETE:
syntax:
2)DELETE FROM table_name WHERE col_name=value; delete only specific record from the
table by taking reference of other column value
3 rows deleted.
14 rows selected.
1 row updated.
1 row updated.
emp----------->empcopy
pk
u
nn------------>nn
fk
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
Syntax:
-----------------------------------------------------------------------------------
--------------------------------------------------------------------------
1 row created.
Rollback complete.
no rows selected
1 row created.
SQL> commit;
Commit complete.
SQL> rollback;
Rollback complete.
1 row created.
SQL> commit;
Commit complete.
SQL>
1 row created.
SQL> commit;
Commit complete.
SQL> select * from test1;
1 row created.
SQL> commit;
Commit complete.
1 row created.
SQL> commit;
Commit complete.
SQL>
SQL> insert into emps values (1001,'Aalok',interval '10 5:40:40' day to second);
1 row created.
SQL> commit;
5 rows updated.
SQL> rollback;
Rollback complete.
1 row updated.
SQL> commit;
Commit complete.
SQL>
4 rows updated.
4 rows updated.
SQL> rollback;
Rollback complete.
1 row updated.
4 rows deleted.
no rows selected
SQL> rollback;
Rollback complete.
1 row deleted.
2 rows deleted.
SQL> rollback;
Rollback complete.
5 rows updated.
SQL> rollback;
Rollback complete.
SQL>