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.

372<br />

CHAPTER 10 ■ DATABASE TABLES<br />

ops$tkyte@ORA10GR1> create table dept<br />

2 ( deptno number(2) primary key,<br />

3 dname varchar2(14),<br />

4 loc varchar2(13)<br />

5 )<br />

6 cluster emp_dept_cluster(deptno)<br />

7 /<br />

Table created.<br />

ops$tkyte@ORA10GR1> create table emp<br />

2 ( empno number primary key,<br />

3 ename varchar2(10),<br />

4 job varchar2(9),<br />

5 mgr number,<br />

6 hiredate date,<br />

7 sal number,<br />

8 comm number,<br />

9 deptno number(2) references dept(deptno)<br />

10 )<br />

11 cluster emp_dept_cluster(deptno)<br />

12 /<br />

Table created.<br />

Here, the only difference from a “normal” table is that we used the CLUSTER keyword <strong>and</strong><br />

told <strong>Oracle</strong> which column of the base table will map to the cluster key in the cluster itself.<br />

Remember, the cluster is the segment here, therefore this table will never have segment attributes<br />

such as TABLESPACE, PCTFREE, <strong>and</strong> so on—they are attributes of the cluster segment, not<br />

the table we just created. We can now load them up with the initial set of data:<br />

ops$tkyte@ORA10GR1> begin<br />

2 for x in ( select * from scott.dept )<br />

3 loop<br />

4 insert into dept<br />

5 values ( x.deptno, x.dname, x.loc );<br />

6 insert into emp<br />

7 select *<br />

8 from scott.emp<br />

9 where deptno = x.deptno;<br />

10 end loop;<br />

11 end;<br />

12 /<br />

PL/SQL procedure successfully completed.<br />

You might be wondering, “Why didn’t we just insert all of the DEPT data <strong>and</strong> then all of<br />

the EMP data, or vice versa? Why did we load the data DEPTNO by DEPTNO like that?” The reason<br />

is in the design of the cluster. We are simulating a large, initial bulk load of a cluster. If we had<br />

loaded all of the DEPT rows first, we definitely would have gotten our seven keys per block<br />

(based on the SIZE 1024 setting we made), since the DEPT rows are very small (just a couple of

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

Saved successfully!

Ooh no, something went wrong!