Chapter 1
Chapter 1
1. Tables
2. View
3. Indexes
4. Sequences
2
2.1 Tables
Tables can be created at any time, even when users are using the database. The
structure of a table can be changed dynamically.
To name the tables and columns, we have to use standard naming rules for any
object in an Oracle database.
The names of tables and columns must start with a letter and have a length of 1 to 30
characters.
Names must contain only the characters A-Z, a-z, 0-9, _ (underscore), $ and # (legal
characters, but their use isn’t recommended).
Two objects belonging to the same user of the Oracle database cannot have the same
name.
Names must not be Oracle server reserved words.
3
2.2 View
You can present logical subsets or combinations of data by creating a view.
A view is a logical table based on one or more tables.
A view does not contain data, but is like a window through which data can be
seen or changed.
The table on which the view is based is called the base table.
The view is stored as a SELECT query in the data dictionary.
4
2.2 View
5
2.2 View
View in two words: virtual tables.
View in one sentence: a view is a table that is the result of a query (SELECT)
to which we have given a name.
The view definition is saved in the database, but the data corresponding to the
view are not.
At two separate times, the "content" of a view may change (if the content of
the tables that go into the description of the view has changed). In fact, the
content of a view is re-executed each time SQL uses the view.
6
2.2 View
View Creation:
7
2.2 View
Keyword Description
OR REPLACE Recreate view if it exists
FORCE Create the view even if the base tables do not exist
NOFORCE create the view only if the base tables exist (default option)
8
2.2 View
Example: The example creates a view that contains the employee ID, first
name, and salary for each employee in department 80.
You can display the structure of the view using the DESCRIBE command.
9
2.2 View
Column aliases in the subquery
You can control column names by including aliases in the subquery.
In this example, a view is created, it contains the employee id (EMPLOYEE_ID) with
the alias ID_NUMBER, the name (LAST_NAME) with the alias NAME, and the annual
salary (SALARY) with the alias ANN_SALARY for each employee in department 50.
10
2.2 View
As an alternative, you can use aliases between the CREATE statement and the
SELECT subquery.
The number of aliases must be the same as the number of attributes selected
in the subquery.
11
2.2 View
Retrieve data from a view: it is possible to retrieve data from a view as
from any table and display all the columns of the view or specify the rows and
columns.
12
2.2 View
View modification :
With the OR REPLACE option, a view can be created even if another already exists
with the same name, the old view is replaced by the new one.
Attention !
When defining aliases in the CREATE OR REPLACE VIEW clause, remember
that their order must match the order of the columns in the subquery.
13
2.2 View
Deleting a View
14
2.2 View
Four reasons to use views:
Macro effect: replace a complicated query with simpler queries.
Confidentiality: Views restrict access to data because they can only display certain
columns in a table.
Integrity Constraints: Views provide groups of users with access to particular data.
Increase logical independence: a view can be used to retrieve data from many tables.
15
2.2 View
There are two types of views: simple and complex. The difference is related to
DML operations (INSERT, UPDATE, DELETE).
A simple view is a view that:
retrieves data from a single table.
does not contain any functions or data groups.
allows execution of DML operations.
A complex view is a view that:
retrieves data from multiple tables.
contains functions or groups of data.
does not always allow DML operations to be performed.
16
2.2 View
17
2.2 View
Complex view creation
The following example creates a complex view that retrieves the department name,
minimum salary, maximum salary, and average salary for each department.
Note that alternative column names have been specified in the view. This is required if
a column in the view is derived from a function or expression.
18
2.2 View
Display the view contents using a SELECT query.
19
2.2 View
The clause WITH CHECK OPTION
The WITH CHECK OPTION clause specifies that INSERTs and UPDATEs executed
through the view cannot create rows that the view cannot select.
20
2.2 View
This reinforces integrity constraints and data checks during inserts or updates.
If there is an attempt to perform a DML operation on unreachable lines
through the view, an error is displayed (with the constraint name if it has been
defined).
Generated error
21
2.2 View
No row is updated because if the DEPARTMENT_ID was changed to 10, the
view would no longer be able to see this employee. With the WITH CHECK
OPTION clause, the view can only see employees in department 20. Changing
DEPARTMENT_ID is therefore not allowed for employees through the view.
22
2.2 View
Prohibition of DML operations
It is possible to prohibit any DML operation on a view by creating it with the WITH
READ ONLY option.
In the following example, the EMPVU10 view is changed to prevent execution of any
DML command.
23
2.2 View
Any attempt to delete a row from a view with the read-only constraint
(WITH READ ONLY) returns an error:
Any attempt to insert or modify a row in a view with the read-only constraint
(WITH READ ONLY) returns an error:
24
2.3 Sequences
A sequence is a database object created by one user that can be shared with multiple
users to generate whole numbers.
The typical use of sequences is to create values for primary keys, which must be
unique for each row.
Sequences of numbers are stored and generated independently of tables. However, a
sequence can be used for multiple tables.
25
2.3 Sequences
Sequence creation
To generate the sequential numbers, we use CREATE SEQUENCE
26
2.3 Sequences
27
2.3 Sequences
In the example below a DEPT_DEPTID_SEQ sequence is created for use with the
DEPARTMENT_ID column of the DEPARTMENTS table.
The sequence starts at 120, caching and cycling are disabled.
Do not use the CYCLE option in a sequence used to generate the values for a
primary key.
28
2.3 Sequences
The following example inserts a new department in the DEPARTMENTS table. It
uses the DEPT_DEPTID_SEQ sequence to generate a new number.
29
2.3 Sequences
Suppose we want to add an employee in the newly created department. For this, it is
necessary to insert a row in the EMPLOYEES table, the sequence dept_deptid_seq
can be used to insert the department number.
30
2.3 Sequences
Sequence modification
If the limit value is reached (MAXVALUE), no new value will be generated by the last
one and an error message will be returned indicating that the sequence exceeds the
MAXVALUE limit.
To continue using the sequence, we can modify it using the following query:
31
2.3 Sequences
Drop a sequence
Example:
32
2.4 Index
Indexes are the objects that you create to improve the performance of certain
queries. Indexes are created automatically for all primary and unique keys.
An index is an object that can speed up finding rows by using a pointer. Indexes can
be created explicitly or automatically. If you don't have an index on a column, a full
table crawl is performed during a search.
An index provides direct and fast access to the rows of the table. Its role is to
reduce the number of Inputs/Outputs (I/O) on a disk by using an indexed path to
find data quickly.
33
2.4 Index
An index is used and maintained automatically by the Oracle server.
After the index has been created, no special intervention is required on the part of
the user.
Indexes are independent of the table they index at the logical and physical level. This
means that they can be created or deleted at any time and have no effect on base
tables or other indexes.
When you drop a table, the corresponding indexes are also dropped.
34
2.4 Index
Index type
Two types of index can be created:
Unique index: The Oracle server automatically creates this index when you define a column with the
PRIMARY KEY constraint or the UNIQUE constraint. The name of the index is the name given to the
constraint.
Non-unique index: This is an index that the user can create. For example, you can create an index for the
FOREIGN KEY column of a table to improve the performance of queries that use joins.
You can manually create a unique index, but it is recommended that you create a UNIQUE
constraint than creating a unique index implicitly.
35
2.4 Index
Drop Index
If you drop a table, the associated indexes and constraints will automatically be
dropped, but views and sequences will not.
36