05.11.2015 Views

Apress.Expert.Oracle.Database.Architecture.9i.and.10g.Programming.Techniques.and.Solutions.Sep.2005

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

394<br />

CHAPTER 10 ■ DATABASE TABLES<br />

ops$tkyte@ORA10G> select d.deptno, d.dname, emp.*<br />

2 from dept_<strong>and</strong>_emp D, table(d.emps) emp<br />

3 /<br />

DEPTNO DNAME EMPNO ENAME JOB MGR HIREDATE SAL COMM<br />

------ ----------- ----- ---------- --------- ----- --------- ----- -----<br />

10 ACCOUNTING 7782 CLARK MANAGER 7839 09-JUN-81 2450<br />

10 ACCOUNTING 7839 KING PRESIDENT 17-NOV-81 5000<br />

10 ACCOUNTING 7934 MILLER CLERK 7782 23-JAN-82 1300<br />

20 RESEARCH 7369 SMITH CLERK 7902 17-DEC-80 800<br />

20 RESEARCH 7566 JONES MANAGER 7839 02-APR-81 2975<br />

20 RESEARCH 7788 SCOTT ANALYST 7566 09-DEC-82 3000<br />

20 RESEARCH 7876 ADAMS CLERK 7788 12-JAN-83 1100<br />

20 RESEARCH 7902 FORD ANALYST 7566 03-DEC-81 3000<br />

30 SALES 7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300<br />

30 SALES 7521 WARD SALESMAN 7698 22-FEB-81 1250 500<br />

30 SALES 7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400<br />

30 SALES 7698 BLAKE MANAGER 7839 01-MAY-81 2850<br />

30 SALES 7844 TURNER SALESMAN 7698 08-SEP-81 1500 0<br />

30 SALES 7900 JAMES CLERK 7698 03-DEC-81 950<br />

14 rows selected.<br />

We are able to cast the EMPS column as a table <strong>and</strong> it naturally did the join for us—no join<br />

conditions were needed. In fact, since our EMP type doesn’t have the DEPTNO column, there is<br />

nothing for us apparently to join on. <strong>Oracle</strong> takes care of that nuance for us.<br />

So, how can we update the data? Let’s say we want to give department 10 a $100 bonus.<br />

We would code the following:<br />

ops$tkyte@ORA10G> update<br />

2 table( select emps<br />

3 from dept_<strong>and</strong>_emp<br />

4 where deptno = 10<br />

5 )<br />

6 set comm = 100<br />

7 /<br />

3 rows updated.<br />

Here is where the “virtually a table for every row” comes into play. In the SELECT predicate<br />

shown earlier, it may not have been obvious that there was a table per row; especially since the<br />

joins <strong>and</strong> such aren’t there, it looks a little like “magic.” The UPDATE statement, however, shows<br />

that there is a table per row. We selected a discrete table to UPDATE—this table has no name,<br />

only a query to identify it. If we use a query that does not SELECT exactly one table, we will<br />

receive the following:<br />

ops$tkyte@ORA10G> update<br />

2 table( select emps<br />

3 from dept_<strong>and</strong>_emp<br />

4 where deptno = 1

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!