DBMS EXP 6
DBMS EXP 6
deptno NUMBER(2,0),
dname VARCHAR2(14),
loc VARCHAR2(13),
CONSTRAINT pk_dept PRIMARY KEY (deptno)
);
Create the EMP table which has a foreign key reference to the DEPT table. The
foreign key will require that the DEPTNO in the EMP table exist in the DEPTNO
column in the DEPT table.
Insert EMP row, using TO_DATE function to cast string literal into an oracle DATE
format.
insert into emp values( 7876, 'ADAMS', 'CLERK', 7788, to_date('13-JUL-87', 'dd-
mm-rr') - 51, 1100, null, 20);
The ANY and ALL operators allow you to perform a comparison between a
single
column value and a range of other values.
Select ename,sal,deptno from emp where deptno=20 and sal>any(select sal from
emp where deptno=30);
UNION: The UNION operator is used to combine the result-set of two or more
SELECT
statements.
illustrate Union
UNION ALL: The UNION ALL command combines the result set of two or more
SELECT statements (allows duplicate values).
Select deptno from emp union all select deptno from dept;
Select job from emp where deptno=10 union all select job from emp where
deptno=20;
INTERSECT: The INTERSECT operator in SQL is used to retrieve the records that
are identical/common between the result sets of two or more tables.
Select job from emp where deptno=10 intersect select job from emp where
deptno=20 union select job from emp where deptno=30;
Select *from emp where rownum<11 minus select *from emp where rownum<10;
Select job from emp where deptno=30 minus select job from emp where
deptno=10;
JOINS
Equi join: Display the details of an employee along with their department
name and
Location
Select empno, ename, emp. deptno, dname, loc from emp, dept where emp.
Deptno = dept.deptno(+);
Left outer join: Returns all records from the left table, and the matched records
from the right
Table
Select e.empno,e.ename,d.deptno,d.dname,d.loc from dept d left outer join emp e
on(e.deptno=d.deptno);
INNER JOIN: Returns records that have matching values in both tables
SELECT ename, dname, job, empno, hiredate, loc FROM emp INNER JOIN dept ON
emp.deptno = dept.deptno ORDER BY ename;
LEFT JOIN: The LEFT JOIN keyword returns all records from the left table (table1),
and the matching records from the right table (table2). The result is 0 records from
the right side, if there is no match.
SELECT ename, dname, job, empno, hiredate, loc FROM emp LEFT JOIN dept ON
emp.deptno = dept.deptno;
RIGHT JOIN : The RIGHT JOIN keyword returns all records from the right table
(table2), and the matching records from the left table (table1). The result is 0
records from the left side, if there is no match.
SELECT ename, dname, job, empno, hiredate, loc FROM emp RIGHT JOIN dept ON
emp.deptno = dept.deptno;
FULL JOIN: The FULL JOIN keyword returns all records when there is a match in
left (table1)
or right (table2) table records.
SELECT ename, dname, job, empno, hiredate, loc FROM emp FULL JOIN dept ON
emp.deptno = dept.deptno;
Simple natural join between DEPT and EMP tables based on the primary
key of the DEPT table DEPTNO, and the DEPTNO foreign key in the EMP
table.
Select ename, dname, job, empno, hiredate, loc from emp, dept where
emp.deptno =
dept.deptno order by ename;
desc Employee;
UNIQUE: When a tables column is defined with a unique constraint that column
will not accept
any duplicate values.
desc Emplo;
CHECK: It is used to check the data before entering into the table column.It is
used to maintain validity integrity.
desc Emplol;
desc deptl;
desc emplo3;