Schema Relational Algebra Soln

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

CSCB20 Worksheet – database schema and relational algebra1

1 Database Schema Diagrams


We will use the university sample relational schema defined in lectures. The following schema
diagram illustrates all the relations in the database. Each relation has primary keys underlined.
Draw arrows to show all foreign key constraints.

takes student
ID
ID  
name  
course_id  
dept_name  
sec_id

semester
tot_cred
year
grad
section course
course_id course_id
department advisor
sec_id

title   dept_name s_id  


semester   dept_name  



i_id
year   building  
time_slot credits
building   budget
room_no   time_slot_id
time_slot_id day
start_time
end_time

prereq instructor
classroom course_id   ID
building
prereq_id name  

room_no dept_name  
capacity  teaches salary
ID  
course_id  

sec_id
semester
year

2 Relational Algebra
We use relational algebra to specify queries on a database. This is the formal mathematical nota-
tional. Later we will see how this translates into SQL.

Summary of Relational Algebra


1
Diagrams and examples taken from Database System Concepts 6th Edition

1
erations typically take one or two relations as input and return a relation as
output.
Relational algebra is covered in detail in Chapter 6, but we outline a few of
the operations below.

Symbol (Name) Example of Use


! !salary>=85000 (instructor )
(Selection) Return rows of the input relation that satisfy
the predicate.
! !ID,salary (instructor)
(Projection) Output specified attributes from all rows of
the input relation. Remove duplicate tuples
from the output.
✶ instructor ✶ department
(Natural join) Output pairs of rows from the two input rela-
tions that have the same value on all attributes
that have the same name.
× instructor × department
(Cartesian product) Output all pairs of rows from the two input
relations (regardless of whether or not they
have the same values on common attributes)
∪ !name (instructor ) ∪ !name (student )
(Union) Output the union of tuples from the two input
relations.

2.1 and
Lets salary. In this example, we could have performed the operations in either
practice...
order, but that is not the case for all situations, as we shall see.
The universitySometimes,
schema and therelations
result of are at the
a query end ofduplicate
contains this handout.
tuples. For example, if we
select the dept name attribute from the instructor relation, there are several cases
of duplication, including “Comp. Sci.”, which shows up three times. Certain rela-
1. What tional languages
is the result ofadhere
σs id=IDstrictly to the
(student ×mathematical
advisor)? definition of a set and remove
duplicates. Others, in consideration of the relatively large amount of processing
required
2. Explain whattoeach
remove
of theduplicates
following from large result
expressions do: relations, retain duplicates. In
these latter cases, the relations are not truly relations in the pure mathematical
(a) sense
σyear≥2009
of the(takes)
term. o n student
Of course,
(b) σyear≥2009 data
(takes n in
o a database must be changed over time. A relation can be
student)
updated by inserting new tuples, deleting existing tuples, or modifying tuples by
(c) ΠID, name, course id (student o n takes)

3. Suppose we have the following relational database:

employee (person name, street, city)


works(person name, company name, salary)
company (company name, city)

(a) Find the names of all employees who live in city ‘Miami’.

Πperson name (σcity=’Miami’ (employee))

(b) Find the names of all employees whose salary is greater than $100,000.

Πperson name (σsalary>100000 (works))

2
(c) Find the names of all employees who live in ‘Miami’ and whose salary is greater than
$100,000.

Πperson name (σcity=’Miami’∧salary>100000 (employee o


n works))

(d) Find the names of all employees who work for “First Bank Corporation”.

Πperson name (σcompany name=‘First Bank Corporation’ (works))

(e) Find the names and cities of residence of all employees who work for “First Bank Cor-
poration”.

Πperson name,city (σcompany name=‘First Bank Corporation’ (works n employee))


o

(f) Find the names, street address, and cities of residence of all employees who work for
“First Bank Corporation” and earn more than $10,000.

Πperson name,street,city (σcompany name=‘First Bank Corporation’∧salary>10000 (employee n works))


o

4. Suppose we have the following relational database:

branch(branch name, branch city, assets)


customer(customer name, customer street, custromer city)
loan(loan number, branch name, amount)
borrower(customer name, loan number)
account(account number, branch name, balance)
depositor(custromer name, account number)

(a) Underline appropriate primary keys.


Primary keys are bold font. Note that some of this is up to interpretation of the database
- feel free to discuss and come to some agreement. A sample solution might be:

branch(branch name, branch city, assets)


customer(customer name, customer street, customer city) ** customer names may
not be unique**
loan(loan number, branch name, amount) ** assume that loan numbers are across all
branches
borrower(customer name, loan number)
account(account number, branch name, balance)
depositor(customer name, account number)
(b) Given your choice of primary keys, identify appropriate foreign keys. Draw a relation
diagram to indicate foreign keys.
try having the students draw a box for each schema and then draw the relation diagram
for the foreign keys as is done on the diagram on the first page.

3
branch(branch name, branch city, assets)
customer(customer name, customer street, customer city) ** names may not be
unique**
loan(loan number, branch name, amount) ** assume that loan numbers are across all
branches
borrower(customer name, loan number)
account(account number, branch name, balance)
depositor(customer name, account number)

Foreign Keys:

loan and account have foreign key branch name for branch.

borrower has foreign key loan number for loan.


depositor has foreign key account number for account.

(c) Find the names of all branches located in ‘Chicago’.


Πbranch name (σbranch city=’Chicago’ (branch))

(d) Find the names of all borrowers who have a loan in branch ’Down-town’.

Πcustomer name (σbranch name=’Down-town’ (borrower n loan))


o

(e) Find all loan numbers with a loan value greater than $10, 000.

Πloan number (σamount>10000 (loan))

(f) Find the names of all depositors who have an account with a value greater than $6000.

Πcustomer name (σbalance>6000 (depositor n account))


o

3
48
University Relations
Chapter 2 Introduction to the Relational Model
Relations and their schemas:
classroom(building, room number, capacity)
department(dept name, building, budget)
course(course id, title, dept name, credits)
instructor(ID, name, dept name, salary)
section(course id, sec id, semester, year, building, room number, time slot id)
teaches(ID, course id, sec id, semester, year)
student(ID, name, dept name, tot cred)
takes(ID, course id, sec id, semester, year, grade)
advisor(s ID, i ID)
time slot(time slot id, day, start time, end time)
prereq(course id, prereq id)

Figure 2.9 Schema of the


4 university database.
Query languages used in practice include elements of both the procedural and
the nonprocedural approaches. We study the very widely used query language
SQL in Chapters 3 through 5.
There are a number of “pure” query languages: The relational algebra is pro-
As you can imagine, there are many more relations maintained in a real uni-
versity database. In addition to those relations we have listed already, instructor,
department, course, section, prereq, and teaches, we use the following relations in this
text:

ID course id sec id Chapter


40 2 Introduction
semester year to the Relational Model
10101 CS-101 1 Fall 2009
10101 CS-315 1 Spring 2010
ID name dept name salary
10101 CS-347 1 Fall 2009
12121 FIN-201 1 Spring 2010 10101 Srinivasan Comp. Sci. 65000
15151 MU-199 1 Spring 2010 12121 Wu Finance 90000
22222 PHY-101 1 Fall 2009 15151 Mozart Music 40000
32343 HIS-351 40 1
Chapter 2Spring
Introduction2010to the22222
Relational Einstein
Model Physics 95000
45565 CS-101 1 Spring 2010 32343 El Said History 60000
45565 CS-319 1 Spring 2010 33456 Gold Physics 87000
76766 BIO-101 1 Summer 2009ID 45565 name Katz dept name Comp. Sci.salary 75000
76766 BIO-301 1 Summer 2010 58583 Califieri History 62000
10101 76543 SrinivasanSingh Comp.Finance Sci. 65000 80000
83821 CS-190 1 Spring 2009
12121 76766 Wu Crick Finance Biology90000 72000
83821 CS-190 2 Spring 2009
15151 83821 MozartBrandt Music Comp. Sci. 40000 92000
83821 CS-319 2 Spring 2010
22222 98345 Einstein Kim Physics Elec. Eng.95000 80000
98345 EE-181 1 Spring 2009
32343 El Said History 60000
44 Chapter 2 Introduction to the Relational Model
33456 Gold Physics 87000
FigureTeaches
2.7 The teaches relation. 45565 KatzFigure 2.1 Comp.
Instructor
The instructor
Sci. relation.
75000
58583 Califieri History 62000
course id sec id semester year building room number time slot id
the relationship76543 betweenSingh a specifiedFinance 80000
ID and the corresponding values for name,
BIO-101 1 Summer 2009dept Painter name, and salary514
76766 Crick
values. B
Biology 72000
BIO-301 1 Summer 2010 In Painter
general,83821
a row 514
Brandt A
Comp.
in a table represents Sci. 92000 among a set of values.
a relationship
CS-101 1 Fall 2009SincePackard
a table 98345 101
Kim of such Elec.
is a collection H Eng.
relationships, 80000
there is a close correspondence
CS-101 1 Spring 2010between
Packard
the concept101 of table and theFmathematical concept of relation, from whichDatabases
CS-190 1 Spring 2009the relational
Taylor data model 3128 takes its name. E 2.1 Structure of Relational
Figure 2.1 The instructor In mathematical terminology, a tuple is
relation.
CS-190 2 Spring 2009simplyTaylor
a sequence 3128 (or list) of values. A A relationship between n values is repre-
CS-315 1 Spring 2010sentedWatson
mathematically 120by an n-tupleDof values, i.e., a tuple with n values, which
Spring the2010 relationship between a100
specified course id prereq
valuesidfor name,
CS-319 1 Watson
corresponds to a row in a table.ID and B the corresponding
CS-319 2 Spring dept name, and
2010 Taylor salary values. 3128 C BIO-301 BIO-101
CS-347 1 Fall In general,
2009 Taylora row in a3128 table representsA a relationship amongBIO-101
BIO-399 a set of values.
EE-181 1 Spring Since 2009a table is a collection3128
Taylor of such relationships,
C there is a closeCS-101
CS-190 correspondence
FIN-201 1 Spring between2010 the courseofidtable101
concept
Packard title
and the mathematical
B concept
CS-315 dept name
of relation,
CS-101 fromcredits
which
HIS-351 1 Spring the2010 relational data
Painter model
BIO-101 takes
514 its name.
Intro. to Biology In
C mathematicalCS-319 terminology,
Biology CS-101 a tuple
4 is
MU-199 1 Spring simply 2010a sequence
Packard (or list)101
BIO-301 of values. A relationship
Genetics D between
CS-347 Biology values is 4repre-
nCS-101
PHY-101 1 Fall sented
2009mathematically
WatsonBIO-399by 100 an n-tuple of values,
Computational ABiologyi.e., aEE-181
tupleBiology
withPHY-101
n values, 3which
corresponds to aCS-101 row in a table.Intro. to Computer Science Comp. Sci. 4
Section Figure 2.6 The sectionCS-190
relation. Game Design Prereq
Figure 2.3Comp. Sci. relation.
The prereq 4
CS-315 Robotics Comp. Sci. 3
Figure 2.7 shows a sample instance of the course CS-319
id
teaches title Thus,
relation. Image Processing
in the relational model deptthe Comp.
name Sci.
credits is 3used to refer to a table
term relation
As you can imagine, there are manyBIO-101 more CS-347
relations
Intro.
Database
themaintained
term
to
System
tuple in
Biology real Concepts
is aused to
uni-refer to a Comp. Sci.
row. Similarly, 3 term attribute refe
the
versity database. In addition to those relations EE-181
we column
have Intro.
listed ato Digital
ofalready,
table. SystemsBiology
instructor, Elec. Eng.4 3
BIO-301 FIN-201Genetics Investment Banking Biology 4
2.2
department, course, section, prereq, andDatabase
teaches, Schema
we use the Examining
following
43 Figure
relations 2.1,
in thiswe can seeFinance
that the relation 3instructor has four attr
BIO-399 HIS-351Computational
World Biology Biology History 3
text: ID, name, deptHistory
name, and salary. 3
CS-101MU-199 Intro. to Computer
Music Video Science
Production Comp.MusicSci. 4 3
We use the term relation instance to refer to a specific instance of a re
CS-190PHY-101 Game DesignPhysical Principles Comp. Sci. 4
dept name building budget i.e., containing a specific set of rows. Physics 4
The instance of instructor shown in Fig
CS-315 Robotics Comp. Sci. 3
ID course id sec id semester has 12year tuples, corresponding to 12 instructors.
Biology Watson 90000 CS-319 Image Processing Comp. Sci. 3
10101 CS-101 1 Fall Database In2009
this chapter, we shall
FigureConcepts
2.2 be using
The course a number of different relations to illustr
relation.
Comp. Sci. Taylor 100000 CS-347 System Comp. Sci. 3
10101 CS-315 1 Spring various 2010concepts underlying the relational data model. These relations rep
Elec. Eng. Taylor 85000 EE-181 Intro. to Digital Systems Elec. Eng. 3
10101 CS-347 1 part of2009
Fall Investment a university. They do not include all the data an actual university da
Finance Painter 120000 FIN-201 Banking Finance 3
12121 FIN-201 1 Spring would contain, in order to simplify our presentation. We shall discuss crite
History Painter 50000 HIS-351 World 2010 History History 3
15151 MU-199 1 Spring the appropriateness of relational structures in great detail in Chapters 7 an
Music Packard 80000 MU-199 Music 2010 Video Production Music 3
22222 PHY-101 1 Fall Physical The2009order in which tuples appear in a relation is irrelevant, since a re
Physics Watson 70000 PHY-101 Principles Physics 4
32343 HIS-351 1 Springis a set2010 of tuples. Thus, whether the tuples of a relation are listed in sorted
45565 CS-101 1 Spring as in Figure
2010 2.1, or are unsorted, as in Figure 2.4, does not matter; the relat
Department Course
Figure 2.2 The course relation.
Figure 2.5 The45565 relation.
departmentCS-319 1 Spring 2010
76766 BIO-101 1 Summer 2009
similarly the contents of a relation instance
76766mayBIO-301
change with 1time asSummer
the relation 2010 ID name dept name salary
is updated. In contrast, the schema of a83821
relationCS-190
does not generally
1 change.
Spring 2009 22222 Einstein Physics 95000
Although it is important to know 83821
the difference
CS-190between2 a relation
Spring schema 2009 12121 Wu Finance 90000
and a relation instance, we often use the same CS-319
83821 name, such as2 instructor,
Spring to refer
2010 32343 El Said History 60000
to both the schema and the instance. Where
98345 required,
EE-181 we explicitly
1 refer to the
Spring 2009 45565 Katz Comp. Sci. 75000
schema or to the instance, for example “the instructor schema,” or “an instance 5 of 98345 Kim Elec. Eng. 80000
the instructor relation.” However, where it is clear whether we mean the schema 76766 Crick Biology 72000
Figure 2.7 The teaches relation.
or the instance, we simply use the relation name. 10101 Srinivasan Comp. Sci. 65000
Consider the department relation of Figure 2.5. The schema for that relation is 58583 Califieri History 62000
83821 Brandt Comp. Sci. 92000

You might also like