View
216
Download
1
Category
Preview:
Citation preview
Learning Objectives
Create tables with large object (LOB) columns and tables that are index-organized
Understand the tasks involved in table management
Use data dictionary views to find information about tables and their underlying structures
Advanced Table Structures
Table types covered:
Tables with LOB columns:
Store large amount of data in a single column of one row
Can define a separate segment for the LOB
Index-organized tables
Store data in primary key order
Best when queries nearly always use primary key to look up data in the table
Tables with LOB Columns
Types of LOB columns:Internal LOB
• Stored inside the database• Datatypes: BLOB, CLOB, NCLOB
External LOB• Stored outside the database• Datatypes: BFILE• Read-only
Tables with LOB Columns
Example:CREATE TABLE SOUNDBYTES
(ALBUM_ID VARCHAR2(20),
ARTIST VARCHAR2(40),
CD_MUSIC BFILE,
POSTER_SHOT BLOB );
External LOB
Internal LOB
Tables with LOB Columns
New package for updating internal LOBs:DBMS_LOB.LOADBLOBFROMFILE:
Load data into BLOB from external file
DBMS_LOB.LOADCLOBFROMFILE: Load data into CLOB from external file
Co
Tables with LOB Columns
Semantics of copying LOBs:
Internal LOB uses copy semantics: Copying a LOB from one row or column to another
copies the contents
External LOB uses reference semantics: Copying a LOB from one row or column to another
copies only a pointer to the original location of contents
Co
LOB Storage
Internal LOBs:• Default stored inline or out of line, by size• Always stored out of line with DISABLE STORAGE IN ROW
External LOBs:• LOB locator stored in the table• LOB data stored in operating system file
LOB Storage
Syntax:CREATE TABLE <tablename> (<other_column_specs>, <LOBcolumnname> <LOBdatatype>)LOB (<LOBcolumnname>) STORE AS <lobsegmentname> (TABLESPACE <tablespacename> ENABLE STORAGE IN ROW|DISABLE STORAGE IN ROW CHUNK <nn> STORAGE (INITIAL <nn> NEXT <nn> MAXEXTENTS UNLIMITED|<nn>) PCTVERSION <nn>|RETENTION LOGGING|NOLOGGING CACHE|NOCACHE);
LOB Storage
Syntax of storage for LOB:STORE AS <lobsegmentname>DISABLE / ENABLE STORAGE IN
ROWSCHUNK <nn>PCTVERSION / RETENTIONLOGGING / NOLOGGINGCACHE / NOCACHE
Index-Organized Tables Relational table with primary key
Sorted and stored in key order
Normal relational tables are heap-organized
Stored in b-tree structure
Index-Organized Tables
Syntax components:ORGANIZATION INDEXOVERFLOWPCTTHRESHOLDINCLUDING <col>TABLESPACE <name>STORAGE (<storage>)
Index-Organized Tables
Example:CREATE TABLE ZIPREFERENCES(ZIPCODE VARCHAR2(10) NOT NULL, CITY VARCHAR2(40) NOT NULL, STATE VARCHAR2(10) NOT NULL, PROVINCE VARCHAR2(10) NOT NULL, COUNTRY VARCHAR2(20) NOT NULL, CONSTRAINT ZIP_PK PRIMARY KEY(ZIPCODE)) ORGANIZATION INDEXTABLESPACE USERSPCTTHRESHOLD 20OVERFLOW TABLESPACE USERSSTORAGE (INITIAL 64K NEXT 32K MAXEXTENTS 50 PCTINCREASE 0);
Overview of Table Management
Types of changes:
Collecting and Refreshing Statistics
Change the storage settings
Reorganize the table online
Drop columns
Truncate or drop the table
Analyzing a Table
Why analyze tables? To give the optimizer up-to-date
information for optimizing queries and other SQL commands
To give you information to help you decide which storage or column settings to change
Analyzing a Table
Oracle's optimizers:
Cost-based optimizerDefaultUses table statistics
Rule-based optimizerOlderUses syntax rules only
Analyzing a Table
Examples ANALYZE command:
ANALYZE TABLE CUSTOMER ESTIMATE STATISTICS
SAMPLE 1000 ROWS;
DBMS_STATS package:
EXECUTE DBMS_STATS.GATHER_TABLE_STATS -
('CLASSMATE','CUSTOMER');
Adjusting Table Storage Structure Syntax of ALTER TABLE command:
ALTER TABLE <schema>.<tablename>PCTFREE <nn> PCTUSED <nn>INITTRANS <nn> MAXTRANS <nn>STORAGE (NEXT <nn> PCTINCREASE <nn> MAXEXTENTS <nn>|UNLIMITED)ALLOCATE EXTENT SIZE <nn> DATAFILE <filename>DEALLOCATE UNUSED KEEP <nn>COMPRESS|NOCOMPRESSMOVE TABLESPACE <tablespacename> STORAGE (INITIAL <nn> NEXT <nn> PCTINCREASE <nn> MAXEXTENTS <nn>|UNLIMITED) COMPRESS|NOCOMPRESS ONLINE
Adjusting Table Storage Structure
Syntax components:PCTFREE <nn> PCTUSED <nn>INITTRANS <nn> MAXTRANS <nn>STORAGE (...)ALLOCATE EXTENTDEALLOCATE UNUSEDCOMPRESSMOVEONLINE
Adjusting Table Storage Structure
Examples: Deallocate unused space:
ALTER TABLE HORSERACE DEALLOCATE UNUSED KEEP 50K;
Change STORAGE setting:
ALTER TABLE CLASSIFIED_AD MOVE TABLESPACE USERS
STORAGE (NEXT 56K);
Reorganizing a Table
DBMS_REDEFINITION package:
Redefine anything in the table Keep old version of the table available
during the change
Reorganizing a Table
DBMS_REDEFINITION steps:
1. Check the table – is it eligible
2. Create interim table with changes you want done
3. Start redefinition process
4. Finish redefinition process
Each step (except #2) is a call to DBMS_REDEFINITION package
Making Other Table Changes
Syntax of the ALTER TABLE command:
ALTER TABLE <schema>.<tablename>
RENAME TO <newname>
LOGGING|NOLOGGING
MONITORING|NOMONITORING
ENABLE|DISABLE ROW MOVEMENT
CACHE|NOCACHE
Making Other Table Changes
Example:
Modify monitoring and cache:
ALTER TABLE EMPLOYEE MONITORING CACHE;
Dropping, Adding, or Modifying a Column in a Table Syntax for ALTER TABLE command:
ALTER TABLE <schema>.<tablename>
RENAME COLUMN <oldcolname> TO <newcolname>
ADD (<colname> <datatype>, ... )
MODIFY (<colname> <datatype>, ... )
DROP (<colname>, <colname>,...)|COLUMN <colname>
CASCADE CONSTRAINTS
SET UNUSED (<colname>,<colname>,...)|COLUMN <colname>
CASCADE CONSTRAINTS
DROP UNUSED COLUMNS
Note: Each of these clauses must be used alone in a single ALTER TABLE command.
Dropping, Adding, or Modifying a Column in a TableExamples: Rename a column:
ALTER TABLE CH08SURGERY RENAME COLUMN PATIENT_FISRT_NAME TO
PATIENT_FIRST_NAME;
Add a column:ALTER TABLE CH08SURGERY ADD (OUTCOME VARCHAR2(40), OPERATING_ROOM_NO
CHAR(4)); Change length of column:
ALTER TABLE CH08SURGERY MODIFY (DOCTOR_NAME VARCHAR2(20));
Truncating and Dropping a Table
TRUNCATE is fast:No redo log entriesNo undo recordsCannot rollbackSyntax:
TRUNCATE TABLE <schema>.<tablename>
DROP STORAGE|REUSE STORAGE
Truncating and Dropping a Table
DROP command:Removes data and structureMust drop constraints that use the
dropped tableExample:
DROP TABLE COPCAR CASCADE CONSTRAINTS;
Querying Table-Related Data Dictionary Views
Example: Look at statistics gathered by
monitoring tables:
SELECT TABLE_NAME, TABLESPACE_NAME, MONITORING,
BLOCKS, EMPTY_BLOCKS
FROM USER_TABLES;
Lecture Summary
LOB data types are either internal LOBS or external LOBS
New features in the DBMS_LOB package simplify loading LOB data
Internal LOB values larger than 4000 bytes are stored out of line in a LOB data segment
Oracle9i works with LOBs by reading and writing one chunk at a time
LOB data segments can also be used to store varray data
Lecture Summary
Index-organized tables require a primary key and store data in order
Optionally split the column data into the main segment and an overflow segment
The ANALYZE command gathers statistics The DBMS_STATS package also gathers
statistics Change a table’s storage parameters using
the ALTER TABLE command
Lecture Summary
DBMS_REDEFINITION restructures a table while keeping it online
ALTER TABLE can do these tasks:Move table to different tablespaceRelease unused spaceRename the tableModify columns (add, change, drop)
TRUNCATE removes rows permanently
Recommended