Database Chapter 3 by Hatem
Database Chapter 3 by Hatem
الفصل الثالث
THE RELATIONAL MODEL
النموذج العالئقي
DELETE
FROM Students S
WHERE s.st_name='Hatem Moharram‘
we can modify the column values in an existing row using
the UPDATE command.
Ex.: we can increment the age and decrement the gpa of the
student with sid 10002:
UPDATE Students S
SET S.age=S.age+1, S.gpa=S.gpa-1
WHERE s.sid=10002
The WHERE clause is applied first and determines which
rows are to be modified.
The SET clause then determines how these rows are to be
modified.
UPDATE Students S
SET S.gpa = S.gpa - 1
WHERE S.gpa >=3.3
gpa age login St_name Sid
4 30 Shote حاتم محرم 10002
1.2 22 zhat سعد مسعود 10003
3.1 32 ttot فرهود فهمي 10004
2.3 24 tfot غالب مغلوب 10005
3.4 25 tyto ابو سلومة األقرع 10006
Enrolled Students
cid grade sid sid St_name login age gpa
Comp01 A 566 566 Hatem tot 20 2.0
Comp02 B 456 300 Aly tat 19 3.3
Comp03 A 671 671 Saad tet 30 3.8
Math002 C 921 303 Kalil fah 25 2.0
456 Hassan fpr 22 3
921 Mohamed got 21 2
INSERT
INTO Enrolled (cid, grade, sid)
VALUES (comp01, 'B', 333)
Enrolled Students
cid grade sid sid St_name login age gpa
Comp01 A 566 566 Hatem tot 20 2.0
Comp02 B 456 300 Aly tat 19 3.3
Comp03 A 671 671 Saad tet 30 3.8
Math002 C 921 303 Kalil fah 25 2.0
456 Hassan fpr 22 3
921 Mohamed got 21 2
S is a variable that
sid St_name login age gpa
takes on the value
566 Hatem tot 20 2.0 of each tuple in
300 Aly tat 19 3.3 Students, one tuple
671 Saad tet 30 3.8 after the other.
303 Kalil fah 25 2.0
the answer to this query
sid St_name login age gpa
566 Hatem tot 20 2.0
300 Aly tat 19 3.3
Ex.: fined the names and logins of students who are younger
than 21.
SELECT S.st_name, S.login
FROM Students S
WHERE S.age <= 20
sid St_name login age gpa
566 Hatem tot 20 2.0
300 Aly tat 19 3.3
671 Saad tet 30 3.8
303 Kalil fah 25 2.0
the answer to this query
it is obtained by applying the
selection to the instance <= 20 of St_name login
Students followed by removing hatem tot
aly tat
unwanted fields.
Ex.: fined the names of all students who obtained an A and
the id of the course in which they got an A.
SELECT S.st_name, E.cid
FROM Students S, Enrolled E
WHERE S.sid=E.sid AND E.grade=’A’
Enrolled Students
cid grade sid sid St_name log age gpa
in
Comp01 A 566 566 Hatem tot 20 2.0
Comp02 B 456 300 Aly tat 19 3.3
Comp03 A 671 671 Saad tet 30 3.8
Math002 C 300 303 Kalil fah 25 2.0
456 Hassan fpr 22 3
cid St_name
Comp01 Hatem
Comp03 Saad
3.5 LOGICAL DBASE DESIGN: ER TO RELATIONAL
Here we describe how to translate an ER diagram into a
collection of tables with associated constraints, that is, a
relational database schema.
Employees
Supervisor Subordinate
Reports_To
B R A
c2 C c1
First Approach:
since
ssn name lot did dname budget
hours_worked contractid
Manages
hourly_wages Hourly_Emps Contact_Emps
Monitors until
Purchaser Beneficiary
Policy
policyid cost
CREAT TABLE Policies (policyid INTEGER
ssn CHAR(11) NOT NULL,
cost REAL,
PRIMARY KEY (policyid),
FOREIGN KEY (ssn) REFERENCES Employees,
ON DELETE CASCADE)
ssn name lot pname age
Employees Dependents
Purchaser Beneficiary
Policy
policyid cost
CREAT TABLE Dependents (pname CHAR(20)
age INTEGERE,
policyid INTEGER,
PRIMARY KEY (pname,policyid)
FOREIGN KEY (policyid) REFERENCES Policies
ON DELETE CASCADE )
3.6 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.