You are on page 1of 6

Introduction to Tables

About Tables
Tables are the basic unit of data storage in an Oracle Database. Data is stored in rows and
columns. You define a table with a table name, such as employees, and a set of columns. You
give each column a column name, such as employee_id, last_name, and job_id; a datatype,
such as VARCHAR2, DATE, or NUMBER; and a width. The width can be predetermined by the
datatype, as in DATE. If columns are of the NUMBER datatype, define precision and scale instead of
width. A row is a collection of column information corresponding to a single record.
Design Tables before Creating Them
Usually, the application developer is responsible for designing the elements of an application,
including the tables. Database administrators are responsible for establishing the attributes of the
underlying tablespace that will hold the application tables. Either the DBA or the applications
developer, or both working jointly, can be responsible for the actual creation of the tables,
depending upon the practices for a site.
Working with the application developer, consider the following guidelines when designing
tables:
• Use descriptive names for tables, columns, indexes, and clusters.
• Be consistent in abbreviations and in the use of singular and plural forms of table names
and columns.
• Document the meaning of each table and its columns with the COMMENT command.
• Normalize each table.
• Select the appropriate datatype for each column.
• Define columns that allow nulls last, to conserve storage space.
• Cluster tables whenever appropriate, to conserve storage space and optimize performance
of SQL statements

Chapter 8 | Managing
Tables
Consider Your Options for the Type of Table to Create
• What types of tables can you create? Here are some choices:
Type of Table Description
Ordinary (heap- This is the basic, general purpose type of table which is the primary subject
organized) table of this chapter. Its data is stored as an unordered collection (heap)
Clustered table A clustered table is a table that is part of a cluster. A cluster is a group of
tables that share the same data blocks because they share common columns
and are often used together.
Index-organized Unlike an ordinary (heap-organized) table, data for an index-organized table
table is stored in a B-tree index structure in a primary key sorted manner. Besides
storing the primary key column values of an index-organized table row, each
index entry in the B-tree stores the nonkey column values as well.
Partitioned table Partitioned tables allow your data to be broken down into smaller, more
manageable pieces called partitions, or even subpartitions. Each partition can
be managed individually, and can operate independently of the other
partitions, thus providing a structure that can be better tuned for availability
and performance.

fig1. Clustered table and Unclustered table


Chapter 8 | Managing
Tables
Creating a Table
When you issue the following statement, you create a table named admin_emp in the hr schema
and store it in the admin_tbs tablespace with an initial extent size of 50K:
CREATE TABLE hr.admin_emp (
empno NUMBER(5) PRIMARY KEY,
ename VARCHAR2(15) NOT NULL,
job VARCHAR2(10),
mgr NUMBER(5),
hiredate DATE DEFAULT (sysdate),
sal NUMBER(7,2),
comm NUMBER(7,2),
deptno NUMBER(3) NOT NULL
CONSTRAINT admin_dept_fkey REFERENCES hr.departments
(department_id))
TABLESPACE admin_tbs
STORAGE ( INITIAL 50K);

Adding Table Columns


To add a column to an existing table, use the ALTER TABLE ... ADD statement.
The following statement alters the hr.admin_emp table to add a new column named bonus:
ALTER TABLE hr.admin_emp
ADD (bonus NUMBER (7,2));

Renaming Table Columns


Oracle Database lets you rename existing columns in a table. Use the RENAME COLUMN clause of
the ALTER TABLE statement to rename a column.
The following statement renames the comm column of the hr.admin_emp table.
ALTER TABLE hr.admin_emp
RENAME COLUMN comm TO commission;
Removing Columns from Tables
When you issue an ALTER TABLE ... DROP COLUMN statement, the column descriptor and the
data associated with the target column are removed from each row in the table. You can drop
multiple columns with one statement.
ALTER TABLE hr.admin_emp DROP COLUMN sal
ALTER TABLE hr.admin_emp DROP (bonus, commission);

Chapter 8 | Managing
Tables
Partition
A smaller and more manageable piece of a table
Partitioning Methods
There are several partitioning methods offered by Oracle Database:
• Range partitioning
• Hash partitioning
• List partitioning
• Composite range-hash partitioning
• Composite range-list partitioning

About Partitioned Tables


• Modern enterprises frequently run mission-critical databases containing upwards of
several hundred gigabytes and, in many cases, several terabytes of data. These enterprises
are challenged by the support and maintenance requirements of very large databases
(VLDB), and must devise methods to meet those challenges.
• One way to meet VLDB demands is to create and use partitioned tables. Partitioned
tables allow your data to be broken down into smaller, more manageable pieces called
partitions, or even subpartitions

Storing partitions in separate tablespaces enables you to:


• Reduce the possibility of data corruption in multiple partitions
• Back up and recover each partition independently
• Control the mapping of partitions to disk drives (important for balancing I/O load)
• Improve manageability, availability, and performance

Chapter 8 | Managing
Tables
Range partition:-
Partition which will be made based on the numeric values of the table is know as range
partition.
When creating range partitions, you must specify:
• Partitioning method: range
• Partitioning column(s)
• Partition descriptions identifying partition bounds
The example below creates a table of four partitions, one for each quarter of sales. The columns
sale_year, sale_month, and sale_day are the partitioning columns, while their values
constitute the partitioning key of a specific row. The VALUES LESS THAN clause determines the
partition bound: rows with partitioning key values that compare less than the ordered list of
values specified by the clause are stored in the partition. Each partition is given a name
(sales_q1, sales_q2, ...), and each partition is contained in a separate tablespace (tsa, tsb, ...).
CREATE TABLE sales
( invoice_no NUMBER,
sale_year INT NOT NULL,
sale_month INT NOT NULL,
sale_day INT NOT NULL )
PARTITION BY RANGE (sale_year, sale_month, sale_day)
( PARTITION sales_q1 VALUES LESS THAN (1999, 04, 01)
TABLESPACE tsa,
PARTITION sales_q2 VALUES LESS THAN (1999, 07, 01)
TABLESPACE tsb,
PARTITION sales_q3 VALUES LESS THAN (1999, 10, 01)
TABLESPACE tsc,
PARTITION sales_q4 VALUES LESS THAN (2000, 01, 01)
TABLESPACE tsd );

A row with sale_year=1999, sale_month=8, and sale_day=1 has a partitioning key of (1999,
8, 1) and would be stored in partition sales_q3.

Chapter 8 | Managing
Tables
List partition:-
Partition, which will be based on the string values know as list partition.

When creating list partitions, you must specify:


• Partitioning method: list
• Partitioning column
• Partition descriptions, each specifying a list of literal values (a value list), which are the
discrete values of the partitioning column that qualify a row to be included in the
partition
The following example creates a list-partitioned table. It creates table q1_sales_by_region
which is partitioned by regions consisting of groups of states.
CREATE TABLE q1_sales_by_region
(deptno number,
deptname varchar2(20),
quarterly_sales number(10, 2),
state varchar2(2))
PARTITION BY LIST (state)
(PARTITION q1_northwest VALUES ('OR', 'WA'),
PARTITION q1_southwest VALUES ('AZ', 'UT', 'NM'),
PARTITION q1_northeast VALUES ('NY', 'VM', 'NJ'),
PARTITION q1_southeast VALUES ('FL', 'GA'),
PARTITION q1_northcentral VALUES ('SD', 'WI'),
PARTITION q1_southcentral VALUES ('OK', 'TX'));

A row is mapped to a partition by checking whether the value of the partitioning column for a
row matches a value in the value list that describes the partition.
For example, some sample rows are inserted as follows:
• (10, 'accounting', 100, 'WA') maps to partition q1_northwest
• (20, 'R&D', 150, 'OR') maps to partition q1_northwest
• (30, 'sales', 100, 'FL') maps to partition q1_southeast
• (50, 'systems engineering', 10, 'CA') does not map to any partition in the table and raises
an error

Chapter 8 | Managing
Tables

You might also like