Ex. No: 2 Performing Insertion, Deletion, Modifying, Altering, Updating and Viewing records based
on conditions.
AIM
To study the various categories of DML commands.
DESCRIPTION
 THE ORACLE TABLE – DUAL - Dual is a small oracle table which consists of only one
row and one column and contains the value X in that column.
 INSERT - This command is used to insert values into the table.
 SELECT - This command is used to display the contents of the table or those of a particular
column.
 RENAME - This command renames the name of the table.
 ARITHMETIC OPERATIONS - Various operations such as addition, multiplication,
subtraction and division can be performed using the numbers available in the table.
 DISTINCT - This keyword is used along with select keyword to display unique values from
the specified column. It avoids duplicates during display.
 ORDER BY CLAUSE - The order by clause arranges the contents of the table in ascending
order (by default) or in descending order (if specified explicitly) according to the specified
column.
 CONCATENATION OPERATOR - This combines information from two or more columns in
a sentence according to the format specified.
LOGICAL OPERATORS
 AND : The oracle engine will process all rows in a table and displays the result only when all
of the conditions specified using the AND operator are specified.
 OR : The oracle engine will process all rows in a table and displays the result only when any
of the conditions specified using the OR operators are satisfied.
 NOT : The oracle engine will process all rows in a table and displays the result only when
none of the conditions specified using the NOT operator are specified.
 BETWEEN : In order to select data that is within a range of values, the between operator is
used. (AND should be included)
PATTERN MATCH
 LIKE PREDICATE :The use of like predicate is that it allows the comparison of one string
value with another string value, which is not identical. This is achieved by using wildcard
characters which are % and _. The purpose of % is that it matches any string and _ matches
any single character.
 IN AND NOT IN PREDICATE :The arithmetic operator = compares a single value to
another single value. In case a value needs to be compared to a list of values then the in
predicate is used. The not in predicate is the opposite of the in predicate. This will select all
the rows whose values do not match all of the values in the list.
AGGREGATE FUNCTIONS
 AVG(N): It returns average value of n ignoring null values.
 MIN (EXPR): It returns minimum value of the expression.
 COUNT (EXPR):It returns the number of rows where expression is not null.
 COUNT (*): It returns the number of rows in the table including the duplicates and those with
null values.
 MAX (EXPR): It returns maximum value
DATE FUNCTIONS
 SYSDATE : The sysdate is a pseudo column that contains the current date and time. It
requires no arguments when selected from the table dual and returns the current date.
GROUP BYCLAUSE
The group by clause is another section of the select statement. This optional class tells oracle to group
rows based on distinct values that exists for specified columns.
HAVING CLAUSE
The having clause can be used in conjunction with the group by clause. Having imposes a condition
on the group by clause, which further filters the groups created by the group by clause.
SQL> create table EMP (EMPNO number(4) not null,
2 ENAME varchar2(30) not null,
3 JOB varchar2(10),
4 MGR number(4),
5 HIREDATE date,
6 SAL number(7,2),
7 DEPTNO number(2));
Table created.
INSERT
Type 1
SQL> insert into emp (empno,ename,job,mgr,hiredate,sal,deptno)
2 values (3737,'Priya','Officer','7777','07-mar-2009',34000,07);
1 row created.
Type 2
SQL> insert into emp values (2323,'Anitha','Officer',5454,'09-jan-08',42000,09);
1 row created.
Type 3
SQL> insert into emp values ('&empno','&ename','&job','&mgr','&hiredate','&sal','&deptno');
Enter value for empno: 7575
Enter value for ename: Karthi
Enter value for job: Officer
Enter value for mgr: 3337
Enter value for hiredate: 07-jul-2007
Enter value for sal: 72000
Enter value for deptno: 07
old 1: insert into emp values ('&empno','&ename','&job','&mgr','&hiredate','&sal','&deptno')
new 1: insert into emp values (' 7575','Karthi','Officer','3337','07-jul-2007','72000',' 07')
1 row created.
SQL> insert into emp values (5352,'Retish','secretary','5555','09-jun-2009','20000',08);
1 row created.
SQL> insert into emp values (5332,'Rocky','assit','5555','08-jan-2008',13000,07);
1 row created.
SQL> select * from emp;
EMPNO ENAME JOB MGR HIREDATE SAL DEPTNO
--------- ------------------------------ ---------- ------------------ --------- -----------------------------
3737 Priya Officer 7777 07-MAR-09 34000 7
2323 Anitha Officer 5454 09-JAN-08 42000 9
7575 Karthi Officer 3337 07-JUL-07 72000 7
5352 Retish secretary 5555 09-JUN-09 20000 8
5332 Rocky assit 5555 08-JAN-08 13000 7
UPDATE
TUPLE UPDATE
SQL> update emp set
2 job = 'Manager',deptno=20, sal = sal +3000
3 where ename = 'Karthi';
1 row updated.
SQL> select * from emp;
EMPNO ENAME JOB MGR HIREDATE SAL DEPTNO
--------- ------------------------------ ---------- ------------------ --------- ------------------------------
3737 Priya Officer 7777 07-MAR-09 34000 7
2323 Anitha Officer 5454 09-JAN-08 42000 9
7575 Karthi Manager 3337 07-JUL-07 75000 20
5352 Retish secretary 5555 09-JUN-09 20000 8
5332 Rocky assit 5555 08-JAN-08 13000 7
ROWUPDATE
SQL> update emp set
2 sal = sal * 1.5
3 where deptno = 7;
2 rows updated.
SELECT
SQL> select * from emp;
EMPNO ENAME JOB MGR HIREDATE SAL DEPTNO
--------- ------------------------------ ---------- ------------------ --------- -------------------------------
3737 Priya Officer 7777 07-MAR-09 51000 7
2323 Anitha Officer 5454 09-JAN-08 42000 9
7575 Karthi Manager 3337 07-JUL-07 75000 20
5352 Retish secretary 5555 09-JUN-09 20000 8
5332 Rocky assit 5555 08-JAN-08 19500 7
SQL> select ename from emp;
ENAME
------------------------------
Priya
Anitha
Karthi
Retish
Rocky
SQL> select sal * 2 from emp;
SAL*2
---------
102000
84000
150000
40000
39000
SQL> select ename,sal * 2 from emp;
ENAME SAL*2
------------------------------ ---------
Priya 102000
Anitha 84000
Karthi 150000
Retish 40000
Rocky 39000
SQL> select mgr from emp;
MGR
---------
7777
5454
3337
5555
5555
DISTINCT
SQL> select distinct mgr from emp;
MGR
------------
3337
5454
5555
7777
ORDER BY
SQL> select * from emp order by ename;
EMPNO ENAME JOB MGR HIREDATE SAL DEPTNO
--------- ------------------------------ ---------- ------------------ --------- -----------------------------
2323 Anitha Officer 5454 09-JAN-08 42000 9
7575 Karthi Manager 3337 07-JUL-07 75000 20
3737 Priya Officer 7777 07-MAR-09 51000 7
5352 Retish secretary 5555 09-JUN-09 20000 8
5332 Rocky assit 5555 08-JAN-08 19500 7
SQL> select * from emp order by ename asc;
EMPNO ENAME JOB MGR HIREDATE SAL DEPTNO
--------- ------------------------------ ---------- ------------------ --------- ------------------------------
2323 Anitha Officer 5454 09-JAN-08 42000 9
7575 Karthi Manager 3337 07-JUL-07 75000 20
3737 Priya Officer 7777 07-MAR-09 51000 7
5352 Retish secretary 5555 09-JUN-09 20000 8
5332 Rocky assit 5555 08-JAN-08 19500 7
SQL> select * from emp order by ename desc;
EMPNO ENAME JOB MGR HIREDATE SAL DEPTNO
--------- ------------------------------ ---------- ------------------ --------- ------------------------------
5332 Rocky assit 5555 08-JAN-08 19500 7
5352 Retish secretary 5555 09-JUN-09 20000 8
3737 Priya Officer 7777 07-MAR-09 51000 7
7575 Karthi Manager 3337 07-JUL-07 75000 20
2323 Anitha Officer 5454 09-JAN-08 42000 9
OR
SQL> select ename from emp where ( mgr = 5555 or deptno = 07);
ENAME
------------------------------
Priya
Retish
Rocky
AND
SQL> select ename from emp where ( mgr = 5555 and deptno = 07);
ENAME
------------------------------
Rocky
IN
SQL> select ename,sal
2 from emp
3 where empno in
4 (2323,5555);
ENAME SAL
------------------------------ ---------
Anitha 42000
SQL> select ename,sal
2 from emp
3 where empno in
4 (2323,7575);
ENAME SAL
------------------------------ ---------
Anitha 42000
Karthi 75000
BETWEEN
SQL> select ename,sal
2 from emp
3 where salbetween 25000 and 50000;
ENAME SAL
------------------------------ ---------
Anitha 42000
TUPLE DELETION
SQL> delete from emp where empno=2323;
1 row deleted.
SQL> select * from emp;
EMPNO ENAME JOB MGR HIREDATE SAL DEPTNO
--------- ------------------------------ ---------- ------------------ --------- -------------------------------
3737 Priya Officer 7777 07-MAR-09 51000 7
7575 Karthi Manager 3337 07-JUL-07 75000 20
5352 Retish secretary 5555 09-JUN-09 20000 8
5332 Rocky assit 5555 08-JAN-08 19500 7
Another Example
Table Name studs
SNAME SID SAGE SAREA SDEPT SPOCKET
ashwin 101 19 anna nagar aeronautical 750
bhavesh 102 18 nungambakkam marine 500
pruthvik 103 20 anna nagar aerospace 250
charith 104 20 kilpauk mechanical 100
CONCATENATION OPERATOR
SQL> select sname || ' is a ' || sdept || ' engineer. ' AS "PROFESSION" from studs;
PROFESSION
ashwin is a aeronautical engineer.
bhavesh is a marine engineer.
pruthvik is a aerospace engineer.
charith is a mechanical engineer.
USING THE WHERE CLAUSE
SQL> select sname,sage from studs where sage<=19;
SNAME SAGE
ashwin 19
PATTERN MATCHING
SQL> select sname, sarea from studs where sarea like '%g%';
SNAME SAREA
ashwin anna nagar
bhavesh nungambakkam
pruthvik anna nagar
NOT IN PREDICATE
SQL> select sname, sid from studs where sid not in(102,104);
SNAME SID
ashwin 101
pruthvik 103
AGGREGATE FUNCTIONS
SQL> select avg( spocket ) result from studs;
RESULT
400
SQL> select min(spocket) result from studs;
RESULT
100
SQL> select count(spocket) result from studs;
RESULT
4
SQL> select count(*) result from studs;
RESULT
4
SQL> select count(spocket) result from studs where sarea='anna nagar';
RESULT
2
SQL> select max(spocket) result from studs;
RESULT
750
SQL> select sum(spocket) result from studs;
RESULT
1600
DATE FUNCTIONS
SQL> select sysdate from dual;
SYSDATE
16-JUL-08
GROUP BYCLAUSE
SQL> select sarea,sum(spocket) result from studs group by sarea;
SAREA RESULT
anna nagar 1000
nungambakkam 500
kilpauk 100
HAVING CLAUSE
SQL> select sarea,sum(spocket) result from studs group by sarea having spocket<600;
SAREA RESULT
nungambakkam 500
kilpauk 100
RESULT
The DML commands were executed and the output was verified.

It6312 dbms lab-ex2

  • 1.
    Ex. No: 2Performing Insertion, Deletion, Modifying, Altering, Updating and Viewing records based on conditions. AIM To study the various categories of DML commands. DESCRIPTION  THE ORACLE TABLE – DUAL - Dual is a small oracle table which consists of only one row and one column and contains the value X in that column.  INSERT - This command is used to insert values into the table.  SELECT - This command is used to display the contents of the table or those of a particular column.  RENAME - This command renames the name of the table.  ARITHMETIC OPERATIONS - Various operations such as addition, multiplication, subtraction and division can be performed using the numbers available in the table.  DISTINCT - This keyword is used along with select keyword to display unique values from the specified column. It avoids duplicates during display.  ORDER BY CLAUSE - The order by clause arranges the contents of the table in ascending order (by default) or in descending order (if specified explicitly) according to the specified column.  CONCATENATION OPERATOR - This combines information from two or more columns in a sentence according to the format specified. LOGICAL OPERATORS  AND : The oracle engine will process all rows in a table and displays the result only when all of the conditions specified using the AND operator are specified.  OR : The oracle engine will process all rows in a table and displays the result only when any of the conditions specified using the OR operators are satisfied.  NOT : The oracle engine will process all rows in a table and displays the result only when none of the conditions specified using the NOT operator are specified.  BETWEEN : In order to select data that is within a range of values, the between operator is used. (AND should be included) PATTERN MATCH  LIKE PREDICATE :The use of like predicate is that it allows the comparison of one string value with another string value, which is not identical. This is achieved by using wildcard characters which are % and _. The purpose of % is that it matches any string and _ matches any single character.
  • 2.
     IN ANDNOT IN PREDICATE :The arithmetic operator = compares a single value to another single value. In case a value needs to be compared to a list of values then the in predicate is used. The not in predicate is the opposite of the in predicate. This will select all the rows whose values do not match all of the values in the list. AGGREGATE FUNCTIONS  AVG(N): It returns average value of n ignoring null values.  MIN (EXPR): It returns minimum value of the expression.  COUNT (EXPR):It returns the number of rows where expression is not null.  COUNT (*): It returns the number of rows in the table including the duplicates and those with null values.  MAX (EXPR): It returns maximum value DATE FUNCTIONS  SYSDATE : The sysdate is a pseudo column that contains the current date and time. It requires no arguments when selected from the table dual and returns the current date. GROUP BYCLAUSE The group by clause is another section of the select statement. This optional class tells oracle to group rows based on distinct values that exists for specified columns. HAVING CLAUSE The having clause can be used in conjunction with the group by clause. Having imposes a condition on the group by clause, which further filters the groups created by the group by clause.
  • 3.
    SQL> create tableEMP (EMPNO number(4) not null, 2 ENAME varchar2(30) not null, 3 JOB varchar2(10), 4 MGR number(4), 5 HIREDATE date, 6 SAL number(7,2), 7 DEPTNO number(2)); Table created. INSERT Type 1 SQL> insert into emp (empno,ename,job,mgr,hiredate,sal,deptno) 2 values (3737,'Priya','Officer','7777','07-mar-2009',34000,07); 1 row created. Type 2 SQL> insert into emp values (2323,'Anitha','Officer',5454,'09-jan-08',42000,09); 1 row created. Type 3 SQL> insert into emp values ('&empno','&ename','&job','&mgr','&hiredate','&sal','&deptno'); Enter value for empno: 7575 Enter value for ename: Karthi Enter value for job: Officer Enter value for mgr: 3337 Enter value for hiredate: 07-jul-2007 Enter value for sal: 72000 Enter value for deptno: 07 old 1: insert into emp values ('&empno','&ename','&job','&mgr','&hiredate','&sal','&deptno') new 1: insert into emp values (' 7575','Karthi','Officer','3337','07-jul-2007','72000',' 07')
  • 4.
    1 row created. SQL>insert into emp values (5352,'Retish','secretary','5555','09-jun-2009','20000',08); 1 row created. SQL> insert into emp values (5332,'Rocky','assit','5555','08-jan-2008',13000,07); 1 row created. SQL> select * from emp; EMPNO ENAME JOB MGR HIREDATE SAL DEPTNO --------- ------------------------------ ---------- ------------------ --------- ----------------------------- 3737 Priya Officer 7777 07-MAR-09 34000 7 2323 Anitha Officer 5454 09-JAN-08 42000 9 7575 Karthi Officer 3337 07-JUL-07 72000 7 5352 Retish secretary 5555 09-JUN-09 20000 8 5332 Rocky assit 5555 08-JAN-08 13000 7 UPDATE TUPLE UPDATE SQL> update emp set 2 job = 'Manager',deptno=20, sal = sal +3000 3 where ename = 'Karthi'; 1 row updated. SQL> select * from emp; EMPNO ENAME JOB MGR HIREDATE SAL DEPTNO --------- ------------------------------ ---------- ------------------ --------- ------------------------------ 3737 Priya Officer 7777 07-MAR-09 34000 7 2323 Anitha Officer 5454 09-JAN-08 42000 9 7575 Karthi Manager 3337 07-JUL-07 75000 20
  • 5.
    5352 Retish secretary5555 09-JUN-09 20000 8 5332 Rocky assit 5555 08-JAN-08 13000 7 ROWUPDATE SQL> update emp set 2 sal = sal * 1.5 3 where deptno = 7; 2 rows updated. SELECT SQL> select * from emp; EMPNO ENAME JOB MGR HIREDATE SAL DEPTNO --------- ------------------------------ ---------- ------------------ --------- ------------------------------- 3737 Priya Officer 7777 07-MAR-09 51000 7 2323 Anitha Officer 5454 09-JAN-08 42000 9 7575 Karthi Manager 3337 07-JUL-07 75000 20 5352 Retish secretary 5555 09-JUN-09 20000 8 5332 Rocky assit 5555 08-JAN-08 19500 7 SQL> select ename from emp; ENAME ------------------------------ Priya Anitha Karthi Retish Rocky SQL> select sal * 2 from emp; SAL*2
  • 6.
    --------- 102000 84000 150000 40000 39000 SQL> select ename,sal* 2 from emp; ENAME SAL*2 ------------------------------ --------- Priya 102000 Anitha 84000 Karthi 150000 Retish 40000 Rocky 39000 SQL> select mgr from emp; MGR --------- 7777 5454 3337 5555 5555 DISTINCT SQL> select distinct mgr from emp; MGR
  • 7.
    ------------ 3337 5454 5555 7777 ORDER BY SQL> select* from emp order by ename; EMPNO ENAME JOB MGR HIREDATE SAL DEPTNO --------- ------------------------------ ---------- ------------------ --------- ----------------------------- 2323 Anitha Officer 5454 09-JAN-08 42000 9 7575 Karthi Manager 3337 07-JUL-07 75000 20 3737 Priya Officer 7777 07-MAR-09 51000 7 5352 Retish secretary 5555 09-JUN-09 20000 8 5332 Rocky assit 5555 08-JAN-08 19500 7 SQL> select * from emp order by ename asc; EMPNO ENAME JOB MGR HIREDATE SAL DEPTNO --------- ------------------------------ ---------- ------------------ --------- ------------------------------ 2323 Anitha Officer 5454 09-JAN-08 42000 9 7575 Karthi Manager 3337 07-JUL-07 75000 20 3737 Priya Officer 7777 07-MAR-09 51000 7 5352 Retish secretary 5555 09-JUN-09 20000 8 5332 Rocky assit 5555 08-JAN-08 19500 7 SQL> select * from emp order by ename desc; EMPNO ENAME JOB MGR HIREDATE SAL DEPTNO --------- ------------------------------ ---------- ------------------ --------- ------------------------------ 5332 Rocky assit 5555 08-JAN-08 19500 7 5352 Retish secretary 5555 09-JUN-09 20000 8
  • 8.
    3737 Priya Officer7777 07-MAR-09 51000 7 7575 Karthi Manager 3337 07-JUL-07 75000 20 2323 Anitha Officer 5454 09-JAN-08 42000 9 OR SQL> select ename from emp where ( mgr = 5555 or deptno = 07); ENAME ------------------------------ Priya Retish Rocky AND SQL> select ename from emp where ( mgr = 5555 and deptno = 07); ENAME ------------------------------ Rocky IN SQL> select ename,sal 2 from emp 3 where empno in 4 (2323,5555); ENAME SAL ------------------------------ --------- Anitha 42000 SQL> select ename,sal 2 from emp 3 where empno in
  • 9.
    4 (2323,7575); ENAME SAL --------------------------------------- Anitha 42000 Karthi 75000 BETWEEN SQL> select ename,sal 2 from emp 3 where salbetween 25000 and 50000; ENAME SAL ------------------------------ --------- Anitha 42000 TUPLE DELETION SQL> delete from emp where empno=2323; 1 row deleted. SQL> select * from emp; EMPNO ENAME JOB MGR HIREDATE SAL DEPTNO --------- ------------------------------ ---------- ------------------ --------- ------------------------------- 3737 Priya Officer 7777 07-MAR-09 51000 7 7575 Karthi Manager 3337 07-JUL-07 75000 20 5352 Retish secretary 5555 09-JUN-09 20000 8 5332 Rocky assit 5555 08-JAN-08 19500 7
  • 10.
    Another Example Table Namestuds SNAME SID SAGE SAREA SDEPT SPOCKET ashwin 101 19 anna nagar aeronautical 750 bhavesh 102 18 nungambakkam marine 500 pruthvik 103 20 anna nagar aerospace 250 charith 104 20 kilpauk mechanical 100 CONCATENATION OPERATOR SQL> select sname || ' is a ' || sdept || ' engineer. ' AS "PROFESSION" from studs; PROFESSION ashwin is a aeronautical engineer. bhavesh is a marine engineer. pruthvik is a aerospace engineer. charith is a mechanical engineer. USING THE WHERE CLAUSE SQL> select sname,sage from studs where sage<=19; SNAME SAGE ashwin 19 PATTERN MATCHING SQL> select sname, sarea from studs where sarea like '%g%'; SNAME SAREA ashwin anna nagar bhavesh nungambakkam pruthvik anna nagar NOT IN PREDICATE SQL> select sname, sid from studs where sid not in(102,104);
  • 11.
    SNAME SID ashwin 101 pruthvik103 AGGREGATE FUNCTIONS SQL> select avg( spocket ) result from studs; RESULT 400 SQL> select min(spocket) result from studs; RESULT 100 SQL> select count(spocket) result from studs; RESULT 4 SQL> select count(*) result from studs; RESULT 4 SQL> select count(spocket) result from studs where sarea='anna nagar'; RESULT 2 SQL> select max(spocket) result from studs; RESULT 750 SQL> select sum(spocket) result from studs; RESULT 1600 DATE FUNCTIONS SQL> select sysdate from dual;
  • 12.
    SYSDATE 16-JUL-08 GROUP BYCLAUSE SQL> selectsarea,sum(spocket) result from studs group by sarea; SAREA RESULT anna nagar 1000 nungambakkam 500 kilpauk 100 HAVING CLAUSE SQL> select sarea,sum(spocket) result from studs group by sarea having spocket<600; SAREA RESULT nungambakkam 500 kilpauk 100 RESULT The DML commands were executed and the output was verified.