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

Data Manipulation Language (DML)

This document summarizes key Data Manipulation Language (DML) commands in Oracle including INSERT, UPDATE, DELETE, and MERGE. The INSERT command is used to insert new rows into tables. The UPDATE command modifies existing rows in tables. The DELETE command removes rows from tables. The MERGE command combines INSERT, UPDATE, and DELETE functionality by merging data from one table into another based on a join condition.

Uploaded by

asiflisten
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
93 views

Data Manipulation Language (DML)

This document summarizes key Data Manipulation Language (DML) commands in Oracle including INSERT, UPDATE, DELETE, and MERGE. The INSERT command is used to insert new rows into tables. The UPDATE command modifies existing rows in tables. The DELETE command removes rows from tables. The MERGE command combines INSERT, UPDATE, and DELETE functionality by merging data from one table into another based on a join condition.

Uploaded by

asiflisten
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

1

DATA MANIPULATION LANGUAGE (DML): Insertion of new rows, modifications to existing rows and removing unwanted rows collectively known as DML. This language includes INSERT, UPDATE, DELETE and MERGE (introduced from Oracle 9i) commands. 1. INSERT: This DML command is used to insert a new row in an existing table. It supports to insert the rows into an existing table in 3 ways; I) Inserting 1 row at a time in 1 table II) Inserting multiple rows at a time in 1 table III) Inserting multiple rows at a time in multiple tables INSERT ONE ROW ONE TABLE: It will allow inserting 1 row at a time in 1 table with complete data or partial data thru constant values or thru variables. When data related to text type and date type is provided it should be enclosed in single quotes. When complete data is provided in a table, it is essential that number of columns in a table should get match with number of values in a sequence. Syntax: INSERT INTO table_name [(list_of_cols)] VALUES (list_of_values); SQL> INSERT INTO EMP VALUES (1001,'SHAREEF','CLERK', 7788,'10-JAN-83', 2500, 500, 20); SQL> INSERT INTO EMP VALUES (1002,'AHMED','ANALYST', 7698,'25-MAY-82', 3400, NULL, 30); SQL> INSERT INTO EMP VALUES (1003, NULL,'CLERK', NULL,'18-JUN-82', 4000, NULL, 20); INSERTING THE DATA USING VARIABLES: SQL> INSERT INTO EMP VALUES (&EMPNO, &ENAME, &JOB, &MGR, &HIREDATE, &SAL, &COMM, &DEPTNO); Enter value for empno: 1004 Enter value for ename: 'RAJ' Enter value for job: 'CLERK' Enter value for mgr: 7788 Enter value for hiredate: '10-JAN-81' Enter value for Sal: 2300 Enter value for comm: 250 Enter value for deptno: 10 Old 1: INSERT INTO EMP VALUES (&EMPNO, &ENAME, &JOB, &MGR, &HIREDATE, &SAL, &COMM, &DEPTNO) New 1: INSERT INTO EMP VALUES (1004,'RAJ','CLERK', 7788,'10-JAN-81', 2300, 250, 10)

1 row created. SQL> / Enter value for empno: 1005 Enter value for ename: KIRAN Enter value for job: MANAGER Enter value for mgr: 7839 Enter value for hiredate: 17-DEC-82 Enter value for Sal: 4500 Enter value for comm: NULL Enter value for deptno: 30 Old 1: INSERT INTO EMP VALUES (&EMPNO, &ENAME, &JOB, &MGR, &HIREDATE, &SAL, &COMM, &DEPTNO) New 1: INSERT INTO EMP VALUES (1005, 'KIRAN', 'MANAGER', 7839, '17-DEC-82', 4500, NULL, 30) 1 row created. INSERTING PARTIAL VALUES: INSERT INTO EMP (EMPNO, ENAME, SAL) VALUES (1007, 'SUNIL', 6500); INSERT INTO EMP (EMPNO, ENAME, SAL) VALUES (&EMPNO, &ENAME, &SAL); Enter value for empno: 1008 Enter value for ename: RAKESH Enter value for Sal: 4500 INSERT MULTIPLE ROWS 1 TABLE (Writing a sub query under INSERT query): To store multiple rows at a time in a single table, it is essential that INSERT query is associated with SELECT statement. Syntax: INSERT INTO table_name [(list_of_cols)] SELECT query; SQL> INSERT INTO BONUS SELECT ENAME, JOB, SAL, COMM FROM EMP; 14 rows created. SQL> INSERT INTO BONUS (ENAME, SAL) SELECT ENAME, SAL FROM EMP; 14 rows created. SQL> INSERT INTO BONUS SELECT ENAME, JOB, SAL, COMM FROM EMP WHERE DEPTNO=20; 5 rows created. INSERT MULTIPLE ROWS MULTIPLE TABLES: INSERT ALL It is called ETL command (E-Extraction, T-Transformation, L-Loading).

3
This command provides 2 syntaxes. 1. Conditional 2. Unconditional 1. INSERT ALL (with conditional syntax) SYNTAX: INSERT ALL WHEN condition1 THEN INTO table1 [(list_of_cols)] [WHEN condition2 THEN INTO table2 [(list_of_cols)] ................... ELSE INTO table [(list_of_cols)] ] SELECT query; Example:

EMP1 EMP EMP2 EMP3


SQL> INSERT ALL WHEN JOB=MANAGER THEN INTO EMP1 WHEN JOB=CLERK THEN INTO EMP2 ELSE INTO EMP3 SELECT * FROM EMP; 14 rows created. INSERT ALL (UNCONDITIONAL SYNTAX): INSERT ALL INTO table1 [(list_of_cols)] [VALUES (list_of_values)] INTO table2 [(list_of_cols)] [VALUES (list_of_values)] ............ SELECT query; Example:

MANAGERS CLERKS OTHERS

EMP1 EMP EMP2 EMP3

SQL> INSERT ALL INTO EMP1 INTO EMP2 INTO EMP3 SELECT * FROM EMP;

4
42 rows created. SQL> INSERT ALL INTO EMP1 (EMPNO, ENAME, SAL) INTO EMP2 (EMPNO, ENAME, SAL) INTO EMP3 (EMPNO, ENAME, SAL) SELECT EMPNO, ENAME, SAL FROM EMP; 42 rows created. SQL> INSERT ALL INTO EMP1 (EMPNO, ENAME, SAL, DEPTNO) INTO EMP2 (EMPNO, ENAME, SAL, DEPTNO) INTO EMP3 (EMPNO, ENAME, SAL, DEPTNO) SELECT EMPNO, ENAME, SAL, DEPTNO FROM EMP WHERE DEPTNO=20; 15 rows created. UPDATE: This DML command is used to modify the existing rows of a table. It supports to modify single or multiple cols w.r.to single or multiple rows. It supports to modify the data with values or with mathematical expressions. It does not support to use the same column for modification with different data for more than once. Syntax: UPDATE table_name SET col1=value/expr [, col2=value/expr, col3=value/expr, ....] [WHERE condition(s)]; 1. Modify the salary of SMITH from 800 to 1800? SQL> UPDATE EMP SET SAL=1800 WHERE ENAME='SMITH'; 1 row updated. 2. Modify the job of SCOTT from ANALYST to MANAGER and SALARY from 3000 to 4500? SQL> UPDATE EMP SET JOB='MANAGER', SAL=4500 WHERE ENAME='SCOTT'; 1 row updated. 3. Update the salaries of deptno 20 with an increment of 8%? SQL> UPDATE EMP SET SAL=SAL+SAL*8/100 WHERE DEPTNO=20; 5 rows updated. 4. Update the salary of BLAKE with an increment of 1000 and provide the comm with 12% of salary? SQL> UPDATE EMP SET SAL=SAL+1000, COMM=SAL*12/100 WHERE ENAME='BLAKE'; 1 row updated.

5
NOTE: comm has been calculated with 12% on the previous salary, since UPDATE command will modify the columns at a time. SQL> UPDATE EMP SET SAL=SAL+1000, COMM= (SAL+1000)*12/100 WHERE ENAME='BLAKE'; 5. Update the salary of MANAGERS with 12% increment, the salary of CLERKS with 10% increment and others with 8%? SQL> UPDATE EMP SET SAL=DECODE (JOB, 'MANAGER', SAL+SAL*12/100, 'CLERK', SAL+SAL*10/100, SAL+SAL*8/100); Writing a sub query under UPDATE command: A sub query can be written in UPDATE command, where the data returned by subquery will be updated at outer query. SYNTAX: UPDATE table_name SET col1= (SELECT query) [, col2= (SELECT query), ...] [WHERE condition(s)]; 1. Update the job of SMITH with the job of SCOTT? SQL> UPDATE EMP SET JOB=(SELECT JOB FROM EMP WHERE ENAME='SCOTT') WHERE ENAME='SMITH'; 1 row updated. 2. Update the job of FORD with the job of CLARK and salary with the salary of KING? SQL> UPDATE EMP SET JOB=(SELECT JOB FROM EMP WHERE ENAME='CLARK'), SAL= (SELECT SAL FROM EMP WHERE ENAME='KING') WHERE ENAME='FORD'; 3. Update the salaries of deptno 20 with average salary of deptno 30? SQL> UPDATE EMP SET SAL= (SELECT AVG (SAL) FROM EMP WHERE DEPTNO=30) WHERE DEPTNO=20; 5 rows updated. DELETE: This DML command is used to remove unwanted rows from a table. It will support to delete all the rows or supports to delete the rows on conditional basis. It deletes the rows for temporary. Syntax: DELETE FROM table_name [WHERE condition(s)]; 1. Delete all the rows of dept table?

SQL> DELETE FROM DEPT; 2. Delete all those rows of EMP table who are working as CLERK, ANALYST? SQL> DELETE FROM EMP WHERE JOB IN ('CLERK', 'ANALYST'); 5 rows deleted. 3. Delete all those employees who have been hired in the year 81 and they do not earn commission? SQL> DELETE FROM EMP WHERE HIREDATE LIKE '%81' AND COMM IS NULL; 6 rows deleted. WRITING A SUB QUERY UNDER DELETE QUERY A subquery can be written under DELETE command, where the output returned by sub query can be used for comparing the data at outer query, based on it deletes all the rows. Syntax: DELETE FROM table_name WHERE expr operator (SELECT query); 1. Delete all those employees who are working on a same job as of SMITH? SQL> DELETE FROM EMP WHERE JOB= (SELECT JOB FROM EMP WHERE ENAME='SMITH'); 4 rows deleted. 2. Delete those employees whose salary is more than the average salary of deptno 20? SQL> DELETE FROM EMP WHERE SAL> (SELECT AVG (SAL) FROM EMP WHERE DEPTNO=20); 3. Delete all those employees who are working in SALES, RESEARCH departments? SQL> DELETE FROM EMP WHERE DEPTNO IN (SELECT DEPTNO FROM DEPT WHERE DNAME IN ('RESEARCH','SALES')); 11 rows deleted.

MERGE: It is introduced thru Oracle 9i. This command is used to join the data from 2 tables into a single table. This command is also called as ETL command. In Oracle 9i, this command was called as UPSERT, since it executes UPDATE, INSERT commands simultaneously. In Oracle 10g, this command provides execution of INSERT, UPDATE & DELETE commands simultaneously.

7
Oracle 10g will support to write filtering conditions. This command works with the help of 4 Clauses. 1. USING clause 2. ON clause 3. WHEN MATCHED clause 4. WHEN NOT MATCHED clause Syntax: MERGE INTO table1 [table1_alias] USING table2 [table2_alias]/SELECT query ON (condition) WHEN MATCHED THEN UPDATE SET col1=value [, col2=value,..] [Filtering Conditions]/DELETE WHEN NOT MATCHED THEN INSERT [(cols-list)] Values (values list) [Filtering Conditions]; USING clause is used to specify source table. ON clause is used to specify joining condition. WHEN MATCHED clause is used to execute UPDATE & DELETE commands. WHEN NOT MATCHED clause is used to execute INSERT commands.

Example: SQL> CREATE TABLE EMPB AS SELECT EMPNO, ENAME, JOB, SAL FROM EMP WHERE 1=2; Table created. SQL> SELECT * FROM EMPB; No rows selected SQL> INSERT INTO EMPB VALUES (7788,'SCOTT',ANALYST, 1000); 1 row created. SQL> INSERT INTO EMPB VALUES (7369,'SMITH',CLERK, 2000); 1 row created. SQL> INSERT INTO EMPB VALUES (7934,'MILLER',CLERK, 3000); 1 row created. SQL> SELECT * FROM EMPB; EMPNO ENAME JOB 7788 SCOTT SAL ANALYST

1000

8
7369 7934 SMITH CLERK MILLER CLERK 2000 3000

SQL> MERGE INTO EMPB E1 USING EMP E2 ON (E1.EMPNO=E2.EMPNO) WHEN MATCHED THEN UPDATE SET E1.SAL=E2.SAL WHEN NOT MATCHED THEN INSERT VALUES (E2.EMPNO, E2.ENAME, E2.JOB, E2.SAL*12); 14 rows merged. Example 2: SQL> MERGE INTO EMPB E1 USING EMP E2 ON (E1.EMPNO=E2.EMPNO) WHEN MATCHED THEN UPDATE SET E1.SAL=E2.SAL DELETE WHERE E2.JOB=ANALYST WHEN NOT MATCHED THEN INSERT VALUES (E2.EMPNO, E2.ENAME, E2.JOB, E2.SAL*12) WHERE E2.JOB=MANAGER;

You might also like