Escolar Documentos
Profissional Documentos
Cultura Documentos
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
What Is a Schema?
owns
HR schema
HR user
> Schema
Constraints
Indexes
Views
Sequences
Temp Tables
Data Dict
Accessing Schema
Objects
Schema
>Constraints
Indexes
Views
Sequences
Temp Tables
Data Dict
DEPARTMENTS
JOB_HISTORY
EMPLOYEE_ID
(PK,FK)
START_DATE
(PK) END_DATE
JOB_ID (FK)
DEPARTMENT_ID (FK)
EMPLOYEES
DEPARTMENT_ID (PK)
DEPARTMENT_NAME
EMPLOYEE_ID
(PK) FIRST_NAME
LAST_NAME
EMAIL
PHONE_NUMBER
HIRE_DATE
JOB_ID (FK)
SALARY
COMMISION_PCT
MANAGER_ID (FK)
DEPARTMENT_ID (FK)
MANAGER_ID
LOCATION_ID (FK)
LOCATIONS
LOCATION_ID (PK)
STREET_ADDRESS
POSTAL_CODE
CITY
STATE_PROVINCE
COUNTRY_ID (FK)
COUNTRIES
JOBS
JOB_ID (PK)
JOB_TITLE
MIN_SALARY
MAX_SALARY
COUNTRY_ID (PK)
COUNTRY_NAME
REGION_ID (FK)
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
101
102
101
103
ID
AGE
22
49
16
30
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
COMMIT issued
COMMIT complete
KEY (job_id);
Dropping a Table
Dropping a table removes:
Data
Table structure DROP TABLE hr.employees PURGE;
Database triggers
Corresponding indexes
Associated object privileges
CASCADE
CONSTRAINTS:
referential integrity constraints
PURGE: No flashback possible
Dependent
Truncating a Table
Truncating a table removes the data and releases
used space.
Corresponding indexes are truncated.
TRUNCATE TABLE hr.employees;
Schema
Constraints
> Indexes
Views
Sequences
Temp Tables
Data Dict
Indexes
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
Bitmap Indexes
Table
File 3
Block 10
Block 11
Index
Block 12
Start
End
Bitmap
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 functions
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
Schema
Constraints
Indexes
> Views
LOCATION table
join
AU_BR_VIEW view
COUNTRY table
Creating Views
Sequences
A sequence is a mechanism for automatically
generating integers that follow a pattern.
Schema
Constraints
Indexes
Views
> Sequences
Temp Tables
Data Dict
Creating a Sequence
Using a Sequence
SQL> CREATE TABLE
orders (id NUMBER,
ord_date DATE,
prod_id NUMBER,
prod_desc
VARCHAR2(30) );
Table created.
SQL> INSERT INTO orders VALUES (
abc_seq. sysdate, 1245009, 'Gizmo X');
1 row created.
Temporary Tables
A temporary table:
Schema
Constraints
Indexes
Views
Sequences
> Temp Tables
Data Dict
Summary
In this lesson, you should have learned
how 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