DML(Data Manipulation Language)/TCL(Transaction Control Lanuage)
DML. DML is short name of Data Manipulation Language which deals with data manipulation and includes most common SQL statements such SELECT, INSERT, UPDATE, DELETE, etc., and it is used to store, modify, retrieve, delete and update data in a database.
➤INSERT
➤UPDATE
➤DELETE
➤MERGE
NOTE:- (ORACLE DO NOT FIRE AUTOCOMMIT FOR ANY DML STATEMENT)
TCL :- TRANSACTION CONTROL LANGUAGE
➤COMMIT
➤ROLLBACK
SQL> SELECT * FROM TAB;
TNAME TABTYPE CLUSTERID
------------------------------ ------- ----------
COUNTRIES TABLE
DEPARTMENTS TABLE
DEPT TABLE
E TABLE
EMPLOYEES TABLE
EMP_DETAILS_VIEW VIEW
JOBS TABLE
JOB_HISTORY TABLE
LOCATIONS TABLE
REGIONS TABLE
10 rows selected.
SQL> DESC E
Name Null? Type
----------------------------------------- -------- ----------------------------
EMPID NUMBER(10)
FIRST_NAME VARCHAR2(15)
SALARY NUMBER(7,2)
➥Insert Query:-
SQL> INSERT INTO E VALUES(1,'RAM',3000);
1 row created.
SQL> INSERT INTO E VALUES(1,'KRISH',NULL);
1 row created.
SQL> INSERT INTO E(EMPID,SALARY) VALUES(2,3000);
1 row created.
SQL> DESC E
Name Null? Type
----------------------------------------- -------- ----------------------------
EMPID NUMBER(10)
FIRST_NAME VARCHAR2(15)
SALARY NUMBER(7,2)
SQL> INSERT INTO E (SELECT * FROM EMPLOYEES);
INSERT INTO E (SELECT * FROM EMPLOYEES)
*
ERROR at line 1:
ORA-00913: too many values
SQL> DESC E
Name Null? Type
----------------------------------------- -------- ----------------------------
EMPID NUMBER(10)
FIRST_NAME VARCHAR2(15)
SALARY NUMBER(7,2)
SQL> DESC EMPLOYEES
Name Null? Type
----------------------------------------- -------- ----------------------------
EMPLOYEE_ID NOT NULL NUMBER(6)
FIRST_NAME VARCHAR2(20)
LAST_NAME NOT NULL VARCHAR2(25)
EMAIL NOT NULL VARCHAR2(25)
PHONE_NUMBER VARCHAR2(20)
HIRE_DATE NOT NULL DATE
JOB_ID NOT NULL VARCHAR2(10)
SALARY NUMBER(8,2)
COMMISSION_PCT NUMBER(2,2)
MANAGER_ID NUMBER(6)
DEPARTMENT_ID NUMBER(4)
SQL> INSERT INTO E (SELECT EMPLOYEE_ID,LAST_NAME,SALARY FROM EMPLOYEES WHERE DEPARTMENT_ID=30);
6 rows created.
SQL> SELECT * FROM E;
EMPID FIRST_NAME SALARY
---------- --------------- ----------
1 RAM 3000
1 KRISH
2 3000
114 Raphaely 11000
115 Khoo 3100
116 Baida 2900
117 Tobias 2800
118 Himuro 2600
119 Colmenares 2500
9 rows selected.
SQL> ROLLBACK;
Rollback complete.
SQL> SELECT * FROM E;
no rows selected
SQL> INSERT INTO E (SELECT EMPLOYEE_ID,LAST_NAME,SALARY FROM EMPLOYEES WHERE DEPARTMENT_ID=30);
6 rows created.
SQL> SELECT * FROM E;
EMPID FIRST_NAME SALARY
---------- --------------- ----------
114 Raphaely 11000
115 Khoo 3100
116 Baida 2900
117 Tobias 2800
118 Himuro 2600
119 Colmenares 2500
6 rows selected.
SQL> COMMIT;
Commit complete.
SQL> ROLLBACK;
Rollback complete.
SQL> SELECT * FROM E;
EMPID FIRST_NAME SALARY
---------- --------------- ----------
114 Raphaely 11000
115 Khoo 3100
116 Baida 2900
117 Tobias 2800
118 Himuro 2600
119 Colmenares 2500
6 rows selected.
➥Update queries:-
SQL> UPDATE E SET SALARY=12000;
6 rows updated.
SQL> SELECT * FROM E;
EMPID FIRST_NAME SALARY
---------- --------------- ---------
114 Raphaely 12000 115 Khoo 12000 116 Baida 12000 117 Tobias 12000
118 Himuro 12000
119 Colmenares 12000
6 rows selected.
SQL> ROLLBACK;
Rollback complete.
SQL> SELECT * FROM E;
EMPID FIRST_NAME SALARY
---------- --------------- ----------
114 Raphaely 11000
115 Khoo 3100
116 Baida 2900
117 Tobias 2800
118 Himuro 2600
119 Colmenares 2500
6 rows selected.
SQL> UPDATE E SET SALARY=12000 WHERE EMPID=114;
1 row updated.
SQL> SELECT * FROM E;
EMPID FIRST_NAME SALARY
---------- --------------- ----------
114 Raphaely 12000
115 Khoo 3100
116 Baida 2900
117 Tobias 2800
118 Himuro 2600
119 Colmenares 2500
6 rows selected.
SQL> COMMIT;
Commit complete.
SQL>
SQL> DELETE FROM E;
6 rows deleted.
SQL> SELECT * FROM E;
no rows selected
SQL> ROLLBACK;
Rollback complete.
SQL> SELECT * FROM E;
EMPID FIRST_NAME SALARY
---------- --------------- ----------
114 Raphaely 12000
115 Khoo 3100
116 Baida 2900
117 Tobias 2800
118 Himuro 2600
119 Colmenares 2500
6 rows selected.
SQL> DELETE FROM E WHERE EMPID=119;
1 row deleted.
SQL> COMMIT;
Commit complete.
SQL>
SQL> SELECT * FROM E;
EMPID FIRST_NAME SALARY
---------- --------------- ----------
114 Raphaely 12000
115 Khoo 3100
116 Baida 2900
117 Tobias 2800
118 Himuro 2600
SQL> CREATE TABLE DUP AS SELECT * FROM E;
Table created.
SQL> SELECT * FROM DUP;
EMPID FIRST_NAME SALARY
---------- --------------- ----------
114 Raphaely 12000
115 Khoo 3100
116 Baida 2900
117 Tobias 2800
118 Himuro 2600
SQL> INSERT INTO E VALUES(119,'Ram',3000);
1 row created.
SQL> UPDATE E SET SALARY=SALARY+3000,FIRST_NAME='Krish' WHERE EMPID=115;
1 row updated.
SQL> SELECT * FROM E;
EMPID FIRST_NAME SALARY
---------- --------------- ----------
114 Raphaely 12000
115 Krish 6100
116 Baida 2900
117 Tobias 2800
118 Himuro 2600
119 Ram 3000
6 rows selected.
SQL> COMMIT;
Commit complete.
SQL> SELECT * FROM DUP;
EMPID FIRST_NAME SALARY
---------- --------------- ----------
114 Raphaely 12000
115 Khoo 3100
116 Baida 2900
117 Tobias 2800
118 Himuro 2600
SQL> MERGE INTO DUP USING E ON(E.EMPID=DUP.EMPID)
2 WHEN MATCHED THEN
3 UPDATE SET
4 DUP.FIRST_NAME=E.FIRST_NAME,
5 DUP.SALARY=E.SALARY
6 WHEN NOT MATCHED THEN
7 INSERT VALUES(E.EMPID,E.FIRST_NAME,E.SALARY);
6 rows merged.
SQL> commit;
Commit complete.
SQL> select * from e;
EMPID FIRST_NAME SALARY
---------- --------------- ----------
114 Raphaely 12000
115 Krish 6100
116 Baida 2900
117 Tobias 2800
118 Himuro 2600
119 Ram 3000
6 rows selected.
SQL> select * from dup;
EMPID FIRST_NAME SALARY
---------- --------------- ----------
114 Raphaely 12000
115 Krish 6100
116 Baida 2900
117 Tobias 2800
118 Himuro 2600
119 Ram 3000
6 rows selected.
SQL> COMMIT;
Comments
Post a Comment