Lab # 7 View, Sequence, Synonym and Trigger Eng. Alaa O Shama

Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

The Islamic University of Gaza

Faculty of Engineering

Department of Computer Engineering

ECOM 4113: Database Lab

Lab # 7

View, Sequence, Synonym and Trigger

Eng. Alaa O Shama

December, 2015
Objectives:
In previous labs we have talked about database objects like: tables,
views, sequence, synonym, and triggers.

We learned tables in details like creating, altering, inserting into and


retrieving data from them.

In this lab we will cover the other database objects and take example
for each object.

Views (Virtual Tables):


In SQL, a view is a virtual table based on the result-set of an SQL
statement.

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.

We can think of a view as a way of specifying a table that we need to


reference frequently. Even though it may not exist physically. For
example, referring to the COMPANY database we may frequently issue
queries that retrieve the employee name, his department and the City
where he works on. Rather than having to specify the join of three
tables EMPLOYEES, DEPARTMENTS, and LOCATIONS every time we
issue this query, we can define a view that is specified as the result of
these joins. Then we can issue queries on the view, which are specified
as a single table retrievals rather than as retrievals involving two joins
on three tables.

We call the EMPLOYEES, DEPARTMENTS, and LOCATIONS tables the


defining tables of the view.

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;

The syntax of creating a view:

View Syntax
CREATE [OR REPLACE] VIEW VIEW NAME AS SELECT STATEMENT;

Now, our view will be like this:

It will be added there:

Now to retrieve the data:


And you can add a condition to your query

Example:
Create Department_info view with Department name, number of
employees and total salary for each.

Note that the View code is stored in the database, in “USER_VIEWS”


view. To display it, use this statement:
You can delete a view with the DROP VIEW command

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:

 CURRVAL: Returns the current value of a sequence.

 NEXTVAL: Increments the sequence and returns the next value.

You must qualify CURRVAL and NEXTVAL with the name of the
sequence:

 sequence.CURRVAL

 sequence.NEXTVAL

To create a sequence, use the following syntax:

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.

Within a single SQL statement containing a reference to NEXTVAL,


Oracle increments the sequence once.

Example:

Using Sequence Values


SELECT SEQ.NEXTVAL FROM DUAL; -- RETURN 1
SELECT SEQ.NEXTVAL FROM DUAL; -- RETURN 2
SELECT SEQ.NEXTVAL FROM DUAL; -- RETURN 3
SELECT SEQ.CURRVAL FROM DUAL; -- RETURN 3
SELECT SEQ.CURRVAL FROM DUAL; -- RETURN 3
SELECT SEQ.NEXTVAL FROM DUAL; -- RETURN 4

If you want to use the sequence value for Employees' table primary
key then it will be like:

The last employee id is 206

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;

Then use the original object with the new name.


Triggers:
A trigger is a PL/SQL block or a PL/SQL procedure associated with a
table, view, schema, or database. Executes implicitly whenever a
particular event takes place and condition satisfies.

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:

1. Department 50 needs access to its employee data. Create a view


named DEPT50 that contains the employee numbers, employee
last names, and department numbers for all employees in
department 50. You have been asked to label the view columns
EMPNO, EMPLOYEE, and DEPTNO.
(Display the contents of the DEPT50 view)

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.

(insert two rows in the DEPT table to test the sequence)

3. Create a synonym for your EMPLOYEES table. Call it EMP.

4. Create trigger after insertion in Employee table, if the hire_date


year is 2015 insert that employee in NEW_EMP table and if the
hire_date year < 2015 insert that employee in OLD_EMP table.
You must first create NEW_EMP and OLD_EMP tables and check
that your trigger executes.

You might also like