PLSQL
PLSQL
Example:
Students(sid: string, name: string, login: string, age: integer,
gpa: real)
An instance of the Students relation appears in Figure 3.1
The degree, also called arity, of a relation is the number
of fields
We can delete all Students tuples with name equal to Smith
using the command:
UPDATE Students
SET age = age + 1, gpa = gpa - 1
WHERE sid = 53688
INTEGRITY CONSTRAINTS OVER RELATIONS
Key Constraints
Example:
CREATE TABLE Enrolled
(
cid VARCHAR2(20) PRIMARY KEY,
grade VARCHAR2(2),
sid NUMBER(5),
FOREIGN KEY(sid) REFERENCES Students(sid)
)
General Constraints
We may require that student ages be within a certain range
of values
Given such an IC specification, the DBMS will reject
inserts and updates that violate the constraint
We can compute the names and logins of students who are
younger than 18 with the following query:
SELECT S.name, S.login FROM Students S
WHERE S.age < 18
Second Method
CREATE TABLE Dept_Mgr
(
did NUMBER(2) PRIMARY KEY,
dname VARCHAR2(20),
budget NUMBER(10,2),
since DATE,
ssn VARCHAR2(11),
FOREIGN KEY(ssn) REFERENCES Employees(ssn)
)
Approach2:
The Sponsors relation contain fields did, pid and since with
(pid,did) as primary key
The Monitors relation contain attributes ssn, did, pid and
until with (ssn,pid,did) as primary key
INTRODUCTION TO VIEWS
A view is a table whose rows are not explicitly stored in
the database but are computed as needed from a view
definition
In finding the names, cid's and sid's of students who got a
grade B in some course
If the optional arguments name, sid, and course are omitted
from the CREATE VIEW statement, the column names
sname, sid, and cid are inherited
For example, we can find out all sailor names and ratings
by using π
Union
Intersection
Set-difference
Cross-product
Union Operation( )
Set-difference Operation( )
Examples
sid
22
1. Find the sid's who are present in S1 and S2
28
πsid(S1) πsid(S2) result: 31
44
2. Find the sid's who are present in both S1 and S2
sid
πsid(S1) πsid(S2) result: 31
58
No two fields in the result must have the same name
Condition Joins
Equijoin
Natural Join
Division
(OR)
(Q7) Find the names of sailors who have reserved all boats
(Q8) Find the names of sailors who have reserved all boats
called Interlake
RELATIONAL CALCULUS
The result of this query is the set of all tuples t for which
the formula p(T) evaluates to true with T = t
Example:
(Q) Find all sailors with a rating above 7
Syntax of TRC Queries
(Q) Find the names of sailors who have reserved a red boat
(Q) Find the names of sailors who have reserved all boats
(Q) Find sailors who have reserved all red boats
Domain Relational Calculus
(Q) Find the names of sailors who have reserved boat 103
(Or)