2. Objectives
After completing this lesson, you should be able to
do the following:
• Create tables using appropriate storage settings
• Control the space used by tables
• Analyze tables to check integrity and migration
• Retrieve information about tables from the data
dictionary
• Convert between different formats of ROWID
4. Structure of a Row
Database block
Row header
Column length
Column value
5. Oracle Data Types
CHAR(N), NCHAR(N)
VARCHAR2(N),
NVARCHAR2(N)
NUMBER(P,S)
DATE
RAW(N)
BLOB, CLOB,
NCLOB, BFILE
LONG, LONG RAW
ROWID, UROWID
VARRAY
TABLE
REF
Data type
Built-inUser-defined
Scalar RelationshipCollection
6. Data Types for Storing
Large Objects
LONG, LONG RAW
Single column per table
Up to 2 gigabytes
SELECT returns data
Data stored in-line
No object type support
Sequential access to chunks
LOB
Multiple columns per table
Up to 4 gigabytes
SELECT returns locator
Data stored in-line or out-of-line
Supports object types
Random access to chunks
7. ROWID Data Type
• Unique identifier for a row
• Used to locate a row
OOOOOO BBBBBBFFF RRR
Data object
number
Relative file
number
Row numberBlock number
ROWID Format
8. Restricted ROWID
• Can identify rows within a segment
• Needs less space
BBBBBBBB FFFFRRRR
Block number Row number File number
. .
9. Collections
• Collections are objects that contain objects.
• VARRAYs are ordered sets of elements containing
a count and a limit.
• Nested tables are tables with a column or
variable of the TABLE data type.
VARRAY Nested
table
10. Creating a Table
CREATE TABLE employee(
id NUMBER(7),
last_name VARCHAR2(25),
dept_id NUMBER(7))
PCTFREE 20 PCTUSED 50
STORAGE(INITIAL 200K NEXT 200K
PCTINCREASE 0 MAXEXTENTS 50)
TABLESPACE data;
11. Temporary Tables
• The rows are private to the session.
• Tables retain data only for the duration of a
transaction or session.
• DML locks are not acquired on the data.
• DMLs do not generate redo logs.
ON COMMIT PRESERVE ROWS;
CREATE GLOBAL TEMPORARY TABLE employee_temp
AS SELECT * FROM employee;
12. Creating a Table: Guidelines
• Use a few standard extent sizes for tables to
reduce tablespace fragmentation.
• Use locally managed tablespaces to avoid
fragmentation.
• Use the CACHE clause for frequently used, small
tables.
13. Setting PCTFREE and PCTUSED
• Compute PCTFREE
(Average Row Size - Initial Row Size) * 100
Average Row Size
• Compute PCTUSED
Average Row Size * 100
100 - PCTFREE -
Available Data Space
17. Nonpartitioned Table Reorganization
ALTER TABLE employee
MOVE TABLESPACE data1;
• Moves data into a new segment while preserving
indexes, constraints, privileges, and so on, on the
table
• Is being used to move a table to a different
tablespace or to reorganize extents
18. Free space
after delete
Unused block
Extent ID 0 1 2 3 4
Extent ID 0 1 2 3 4
Used block
High-Water Mark
After inserts:
After deletes:
High-water
mark
19. Finding the High-Water Mark:
DBMS_SPACE.UNUSED_SPACE
Extent ID 0 1 2 3 4
High-water mark
LAST_USED_EXTENT_FILE_ID,
LAST_USED_EXTENT_BLOCK_ID
TOTAL_BLOCKS
UNUSED_BLOCKS
20. Free space
after delete
Unused block
Before
deallocation
Used block
Deallocation of Unused Space
ALTER TABLE summit.employee
DEALLOCATE UNUSED;
After
deallocation
High-water
mark
Extent ID 43210
43210Extent ID
23. Dropping a Column
ALTER TABLE employee
DROP COLUMN comments
CASCADE CONSTRAINTS CHECKPOINT 1000;
Remove a column from a table
• This removes the column length and data from
each row, freeing space in the data block.
• Dropping a column in a large table takes a
considerable amount of time.
24. Using the UNUSED Option
ALTER TABLE orders
SET UNUSED COLUMN comments
CASCADE CONSTRAINTS;
• Drop unused columns
ALTER TABLE orders
DROP UNUSED COLUMNS CHECKPOINT 1000;
• Mark a column as unused
• Continue to drop column operation
ALTER TABLE orders
DROP COLUMNS CONTINUE CHECKPOINT 1000;
28. Summary
In this lesson, you should have learned how to:
• Create a table with appropriate storage and
block utilization parameters
• Control table storage
• Use the DBMS_ROWID package