Creating Cluster tables in Oracle 11g



The flow is

* First you need to create a cluster.
* Then create tables that need to be clustered by using the newly created cluster.

The idea of clustered table is to minimize space used by a group of tables by clustering column that occur commonly in these tables.

Suppose there are 3 tables namely T1, T2, T3 and they all have a column named Emp_ID with the same values, then clustering these table using this column Emp_ID reduce space occupied by the column. This column will be stored only one time in a cluster instead of storing in 3 tables.

Here is the command to create a cluster


create cluster test_cluster (Emp_ID varchar2(32));

Create the tables that need to clustered by using the above created cluster.

create table T1 (Col1 varchar2(100), Emp_ID varchar2(32)) cluster test_cluster(Emp_ID);

create table T2 (Col1 varchar2(100), Emp_ID varchar2(32)) cluster test_cluster(Emp_ID);

etc.

Make sure you match the type of the cluster column and the column you assign for clustering while creating the table. You will get error as shown below if you don't.


create table clus_test (col2 number, keys number) cluster test_cluster (col1);

Error report:
SQL Error: ORA-00945: specified clustered column does not exist
00945. 00000 -  "specified clustered column does not exist"

By default oracle creates Index Clusters, you must create an cluster index for this cluster before you can make a DML command on the associated table of this cluster.

You will get the below error if you try to issue a DML for a unindexed cluster table.


insert into T1 values ('testing');

Error report:
SQL Error: ORA-02032: clustered tables cannot be used before the cluster index is built
02032. 00000 -  "clustered tables cannot be used before the cluster index is built"
*Cause:    User attempted to perform a DML statement on a clustered table
           for which no cluster index has yet been created.
*Action:   Create the cluster index.
 
 
Create an index for this cluster.
 
create index clust_idx_test on cluster test_cluster;
 
Results:

index CLUS_TEST_IDX created.
 
If you try a DML it will succeed.

Comments

Popular posts from this blog

Current Variables in T24

All accounting entries of an T24 account

SQL queries in JBase