+ All Categories
Home > Documents > 11 Copyright © Oracle Corporation, 2001. All rights reserved. Managing Tables.

11 Copyright © Oracle Corporation, 2001. All rights reserved. Managing Tables.

Date post: 31-Mar-2015
Category:
Upload: gianna-burkart
View: 221 times
Download: 1 times
Share this document with a friend
Popular Tags:
22
11 Copyright © Oracle Corporation, 2001. All rights reserved. Managing Tables
Transcript
Page 1: 11 Copyright © Oracle Corporation, 2001. All rights reserved. Managing Tables.

11Copyright © Oracle Corporation, 2001. All rights reserved.

Managing Tables

Page 2: 11 Copyright © Oracle Corporation, 2001. All rights reserved. Managing Tables.

11-2 Copyright © Oracle Corporation, 2001. All rights reserved.

Objectives

After 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, drop a table

• Drop a column within a table

Page 3: 11 Copyright © Oracle Corporation, 2001. All rights reserved. Managing Tables.

11-3 Copyright © Oracle Corporation, 2001. All rights reserved.

Storing User Data

Regular table

Cluster

Partitionedtable

Index-organizedtable

Page 4: 11 Copyright © Oracle Corporation, 2001. All rights reserved. Managing Tables.

11-6 Copyright © Oracle Corporation, 2001. All rights reserved.

Oracle Built-in Data Types

CHAR(N), NCHAR(N)VARCHAR2(N),NVARCHAR2(N)NUMBER(P,S)DATETIMESTAMPRAW(N)BLOB, CLOB,NCLOB, BFILELONG, LONG RAWROWID, UROWID

VARRAY

TABLE

REF

Data type

Built-inUser-defined

Scalar RelationshipCollection

Page 5: 11 Copyright © Oracle Corporation, 2001. All rights reserved. Managing Tables.

11-10 Copyright © Oracle Corporation, 2001. All rights reserved.

ROWID Format

• Extended ROWID Format

• Restricted ROWID Format

OOOOOO BBBBBBFFF RRR

Data object number

Relative file number

Row numberBlock number

BBBBBBBB FFFFRRRR

Block number Row number File number

. .

Page 6: 11 Copyright © Oracle Corporation, 2001. All rights reserved. Managing Tables.

11-12 Copyright © Oracle Corporation, 2001. All rights reserved.

Structure of a Row

Database block

Row header

Column length

Column value

Page 7: 11 Copyright © Oracle Corporation, 2001. All rights reserved. Managing Tables.

11-13 Copyright © Oracle Corporation, 2001. All rights reserved.

Creating 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)

);

Page 8: 11 Copyright © Oracle Corporation, 2001. All rights reserved. Managing Tables.

11-17 Copyright © Oracle Corporation, 2001. All rights reserved.

Creating 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.

Page 9: 11 Copyright © Oracle Corporation, 2001. All rights reserved. Managing Tables.

11-18 Copyright © Oracle Corporation, 2001. All rights reserved.

Creating 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

• Can create indexes, views, and triggers on temporary tables

CREATE GLOBAL TEMPORARY TABLE

hr.employees_temp

AS SELECT * FROM hr.employees;

Page 10: 11 Copyright © Oracle Corporation, 2001. All rights reserved. Managing Tables.

11-19 Copyright © Oracle Corporation, 2001. All rights reserved.

(Average Row Size - Initial Row Size) * 100

Average Row Size

Average Row Size * 100100 -- PCTFREE --

Available Data Space

Setting PCTFREE and PCTUSED

• Compute PCTFREE

• Compute PCTUSED

Page 11: 11 Copyright © Oracle Corporation, 2001. All rights reserved. Managing Tables.

11-20 Copyright © Oracle Corporation, 2001. All rights reserved.

Before update After update

Pointer

Row Migration and Chaining

Page 12: 11 Copyright © Oracle Corporation, 2001. All rights reserved. Managing Tables.

11-21 Copyright © Oracle Corporation, 2001. All rights reserved.

Changing Storage and Block Utilization Parameters

ALTER TABLE hr.employees

PCTFREE 30

PCTUSED 50

STORAGE(NEXT 500K

MINEXTENTS 2

MAXEXTENTS 100);

Page 13: 11 Copyright © Oracle Corporation, 2001. All rights reserved. Managing Tables.

11-24 Copyright © Oracle Corporation, 2001. All rights reserved.

Manually Allocating Extents

ALTER TABLE hr.employees

ALLOCATE EXTENT(SIZE 500K

DATAFILE ‘/DISK3/DATA01.DBF’);

Page 14: 11 Copyright © Oracle Corporation, 2001. All rights reserved. Managing Tables.

11-25 Copyright © Oracle Corporation, 2001. All rights reserved.

Nonpartitioned Table Reorganization

• When a nonpartitioned table is reorganized, its structure is kept, but not its contents.

• Used to move a table to a different tablespace or reorganize extents.

ALTER TABLE hr.employees

MOVE TABLESPACE data1;

Page 15: 11 Copyright © Oracle Corporation, 2001. All rights reserved. Managing Tables.

11-26 Copyright © Oracle Corporation, 2001. All rights reserved.

Truncating a Table

• Truncating a table deletes all rows in a table and releases used space.

• Corresponding indexes are truncated.

TRUNCATE TABLE hr.employees;

Page 16: 11 Copyright © Oracle Corporation, 2001. All rights reserved. Managing Tables.

11-27 Copyright © Oracle Corporation, 2001. All rights reserved.

Dropping a Table

DROP TABLE hr.department

CASCADE CONSTRAINTS;

Page 17: 11 Copyright © Oracle Corporation, 2001. All rights reserved. Managing Tables.

11-29 Copyright © Oracle Corporation, 2001. All rights reserved.

Dropping 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;

Page 18: 11 Copyright © Oracle Corporation, 2001. All rights reserved. Managing Tables.

11-31 Copyright © Oracle Corporation, 2001. All rights reserved.

Using the UNUSED Option

• Mark a column as unused:

• Drop unused columns:

• Continue to drop column operation:

ALTER TABLE hr.employeesSET UNUSED COLUMN comments CASCADE CONSTRAINTS;

ALTER TABLE hr.employees

DROP UNUSED COLUMNS CHECKPOINT 1000;

ALTER TABLE hr.employees

DROP COLUMNS CONTINUE CHECKPOINT 1000;

Page 19: 11 Copyright © Oracle Corporation, 2001. All rights reserved. Managing Tables.

11-33 Copyright © Oracle Corporation, 2001. All rights reserved.

Obtaining Table Information

Information about tables can be obtained by querying

the following views:

• DBA_TABLES• DBA_OBJECTS

Page 20: 11 Copyright © Oracle Corporation, 2001. All rights reserved. Managing Tables.

11-35 Copyright © Oracle Corporation, 2001. All rights reserved.

Summary

In 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

Page 21: 11 Copyright © Oracle Corporation, 2001. All rights reserved. Managing Tables.

11-36 Copyright © Oracle Corporation, 2001. All rights reserved.

Practice 11 Overview

This practice covers the following topics:

• Creating a table

• Viewing, marking as unused, and dropping columns within a table

• Allocating extents manually

• Truncating a table

• Obtaining table information

Page 22: 11 Copyright © Oracle Corporation, 2001. All rights reserved. Managing Tables.

11-38 Copyright © Oracle Corporation, 2001. All rights reserved.


Recommended