Database
Objects
Dell Confidential
Agenda
1)Overview on database objects
2)Table and its structure
3)Temporary tables
4)External tables
Dell Confidential
Database objects
Database objects are the logical entities which Oracle
manages for users.
Users will interact with them through Oracle.
Each object has a unique name.
These objects fit together to form a database.
Database objects are classified into 2 main types:
1)Schema Objects
2)Non Schema Objects
Dell Confidential
Introduction to Schema Objects
A schema is a collection of logical structures of data, or schema objects.
A schema is owned by a database user and has the same name as
that user.
Each user owns a single schema.
Schema objects are logical structures created by users to contain, or
reference, their data.
Schema objects include structures like tables, views, and indexes etc.
In a relational database, the schema defines the tables, the fields in each
table, and the relationships between fields and tables.
Dell Confidential
illustrates the relationship among
objects, tablespaces, and datafiles.
Dell Confidential
Overview of Tables
Tables
Data
are the basic unit of data storage in an Oracle database.
is stored in rows and columns.
You define a table with a table name (such as employees) and 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.
Dell Confidential
More on tables
After you create a table, insert rows of data using
SQL statements.
Table data can then be queried, deleted, or updated
using SQL.
You can specify rules for each column of a table.
These rules are called integrity constraints.
One example is a NOT NULL integrity constraint.
This constraint forces the column to contain a value
in every row.
Dell Confidential
The EMP Table
Dell Confidential
How Table Data Is Stored
When a table is created, a data segment is allocated in a
tablespace to hold the table's future data.
Controlling the allocation and use of space for a table's data
segment is done in the following ways:
Setting the storage parameters for the data segment.
Setting the PCTFREE and PCTUSED parameters for the
data segment.
Dell Confidential
Managing Storage Parameters
The Storage parameters lets you specify how Oracle Database
should store a database object.
Storage parameters affect both how long it takes to access data
stored in the database and how efficiently space in the
database
The Oracle Database server manages extents for locally
managed tablespaces.
UNIFORM
AUTOALLOCATE (system-managed)
INITIAL, NEXT, PCTINCREASE, and MINEXTENTS are used
together to compute the initial size of the segment. After the
segment size is computed, internal algorithms determine the
size of each extent.
Dell Confidential
10
The PCTFREE Parameter
The PCTFREE parameter sets the minimum percentage of a
data block to be reserved as free space for possible updates
to rows that already exist in that block.
Dell Confidential
11
The PCTUSED Parameter
After a data block becomes full as determined by PCTFREE,
Oracle does not consider the block for the insertion of new rows
until the percentage of the block being used falls below the
parameter PCTUSED.
Dell Confidential
12
How PCTFREE and PCTUSED Work
Together
PCTFREE and PCTUSED work together to optimize the use of
space in the data blocks of the extents within a data segment.
Figure illustrates the interaction of these two parameters.
Dell Confidential
13
Dell Confidential
14
Format of a Table Row Piece
Dell Confidential
Temporary Tables
Addition
to permanent tables, oracle
creates temporary table to hold session
private data.
Each
session can only see and modify its
own data.
If
the TRUNCATE statement is issued
against a temporary table, only the session
specific data is truncated. There is no affect
on the data of other sessions.
Dell Confidential
Data in temporary tables is
automatically delete at the end of the
database session, even if it ends
abnormally.
Indexes ,views and triggers can be
created on temporary tables. The
content of the index and the scope of
the index is that same as the database
session.
Dell Confidential
.
Temporary tables are ideal for holding
intermediate data used by the current
SQL session.
EX:
Select *
into global temporary customer_india
from customer
where state=KARNATAKA
Dell Confidential
Creation..
The data in a temporary table is private
for the session that created it and can be
session-specific or transaction-specific. If
the data is to deleted at the end of the
transaction the table should be defined
as follows:
CREATE GLOBAL TEMPORARY TABLE
my_temp_table (
column1 NUMBER,
column2 NUMBER
) ON COMMIT DELETE ROWS;
Dell Confidential
..
If on the other hand that data should
be preserved until the session ends it
should be defined as follows:
CREATE GLOBAL TEMPORARY
TABLE my_temp_table (
column1 NUMBER,
column2 NUMBER
) ON COMMIT PRESERVE ROWS;
Dell Confidential
External Tables
External tables access data in external sources as
if it were in a table in the database
External tables are largely used as a convenient
way of moving data into and out of the database.
Dell Confidential
21
Creating an external table
When creating an external table you specify following attributes :
TYPE - specifies the type of external table
The ORACLE_LOADER access driver is the default.
It can perform only data loads, and the data must come from text datafiles.
The ORACLE_DATAPUMP access driver can perform both loads and
unloads. The data must come from binary dump files.
DEFAULT DIRECTORY - specifies the default location of files that are read
or written by external tables.
ACCESS PARAMETERS - describe the external data source and
implements the type of external table that was specified.
LOCATION - specifies the location of the external data.
The location is specified as a list of directory objects.
Dell Confidential
22
Creating and Loading an External
Table Using ORACLE_LOADER
1. Assume your .dat file looks as follows:
56november, 15, 1980 baker mary alice
87december, 20, 1970 roper lisa marie
2. Since an external table's data is in the operating system, its
data file needs to be in a place Oracle can access it. So the first
step is to create a directory and grant access to it.
CREATE DIRECTORY ext_tab_dir AS '/usr/apps/datafiles';
GRANT READ ON DIRECTORY ext_tab_dir TO SCOTT;
Dell Confidential
23
Creating and Loading an External
Table Using ORACLE_LOADER
3. Create a traditional table named emp:
CREATE TABLE emp (emp_no CHAR(6), last_name CHAR(25),
first_name CHAR(20), middle_initial CHAR(1));
4. Create an external table named emp_load:
CREATE TABLE emp_load (employee_number CHAR(5),
employee_last_name CHAR(20),
employee_first_name CHAR(15),
employee_middle_name CHAR(15))
ORGANIZATION EXTERNAL (type oracle loader default directory ext_tab_dir
ACCESS PARAMETERS (records delimited by
newline fields(employee_number CHAR(2),
employee_dob CHAR(20),
employee_last_name CHAR(18),
employee_first_name CHAR(11),
employee_middle_name CHAR(11)))
LOCATION ('info.dat'));
Dell Confidential
24
Creating and Loading an External
Table Using ORACLE_LOADER
5.Load the data from the external table emp_load into the table emp:
Insert into emp (emp_no, first_name, middle_initial, last_name)
values(SELECT employee_number, employee_first_name,
substr(employee_middle_name, 1, 1),
employee_last_name FROM emp_load);
6.Perform the following select operation to verify that the information in the
.dat file was loaded into the emp table:
SQL> SELECT * FROM emp; EMP_NO LAST_NAME FIRST_NAME M
------ ------------------------- -------------------- 56 baker mary a
87 roper lisa m
Dell Confidential
25
External Table Restrictions
An external table does not describe any data that is stored in the database.
An external table does not describe how data is stored in the external source.
This is the function of the access parameters.
An external table cannot load data into a LONG column.
When identifiers (for example, column or table names) are specified
in the external table access parameters, certain values are considered
to be reserved words by the access parameter parser. If a reserved word
is used as an identifier, it must be enclosed in double quotation marks.
Dell Confidential
26