Lab # 7 View, Sequence, Synonym and Trigger Eng. Alaa O Shama
Lab # 7 View, Sequence, Synonym and Trigger Eng. Alaa O Shama
Lab # 7 View, Sequence, Synonym and Trigger Eng. Alaa O Shama
Faculty of Engineering
Lab # 7
December, 2015
Objectives:
In previous labs we have talked about database objects like: tables,
views, sequence, synonym, and triggers.
In this lab we will cover the other database objects and take example
for each object.
A view contains rows and columns, just like a real table. The fields in a
view are fields from one or more real tables in the database.
You can add SQL functions, WHERE, and JOIN statements to a view
and present the data as if the data were coming from one single table.
Our query is
select First_name,last_name,DEPARTMENT_name,City
from EMPLOYEES E,DEPARTMENTS D,LOCATIONS L
where E.department_id=D.DEPARTMENT_ID and
D.Location_id=L.LOCATION_ID;
View Syntax
CREATE [OR REPLACE] VIEW VIEW NAME AS SELECT STATEMENT;
Example:
Create Department_info view with Department name, number of
employees and total salary for each.
Sequence:
A sequence is a schema object that can generate numeric values.
These values are often used for primary and unique keys. You can
refer to sequence values in SQL statements with these pseudo
columns:
You must qualify CURRVAL and NEXTVAL with the name of the
sequence:
sequence.CURRVAL
sequence.NEXTVAL
Syntax
CREATE SEQUENCE SEQUENCE_NAME [START WITH N] [INCREMENT BY M];
By default, the sequence starts with 1 and the increment value for it is
1.
How to Use Sequence Values
When you create a sequence, you can define its initial value and the
increment between its values.
The first reference to NEXTVAL returns the initial value of the
sequence. Subsequent references to NEXTVAL increment
the sequence value by the defined increment and return the new
value. Any reference to CURRVAL always returns the current value of
the sequence, which is the value returned by the last reference to
NEXTVAL.
Before you use CURRVAL for a sequence in your session, you must
first initialize the sequence with NEXTVAL.
Example:
If you want to use the sequence value for Employees' table primary
key then it will be like:
Now, our sequence must start with 207 and increment by 1(default).
Insert statement will be:
After insertion:
Synonyms:
Synonyms is an alternative name for a table, view, sequence, operator,
procedure, stored function, package, materialized view, Java class
schema object, user-defined object type, or another synonym.
The Difference between rename, aliasing and synonyms:
Aliasing is just for the query where it's used, not general.
Rename command will rename the table in the database.
Synonym will not rename the table in database.
To create a synonym for any database object, use the following syntax:
Synonym’s Syntax
CREATE [OR REPLACE] SYNONYM SYNONYM_NAME FOR ORIGINAL_OBJECT;
Syntax is:
Trigger’s Syntax
CREATE [OR REPLACE] TRIGGER TRIGGER_NAME
TIMING
EVENT1 [OR EVENT2 OR EVENT3]
ON OBJECT_NAME
[
[REFERENCING OLD AS old | NEW AS new]
FOR EACH ROW
[WHEN (CONDITION)]
]
TRIGGER_BODY
The trigger type determines whether the body executes for each row
or only once for the triggering statement.
- A statement trigger:
Executes once for the triggering event
Is the default type of trigger
Fires once even if no rows are affected at all
- A row trigger:
Executes once for each row affected by the triggering event
Isn't executed if the triggering event doesn't affect any rows
Is indicated by specifying the “FOR EACH ROW” clause
Trigger Timing: When should the trigger fire?
BEFORE: Execute the trigger body before the triggering DML event
on a table.
AFTER: Execute the trigger body after the triggering DML event on
a table.
INSTEAD OF: Execute the trigger body instead of the
Triggering statement.
This is used for views that are not otherwise modifiable.
A trigger event:
Determines which DML statement causes the trigger to execute.
Types are:
INSERT
UPDATE [OF column]
DELETE
A trigger body:
Is a PL/SQL block or a CALL to a procedure. It determines what action is
performed.
Example:
write a trigger that is fire when someone is trying to modify an employee’s
salary. The trigger will insert the values of the old salary and the new salary
into an AUDIT table named “AUDIT_EMP”.
1. Create “AUDIT_EMP” table.
2. Create trigger “TRACK_SAL_CHANGES”
Now try to issue some “UPDATE statements” and notice the changes
on table “AUDIT_EMP”
In AUDIT_EMP table:
Exercises:
2. You need a sequence that can be used with the PRIMARY KEY
column of the DEPT table. The sequence should start at 200 and
increment by 10. Name the sequence DEPT_ID_SEQ.