Module 15 - Synonyms, Sequences and Views
Module 15 - Synonyms, Sequences and Views
htm; updated July 8, 2013; Some figures shown in these notes are from Oracle document
D11321GC11. Ref: Oracle Database Administrator's Guide 11g Release 2 (11.2) E25494-03
Synonyms
Facts about Synonyms
Synonyms are a form of database shorthand.
Individuals with the CREATE PUBLIC SYNONYM privilege can also create
synonyms for objects in other schemas. Example:
Synonym created.
Public synonyms must be unique names for this reason DBAs tend to
discourage the creation of public synonyms. The CREATE PUBLIC
SYNONYM privilege will usually not be granted to regular system users.
Using Synonyms
Private synonyms (your schema) and public synonyms can be used in DML
statements if you have the necessary privileges to access the underlying
objects.
Example:
You have SELECT and INSERT privileges for the table PRODUCT.
You can use the synonym PROD to both SELECT and INSERT rows from
the table.
Synonym created.
1 row created.
PRODUCT_NU DESCRIPTION
WHOLESALE_PRICE SALE_PRICE
RETAIL_PRICE
Large Desk
276.60
599.95
2
120.00
Small Desk
138.00
429.95
3
14.75
Tiny Desk
16.96
100.50
4
599.99
Ornate Desk
459.55
Example:
1000.99
The user named Susan has SELECT but not INSERT privileges on
PRODUCT.
CONN SUSAN/pa$$w0rd;
PRODUCT_NU DESCRIPTION
WHOLESALE_PRICE SALE_PRICE
RETAIL_PRICE
Large Desk
276.60
599.95
2
120.00
Small Desk
138.00
429.95
3
14.75
Tiny Desk
16.96
100.50
4
599.99
Ornate Desk
459.55
1000.99
ERROR at line 1:
ORA-01031: insufficient privileges
Sequences
Facts About Sequences
Sequences are special database objects used to generate numbers in
sequential order, typically to be stored as values in data rows for a data
table.
Primary use: To generate unique key values for tables that can be used
to link to other tables or that will serve as primary keys (sequence
generated primary keys are termed surrogate keys).
This speeds access, but loses the cached numbers if the database is
shut down.
When the last cached number is used, a new set is created into
cache.
Sequence created.
Using Sequences
In order to use a sequence, you must first generate the initial sequence
number by using the NEXTVAL option.
1 row created.
Display the data rows from TestOrders note the first data row was added
during an earlier lab assignment. The second data row added today by
using the sequence.
75.95
1 08-JUL-13
100.42
The CURRVAL option returns the current sequence number, but will not
execute unless the sequence has been called at least one time using the
NEXTVAL option.
1 row created.
Display the data rows from TestOrderDetails note the first two data rows
were added during an earlier lab assignment. The third data row was
added today by using the sequence.
PRODUCTID
ORDERID QUANTITY_ORDERED
ITEMPRICE
111
50
66666
111
25.95
14985
50.21
If you use NEXTVAL and CURRVAL in the same SQL statement, both of
these values will be the value retrieved by NEXTVAL.
Gaps may occur in sequence numbers received for use since other users
may access the same sequence.
A subquery
When sequence values are "read" they are stored to the sequence cache
in the SGA. You want these values to be accessible quickly.
After these values are used, value #41 through #80 are next generated and
stored.
Choose a high value for CACHE to result in fewer reads from disk to the
sequence cache, recognizing that in an instance failure some sequence
values are likely to be lost.
You cannot alter the START WITH clause without dropping and
recreating the sequence.
Sequence dropped.
Views
Facts about Views
Views are virtual tables that is, they are a logical representation of a
collection of columns from one or more (related) base tables, materialized
views, or other views. Views are defined in a way such that they make it
easier for a user to access needed information from a database.
Creating a view of the data from these tables provides the customer
a single "object" with which to process information about a sales
order.
As another example, consider the "tables" you access that comprise the
data dictionary. Most of these are actually views of the data.
Advantages of Views
Views are efficient unless views are stacked, i.e. one view
references another view - then performance problems can occur.
View privileges:
A view owner must have been granted explicit privileges to access all
objects referenced in a view definition these privileges cannot be
obtained through roles.
For a view owner to grant view access to others the owner must have
received object privileges on the base objects with the GRANT
OPTION or the system privileges with the ADMIN OPTION.
Example:
User1 has the SELECT and INSERT privileges for the Employee
table owned by User2.
Any view on the Employee table created by User1 can only support
SELECT and INSERT operations.
DELETE and UPDATE operations are not supported.
This example uses the Product table in the DBOCK schema. Here is a
description of the table.
DESC Product;
Name
Null?
Type
DESCRIPTION
RETAIL_PRICE
NUMBER(8,2)
WHOLESALE_PRICE
NUMBER(8,2)
SALE_PRICE
NUMBER(8,2)
PRODUCT_NUMBER DESCRIPTION
SALE_PRICE
Large Desk
276.60
Small Desk
138.00
Tiny Desk
Ornate Desk
16.96
459.55
View created.
You can retrieve data rows from a view using the standard SELECT
statement. Note that only more expensive products are listed.
PRODUCT_NUMBER DESCRIPTION
SALE_PRICE
Large Desk
276.60
Small Desk
138.00
Ornate Desk
459.55
1 row created.
The rows from the underlying Product table include the new product. Note
that the new row has no Retail_Price or Wholesale_Price values they
are NULL values for these columns were not inserted through the view
and there is no default value for those columns.
PRODUCT_NUMBER DESCRIPTION
WHOLESALE_PRICE SALE_PRICE
RETAIL_PRICE
Large Desk
276.60
599.95
2
120.00
Small Desk
138.00
429.95
3
14.75
Tiny Desk
16.96
100.50
4
599.99
Ornate Desk
459.55
6
495.95
1000.99
Executive Desk
ERROR at line 1:
ORA-01402: view WITH CHECK OPTION where-clause
violation
JOIN Views
A JOIN VIEW includes column data from more than one base table. This
example joins the Course and Section tables created during an earlier lab
assignment.
View created.
This selects rows from both the Course, Section, and Course_Section_VW
tables/views. Can you determine why the view only has two rows?
COURSE_ DESCRIPTION
SECTION_YEAR
SECTION_NUMBER SECTIO
111111 Summer
222222 Fall
111112 Fall
111113 Summer
COURSE_ DESCRIPTION
HOURS
222222 Fall
111112 Fall
111113 Summer
Join views are updateable, but there are a number of restrictions regarding
updating this type of view. For example, only one underlying table can be
modified by an INSERT, UPDATE, or DELETE operation. Rules governing
an updateable join view are covered in detail in the assigned readings and
later in this note set.
To alter a view in another schema you need the ALTER ANY TABLE
system privilege to execute an ALTER VIEW command. Example:
View altered.
Use the DROP VIEW command to drop a view that is no longer needed.
To drop a view from another schema you need the DROP ANY VIEW
system privilege.
View dropped.
If a view omits a NOT NULL column (that does not have a DEFAULT
clause), then INSERT operations cannot be used through the view.
ERROR at line 1:
ORA-01400: cannot insert NULL into
("DBOCK"."PRODUCT"."DESCRIPTION")
A join view can be updated, but is limited to updating only one base
table participating in the view. There are numerous rules regarding
updating a join view.
o UPDATE updatable columns must map to columns of a keypreserved table that is a table where the key can also be a
key of the virtual table resulting from the join.
o DELETE rows from a join view can be deleted providing there
is only one key-preserved table in the join a view created with
the WITH CHECK OPTION clause with a key preserved table
that is repeated cannot have the rows deleted through the view.
o INSERT rows inserted cannot reference columns of any nonkey-preserved table. INSERT operations are not permitted if
the join view is defined with the WITH CHECK OPTION clause.
Example: The first query shows which columns are updatable from the
Products_Over100_VW view. Note the use of the Table_Name column
name from the DBA_Updatable_Columns view even though it is a view
that is being evaluated. The second query evaluates the
Course_Section_VW view it has columns that are not updatable.
COLUMN_NAME
UPD
------------------------------ --PRODUCT_NUMBER
YES
DESCRIPTION
YES
SALE_PRICE
YES
COLUMN_NAME
UPD
------------------------------ --COURSE_NUMBER
NO
DESCRIPTION
NO
SECTION_NUMBER
YES
SECTION_TERM
YES
SECTION_YEAR
YES
END OF NOTES