| Lesson 3 | Creating an Index-Organized Table |
| Objective | Understand and Apply the Syntax for Creating an Index-Organized Table in Oracle |
An index-organized table (IOT) is a specialized table structure in Oracle where data is stored in a B-tree index structure, sorted by the primary key. Unlike a standard heap-organized table, which stores data in an unordered heap, an IOT combines the table and its primary key index into a single structure. This provides faster access to data when queried by the primary key but may impact performance for other operations.
The syntax for creating an IOT is similar to that of a standard table, with the addition of the ORGANIZATION INDEX keywords. An IOT requires a primary key, as the data is physically stored in the order of the primary key. Below is the syntax:
CREATE TABLE, followed by the table name, column definitions, a primary key constraint, and the ORGANIZATION INDEX clause. Optional clauses like TABLESPACE, PCTTHRESHOLD, and OVERFLOW can be included for advanced configurations.
CREATE TABLE table_name (
column_list
CONSTRAINT pk_name PRIMARY KEY (pk_column_list)
)
ORGANIZATION INDEX
[TABLESPACE tablespace_name]
[PCTTHRESHOLD percentage]
[OVERFLOW TABLESPACE overflow_tablespace_name];
| CREATE TABLE | Required keywords to initiate table creation. |
| table_name | Unique name for the table. |
| column_list | List of columns with their data types. |
| CONSTRAINT | Introduces the primary key constraint, required for an IOT. |
| pk_name | Name of the primary key constraint. |
| PRIMARY KEY | Specifies the primary key columns. |
| pk_column_list | List of columns forming the primary key. |
| ORGANIZATION INDEX | Specifies that the table is an IOT, stored as a B-tree index. |
| TABLESPACE | Optional: Specifies the tablespace for the IOT. |
| PCTTHRESHOLD | Optional: Defines the percentage of an index block that a row can occupy before overflow is triggered. |
| OVERFLOW TABLESPACE | Optional: Specifies a tablespace for storing row data that exceeds the PCTTHRESHOLD. |
CREATE TABLE sales_figures (
store_id NUMBER,
quarter INTEGER,
month INTEGER,
amount NUMBER,
CONSTRAINT pk_sales_figures PRIMARY KEY (store_id, quarter, month)
)
ORGANIZATION INDEX;
store_id, quarter, and month,
as the data is stored in sorted order by these columns.
CREATE TABLE admin_docindex (
token CHAR(20),
doc_id NUMBER,
token_frequency NUMBER,
token_offsets VARCHAR2(2000),
CONSTRAINT pk_admin_docindex PRIMARY KEY (token, doc_id)
)
ORGANIZATION INDEX
TABLESPACE admin_tbs
PCTTHRESHOLD 20
OVERFLOW TABLESPACE admin_tbs2;
admin_docindex stores document indexing data with a composite primary key (token, doc_id).PCTTHRESHOLD 20 means that if a row exceeds 20% of the index block size, the excess columns (starting with token_offsets) are moved to an overflow segment.admin_tbs2 tablespace, improving performance for large rows.ORGANIZATION INDEX clause.ORGANIZATION HEAP, the default in Oracle).ORGANIZATION HEAP keyword is rarely used explicitly, as it is the default table structure, but it can be included for clarity in documentation.Use an IOT when:
Avoid IOTs when: