by 구루비스터디 Query Transforming 이행성규칙 Vieew Merging 뷰병합 [2013.09.11]
예)
① sales_qty > 1200/12
② sales_qty > 100
③ sales_qty*12 > 1200
①,② 는 동일하게 취급 - 사전에 최대한의 연산을 하여 두 번째 조건식이 됨
③은 연산을 하여 ② 조건식을 만들지 않음 - 비교연산자의 추가적 연산은 연산자를 좌우로 이동시켜 단순화를 하지는 않기 때문
예)
① job IN ('CLERK','MANAGER')
② job = 'CLERK' OR job = 'MANAGER'
예)
① sales_qty > ANY (:in_qty1, :in_qty2)
② sales_qty > :in_qty1 OR sales_qty > :in_qty2
예)
① where 100000 > ANY (select sal from emp where job = 'CLERK')
② where EXISTS (select sal from emp where job = 'CLERK' and 100000 > sal)
예)
① sales_qty > ALL (:in_qty1, :in_qty2)
② sales_qty > :in_qty1 AND sales_qty > :in_qty2
예)
① WHERE 100000 > ALL (SELECT sal FROM emp WHERE job = 'CLERK')
② WHERE NOT (100000 <= ALL (SELECT sal FROM emp WHERE job = 'CLERK'))
③ WHERE NOT EXISTS (SELECT sal FROM emp WHERE job = 'CLERK' AND 100000 <= sal)
예)
① sales_qty BETWEEN 100 and 200
② sales_qty >= 100 AND sales_qty <= 200
예)
① NOT (sal < 30000 OR comm IS NULL)
② NOT sal < 30000 AND comm IS NOT NULL)
③ sal >= 30000 AND comm IS NOT NULL
① → ② → ③ 순서로 변환
예)
① NOT deptno = ( SELECT deptno FROM emp WHERE empno = 7689)
② deptno <> (SELECT deptno FROM emp WHERE empno = 7689)
WHERE column1 comparison_operators constant
AND column1 = column2
예)
select * from emp where job = 'CLERK' OR deptno = 10 ;
select * from emp where job = 'CLERK'
UNION ALL
SELECT * from emp where deptno = 10 and job <> 'CLERK';
예)
CREATE VIEW emp_10 (e_no, e_name, job, manager, hire_date, salary, commission, deptno) AS
SELECT empno, ename, job, mgr, hiredate, sal, comm, deptno
FROM emp
WHERE deptno= 10;
- 액세스 쿼리
SELECT em_no, e_name, salary, hire_date
FROM emp_10
WHERE salary >10000;
- SQL 병합된 쿼리
SELECT empno, ename, sal, hiredate
FROM emp
WHERE deptnl = 10
AND sal > 10000
예)
CREATE VIEW emp_union_view(e_no,e_name, job, mgr, hiredate,sal, comm,deptno) AS
SELECT empno, ename, job, mgr, hiredate, sal, comm, deptno FROM regular_emp
UNION ALL
SELECT empno, ename, job, magager, hiredate, salary, comm, 90 FROM temporary_emp;
- 액세스 쿼리
SELECT e_no, e_name, mgr,sal
FROM emp_union_view
WHERE deptnl = 20;
- 변환된 쿼리
SELECT empno, ename, mgr, sal
FROM (SELECT empno, ename, ggr, sal FROM regular_emp WHERE deptno = 20
UNION ALL
SELECT empno, ename, magager, salary FROM temporary_emp WHERE 90 = 20);
예)
CREATE VIEW emp_group_by_deptno AS
SELECT deptno, AVG(sal) avg_sal, MIN(sal) min_sal, MAX(sal) max_sal
FROM emp
GROUP BY deptno;
- 액세스 쿼리
SELECT *
FROM emp_group_by_deptno
WHERE deptno = 10;
- 변환된 쿼리
SELECT deptno, AVG(sal) avg_sal, MIN(sal) min_sal, MAX(sal) max_sal
FROM emp
WHERE deptno = 10
GROUP BY deptno;
예)
- 액세스 쿼리
SELECT emp.ename, emp.sal
FROM emp, dept
WHERE (emp.deptno, emp.sal) IN (SELECT deptno, avg_sal FROM emp_group_by_deptno)
AND emp.deptno = dept.deptno
AND dept.oc = 'London' ;
- 변환된 쿼리
SELECT e1.ename, e1.sal
FROM emp e1, dept d, emp e2
WHERE e1.deptno = d.deptno
AND d.loc = 'London'
AND e1.deptno = e2.deptno
GROUP BY e1.rowid, d.rowid, e1.ename, e1.dalary
HAVING e1.sal = AVG(e2.sal) ;
- 강좌 URL : http://www.gurubee.net/lecture/2610
- 구루비 강좌는 개인의 학습용으로만 사용 할 수 있으며, 다른 웹 페이지에 게재할 경우에는 출처를 꼭 밝혀 주시면 고맙겠습니다.~^^
- 구루비 강좌는 서비스 제공을 위한 목적이나, 학원 홍보, 수익을 얻기 위한 용도로 사용 할 수 없습니다.