Storing User DataRegular table Cluster Partitioned table Index-organized table... Creating a TableCREATE TABLE hr.employees employee_id NUMBER6, first_name VARCHAR220, last_name VARCHAR2
Trang 111Managing Tables
Trang 2After completing this lesson, you should be able to do the following:
• Identify the various methods of storing data
• Outline Oracle data types
• Distinguish between an extended versus a restricted
ROWID
• Outline the structure of a row
• Create regular and temporary tables
• Manage storage structures within a table
• Reorganize, truncate, and drop a table
• Drop a column within a table
Trang 3Storing User Data
Regular table
Cluster
Partitioned table
Index-organized
table
Trang 4Oracle Built-in Data Types
REF
Data type
Built-in User-defined
Trang 5ROWID Format
• Extended ROWID Format
• Restricted ROWID Format
Data object
number
Relative file number
Row number Block number
Trang 6Structure of a Row
Database block
Row header Column length Column value
Trang 7Creating a Table
CREATE TABLE hr.employees(
employee_id NUMBER(6), first_name VARCHAR2(20), last_name VARCHAR2(25), email VARCHAR2(25),
phone_number VARCHAR2(20), hire_date DATE DEFAULT SYSDATE, job_id VARCHAR2(10),
salary NUMBER(8,2), commission_pct NUMBER (2,2), manager_id NUMBER(6),
department_id NUMBER(4)) TABLESPACE USERS;
Trang 8Creating a Table: Guidelines
• Place tables in separate tablespaces.
• Use locally-managed tablespaces to avoid
fragmentation.
• Use few standard extent sizes for tables to reduce
tablespace fragmentation.
Trang 9Creating Temporary Tables
• Created using the GLOBAL TEMPORARY clause:
• Tables retain data only for the duration of a
transaction or session.
• DML locks are not acquired on the data.
• You can create indexes, views, and triggers on
Trang 10(Average Row Size - Initial Row Size) * 100
Average Row Size
Average Row Size * 100
100 - PCTFREE - -
-Available Data Space
Setting PCTFREE and PCTUSED
• Compute PCTFREE
• Compute PCTUSED
Trang 11Before update After update
Pointer
Row Migration and Chaining
Trang 12Changing Storage and Block
Trang 13Manually Allocating Extents
ALTER TABLE hr.employees
ALLOCATE EXTENT(SIZE 500K
DATAFILE ‘/DISK3/DATA01.DBF’);
Trang 14Nonpartitioned Table Reorganization
• When a nonpartitioned table is reorganized, its
structure is kept, but not its contents.
• It is used to move a table to a different tablespace or
reorganize extents.
ALTER TABLE hr.employees
MOVE TABLESPACE data1;
Trang 15Truncating a Table
• Truncating a table deletes all rows in a table and
releases used space.
• Corresponding indexes are truncated.
TRUNCATE TABLE hr.employees;
Trang 16Dropping a Table
DROP TABLE hr.departments
CASCADE CONSTRAINTS;
Trang 17Dropping a Column
Removing a column from a table:
• 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.
ALTER TABLE hr.employees
DROP COLUMN comments
CASCADE CONSTRAINTS CHECKPOINT 1000;
Trang 18Renaming a Column
Renaming a column from a table:
ALTER TABLE hr.employees
RENAME COLUMN hire_date
TO start_date;
Trang 19Using the UNUSED Option
• Mark a column as unused:
• Drop unused columns:
• Continue to drop column operation:
ALTER TABLE hr.employees
SET UNUSED COLUMN comments CASCADE
CONSTRAINTS;
ALTER TABLE hr.employees
DROP UNUSED COLUMNS CHECKPOINT 1000; ALTER TABLE hr.employees
DROP COLUMNS CONTINUE CHECKPOINT 1000;
Trang 20Obtaining Table Information
Information about tables can be obtained by querying the following views:
• DBA_TABLES
• DBA_OBJECTS
Trang 21In this lesson, you should have learned how to:
• Distinguish between an extended versus a restricted ROWID
• Outline the structure of a row
• Create regular and temporary tables
• Manage storage structures within a table
• Reorganize, truncate, and drop a table
• Drop a column within a table
• Obtaining table information