0% found this document useful (0 votes)
55 views9 pages

ASSIGNMENT NO 01 DB

The document discusses relational database concepts and provides examples of applying relational algebra operations to solve queries on given relations. It defines several relations and attributes, explains integrity constraints and domains. It also shows how to determine if relations are union compatible. Finally, it provides solutions to several queries using selection, projection, join, set difference and other relational algebra operations.

Uploaded by

Mohsin Abbasi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views9 pages

ASSIGNMENT NO 01 DB

The document discusses relational database concepts and provides examples of applying relational algebra operations to solve queries on given relations. It defines several relations and attributes, explains integrity constraints and domains. It also shows how to determine if relations are union compatible. Finally, it provides solutions to several queries using selection, projection, join, set difference and other relational algebra operations.

Uploaded by

Mohsin Abbasi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Data Base Systems (CSC402)

ASSIGNMENT NO 01

Submitted to:
DR. MADIHA

Submitted by:
MOHSIN IDREES
SP21-BAI-014
Q1) Consider the following relations and answer the questions given below

 BOOKS(DocId, Title, Publisher, Year)


 STUDENTS(StId, StName, Major, Age)
 AUTHORS(AName, Address)
 BORROWS(DocId, StId, Date)
 HAS-Written(DocId, AName)
 DESCRIBES(Keyword)

a) Identify the attributes and explain the integrity constraints which are
applicable on them for the given relations. (apply this on each relation
individually)

The attributes and applicable integrity constraints for each relation:

 BOOKS (DocId, Title, Publisher, Year):

Attributes:

o DocId: must be unique for each book and not null.


o Title: the title of the book, must not be null.
o Publisher: the publisher of the book, must not be null.
o Year: the year in which the book was published, not be null and must be
valid.

Integrity constrains:

o Primary Key: DocId


o Functional Dependency: DocId → Title, Publisher, Year
o Referential Integrity: References to DocId in BORROWS and HAS-Written
must exist in BOOKS.

 STUDENTS (StId, StName, Major, Age):

Attributes:
o StId: must be unique for each student and not null.
o StName: the name of the student, must not be null.
o Major: the major of the student, must not be null.
o Age: the age of the student, not be null and must be valid.

Integrity constrains:

o Primary Key: StId


o Functional Dependency: StId → StName, Major, Age
o Referential Integrity: References to StId in BORROWS must exist in
STUDENTS.

 AUTHORS (AName, Address):

Attributes:

o AName: the name of the author, must not be null.


o Address: the address of the author, must not be null.

Integrity constrains:

o Primary Key: AName


o Functional Dependency: AName → Address

 BORROWS (DocId, StId, Date):

Attributes:

o DocId: the identifier of the book borrowed, must not be null.


o StId: the identifier of the student who borrowed the book, must not be
null.
o Date: the date when the book was borrowed, not null and must be valid.

Integrity constrains:

o Primary Keys: DocId, StId, Date


o Foreign Keys: DocId references BOOKS(DocId) and StId references
STUDENTS(StId)

 HAS-Written (DocId, AName):

Attributes:
o DocId: the identifier of the book written by the author, must not be null.
o AName: the name of the author who wrote the book, must not be null.

Integrity constrains:

o Primary Key: DocId, AName


o Foreign Keys: DocId references BOOKS(DocId) and AName references
AUTHORS(AName)

 DESCRIBES (Keyword):

Attributes:

o Keyword: describing a book, must be unique and not null.

Integrity constrains:

o Primary Key: Keyword

b) Assign appropriate domain to each attribute belonging to a relation. (Apply


this on all relations)
Domains for each attribute in each relation:

 BOOKS (DocId, Title, Publisher, Year):


o DocId: string
o Title: string
o Publisher: string
o Year: positive integer

 STUDENTS (StId, StName, Major, Age):


o StId: integer
o StName: string
o Major: string
o Age: positive integer

 AUTHORS (AName, Address):


o AName: string
o Address: string

 BORROWS (DocId, StId, Date):


o DocId: string
o StId: integer
o Date: date

 HAS-Written (DocId, AName):


o DocId: string
o AName: string

 DESCRIBES (Keyword):
o Keyword: string

c) After completing part b determine which relations are union compatible and
which are not. (Specify the reasons)

Two table can only be union compatible if and only if:


1) They have same number of attribute/columns.
2) Domain of every column same.
The relations AUTHORS and HAS-Written are union compatible because they have the same
number of attributes and their domains which is “string”. However, All other relations are
not compatible with each other because of difference of numbers of attribute and domains.

Q2) Apply relational algebra concepts on the relational schema given below
to solve the given queries. [6]

o passenger ( pid, pname, pgender, pcity)


o agency ( aid, aname, acity)
o flight (fid, fdate, time, src, dest)
o booking (pid, aid, fid, fdate)

 Find only the flight time and ids for passenger with flights to Karachi before
06/11/2020.

σ dest=‘Karachi’ ∧ fdate < ‘06/11/2020’, passenger ⨝ booking ⨝ flight) (time, fid)

Explanation:

o The symbol ⨝ is used to perform a specific type of operation, called


natural join, between two or more sets of data.
o The symbol σ is used to filter out the data that meets a specific condition.
o In the flight relation, the selection conditions are that the destination
should be 'Karachi' and the flight date should be before '06/11/2020'.
o We perform a join operation between the passenger, booking, and flight
relations to obtain the required data.
o We want to retrieve two attributes, time and flight ID, from the resulting
relation.
o .

 Find the passenger names, gender for passengers who have bookings on at least
one flight.

π(pname, pgender)(passenger ⨝ booking)

Explanation:

o The symbol ⨝ is used to perform a specific type of operation, called


natural join, between two or more sets of data.
o The symbol π is used to retrieve only the required attributes from the
data.
o We perform a join operation between the passenger and booking
relations to obtain the required data.
o We want to retrieve two attributes, passenger name and gender, from the
resulting relation.

 Find the passenger names for those who do not have any bookings in any flights to
Lahore and Karachi.

π(pname)(passenger - π(pid, pname, pgender, pcity)(σ(dest = 'Lahore' ∨ dest =


'Karachi', booking ⨝ flight ⨝ passenger)))

Explanation:

o σ represents the selection condition to filter the required data.


o dest = 'Lahore' ∨ dest = 'Karachi' is the selection condition for the flight
relation to get the flights going to Lahore or Karachi.
o booking ⨝ flight ⨝ passenger is the join operation between the booking,
flight, and passenger relations to get the passengers who have bookings
in flights to Lahore or Karachi.
o π(pid, pname, pgender, pcity) is the projection operation to retrieve the
required attributes for the set of passengers who have bookings in flights
to Lahore or Karachi.
o passenger - π(pid, pname, pgender, pcity)(σ(dest = 'Lahore' ∨ dest =
'Karachi', booking ⨝ flight ⨝ passenger)) is the set difference operation
between the passenger relation and the set of passengers who have
bookings in flights to Lahore or Karachi.
o π(pname) is the projection operation to retrieve the names of the
passengers from the resulting relation.

 Get the details of flights that are scheduled on both dates 01/12/2020 and
02/12/2020 between 16:00 to 18:00 hours.

σ(fdate = '01/12/2020' ∧ time >= '16:00' ∧ time <= '18:00', flight) ⋂ σ(fdate =
'02/12/2020' ∧ time >= '16:00' ∧ time <= '18:00', flight)

Explanation:

o σ represents the selection condition to filter the required data.


o fdate = '01/12/2020' ∧ time > '16:00' ∧ time < '18:00' is the selection
condition for the flight relation to get the flights that occur on 01/12/2020
between 16:00 to 18:00 hours.
o fdate = '02/12/2020' ∧ time >= '16:00' ∧ time <= '18:00' is the selection
condition for the flight relation to get the flights that occur on 02/12/2020
between 16:00 to 18:00 hours.
o ⋂ represents the set intersection operation to get the flights that occur
on both days.
o flight is the relation on which these operations are performed.

 Find the details of all male passengers who are associated with more than one
agency and rename attribute aname to agency-name.

ρ(booking_passenger, π(pid, pname, pgender, pcity, aid, fdate, fid, agency-name)


(passenger ⨝ booking ⨝ agency))

γ(pid, agency-count)(booking_passenger)

σ(pgender = 'M' ∧ agency-count > 1, γ) {agency-name → agency_name}


(booking_passenger)

Explanation:
o ρ operator is used to rename the joined relation as "booking_passenger"
for ease of understanding.
o π operator is used to select the required attributes from the joined
relation passenger, booking, and agency and rename the attribute
"aname" as "agency-name".
o ⨝ is used to join the passenger, booking, and agency relations on the
common attributes pid and aid.
o γ is used to group the joined relation by the passenger id and count the
number of distinct agencies associated with each passenger.
o σ is used to select the male passengers who are associated with more
than one agency and rename the "aname" attribute to "agency-name".

 Count the number of agencies that are located in the same city as passenger with id
245 and display the output as total count, city name and passenger name and id.

ρ(passenger245, σ(pid = 245)(passenger))

ρ(agency_city, π(acity, COUNT(*) AS count_agency)(passenger245 ⨝ agency, acity))

π(count_agency, acity, pname, pid)(passenger245 ⨝ agency_city ⨝ passenger)

Explanation:

o ρ operator is used to create two relations for passenger with id 245 and
agency table named "passenger245" and "agency", respectively.
o π operator is used to project the "acity" attribute from the "agency"
relation and count the number of agencies in each city by using the
GROUP BY clause with the attribute "acity". The result is saved in the
relation "agency_city".
o ⨝ is used to join the "passenger245" and "agency_city" relations on the
common attribute "acity".
o π operator is used to project the required attributes including
"count_agency", "acity", "pname", and "pid" from the joined relation
"passenger245 ⨝ agency_city ⨝ passenger".
o The final result includes the count of agencies, city name, passenger
name, and passenger id in which the passenger with id 245 is located and
where at least one agency is also located.

You might also like