View Index Stored Procedure and Trigger
View Index Stored Procedure and Trigger
Concepts in SQL
Kuribachew Gizaw(PhD)
October
2023
Outline
View
Indexes and hashes
Stored Procedure
Triggers
Views and Creating views using SQL
A view in SQL terminology is a single table that is derived from
other tables
A view does not exist in the physical table, it is considered as a
virtual Table
This limits the possible update operations that can be applied to
view
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.
Views and Creating views using SQL
Syntax to create View
CREATE VIEW <view name> [ (<column name>{,<column name>})] AS
<select statement>
Syntax to drop View
DROP VIEW <view name>
Indexing and Hashing
Creating indexes using SQL
Indexing mechanisms used to speed up access to desired data.
E.g., author catalog in library
Search Key - attribute to set of attributes used to look up records in a file.
An index file consists of records (called index entries) of the form
search-key pointer
Index files are typically much smaller than the original file
Two basic kinds of indices:
Ordered indices: search keys are stored in sorted order
Hash indices: search keys are distributed uniformly across “buckets” using a “hash function”.
Ordered Indices
In an ordered index, index entries are stored sorted on the search key value. E.g.,
author catalog in library.
Primary index: in a sequentially ordered file, the index whose search key specifies the
sequential order of the file.
Also called clustering index
The search key of a primary index is usually but not necessarily the primary key.
Secondary index: an index whose search key specifies an order different from the
sequential order of the file. Also called
non-clustering index.
Index-sequential file: ordered sequential file with a primary index.
Primary and Secondary Indices
Residents
IDNu FNAme LNAMe SEX PhoneNumb SubCity
mbe er
r
101 Almaz Gebeya F 0911123456 Gullele
w
102 Beken Tulu M 0989786756 Bole
103 Fereweyen Hagos F 0923456789 Yeka
i
104 Sarem Neri F 0912435678 Gullele
105 Hasenan Handeb F 0923436789 Bole
o
1.
Do update with case by inserting
Grade attribute
>85 A
>75 B
>65 C
>55 D
<F
Query: Get all Students whose name
starts with ‘L’,
select * from student
where Fname like 'L%‘
[from F to M],
ends with D?
Query: List all students whose mark
is between 50 and 70
Each one mark
II. Create a view of Female Residents
III. Create an index by Phone Number
IV.update the Fname of
IDNumber=101 by Hana
V. Delete the first row
Active Database and Triggers
1. The event(s) that triggers the rule: These events are usually
database update operations(Insert, delete, modify) that are explicitly
applied to the database.
However, in the general model ,they could also be temporal events2 or other
kinds of external events.
2. The condition that determines whether the rule action should
be executed:
Once the triggering event has occurred, an optional condition may be
evaluated. If no condition is specified, the action will be executed once the
event occurs. If a condition is specified, it is first evaluated, and only if it
evaluates to true will the rule action be executed.
3. The action to be taken: The action is usually a sequence of SQL
statements, but it could also be a database transaction or an external
program that will be automatically executed.
Creating Triggers using SQL
stored procedures are program modules that are stored by the DBMS
at the database server.
These extensions are known as SQL/PSM (SQL/Persistent Stored
Modules) and can be used to write stored procedures.
SQL/PSM also serves as an example of a database programming
language that extends a database model and language—namely, SQL
—with some programming constructs, such as conditional statements
and loops.
More involved procedures to enforce rules are popularly called stored
procedures; they become a part of the overall database definition and
are invoked appropriately when certain conditions are met.
Creating Stored Procedures using
SQL
General Syntax to create a Stored Procedure
Create Procedure Procedure_Name
As
Begin
Select
From
Where
End
To execute the Procedure
Exec Procedure_Name;
Create Stored Procedure Example
Create A Procedure named Tax for the price where Tax =3%
of the price
Declare Tax INT
Create A Procedure TaX
As
BEGIN
Set Tax = (Select Price
From Product
where tax =price*
END
Create the following Table
Employee
Create A Procedure named Tax for the price where Tax =3%
of the price
Declare Tax INT
Create A Procedure TaX
As
BEGIN
Set Tax = (Select Price
From BooK
where tax =price*3%;
END
Create Stored Procedure Example
Using IF/Else Statement