Less06 Schema
Less06 Schema
Facultad de Ingeniería
Escuela de Ingeniería Informática
Esquemas
Base de Datos II
Managing Schema Objects
Objectives
•After completing this lesson, you should be able to:
– Define schema objects and data types
– Create and modify tables
– Define constraints
– View the columns and contents of a table
– Create indexes
– Create views
– Create sequences
– Explain the use of temporary tables
> Schema
owns
HR schema
HR user
Accessing Schema Objects
Naming Database Objects
– The length of names must be from 1 to 30 bytes, with
these exceptions:
• Names of databases are limited to 8 bytes.
• Names of database links can be as long as 128 bytes.
– Nonquoted names cannot be Oracle-reserved words.
– Nonquoted names must begin with an alphabetic
character from your database character set.
– Quoted names are not recommended.
Specifying Data Types in Tables
•Common data types:
– CHAR(size [BYTE|CHAR]): Fixed-length
character data of size bytes or characters
– VARCHAR2(size [BYTE|CHAR]): Variable-
length character string having a maximum length of
size bytes or characters
– DATE: Valid date ranging from January 1, 4712
(B.C.), through December 31, 9999 (A.D.)
– NUMBER(p,s): Number with precision p and
scale s
Creating and Modifying Tables
REGIONS
REGION_ID (PK)
REGION_NAME
Defining Constraints
Constraint Violations
•Examples of how a constraint can be violated:
– Inserting a duplicate primary key value
– Deleting the parent of a child row in a referential
integrity constraint
– Updating a column to a value that is out of the bounds
of a check constraint
ID AGE
101 … X …
…
22
49
–30
102 …
101
103 … … 16
… 5
Constraint States
DISABLE DISABLE ENABLE ENABLE
NOVALIDATE VALIDATE NOVALIDATE VALIDATE
No DML
New data
Existing data
Constraint Checking
•Constraints are checked at the time of:
– Statement execution (for nondeferred constraints)
– COMMIT (for deferred constraints)
Case: DML statement followed by COMMIT
1 Nondeferred constraints
checked
2 COMMIT issued
4 COMMIT complete
Creating Constraints with SQL:
Examples
ALTER TABLE countries
a
ADD (UNIQUE(country_name) ENABLE NOVALIDATE);
Indexes Constraints
> Indexes
Views
Sequences
Temp Tables
Data Dict
… WHERE key = 22
Row
Key pointer
22
22
Index Table
Types of Indexes
•These are several types of index structures that
are available depending on your needs. Two of
the most common are:
– B-tree index
• Default index type; in the form of a balanced tree
– Bitmap index:
• Has a bitmap for each distinct value indexed
• Each bit position represents a row that may or may not
contain the indexed value.
• Best for low-cardinality columns
B-Tree Index
Index entry
Root
Branch
Block 11
Index
Block 12
Start End
Key ROWID ROWID Bitmap
<Blue, 10.0.3, 12.8.3, 1000100100010010100>
<Green, 10.0.3, 12.8.3, 0001010000100100000>
<Red, 10.0.3, 12.8.3, 0100000011000001001>
<Yellow, 10.0.3, 12.8.3, 0010001000001000010>
Index Options
– Unique index: Ensures that every indexed value is
unique
– Reverse key index: Has its key value bytes stored in
reverse order
– Composite index: Is based on more than one column
– Function-based index: Is based on a function’s return
value
– Compressed index: Has repeated key values removed
– Order: An index can have its key values stored in
ascending or descending order.
Creating Indexes
Views
Constraints
Indexes
> Views
…
LOCATION table
join
AU_BR_VIEW view COUNTRY table
Sequences Constraints
Indexes
Views
> Sequences
Temp Tables
•A sequence is a mechanism for automatically Data Dict
1 row created.
Schema