0% found this document useful (0 votes)
42 views17 pages

23000483-CS237 Assignment-1-1

The document contains a series of assignments related to Database Management Systems, including relational algebra queries, SQL DDL definitions, and discussions on referential integrity constraints. It covers various database schemas, queries for retrieving specific data, and explanations of database concepts such as candidate keys and referential integrity. The assignments also include examples and justifications for operations performed on the database relations.

Uploaded by

ujjwalkandpal9
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)
42 views17 pages

23000483-CS237 Assignment-1-1

The document contains a series of assignments related to Database Management Systems, including relational algebra queries, SQL DDL definitions, and discussions on referential integrity constraints. It covers various database schemas, queries for retrieving specific data, and explanations of database concepts such as candidate keys and referential integrity. The assignments also include examples and justifications for operations performed on the database relations.

Uploaded by

ujjwalkandpal9
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/ 17

Aryan 23000483

ASSIGNMENT-1

DATABASE MANAGEMENT SYSTEM(CS237)


NAME: Aryan Singh
ENROLLMENT NO: 23000483
B-TECH CSE SY

1. Consider the relational database of Figure 3.39, where the primary keys are underlined.
employee (person-name, street, city)
works (person-name, company-name, salary)
company (company-name, city)
manages (person-name, manager-
name)

Figure 3.39

Give an expression in the relational algebra to express each of the following queries:

a.Find the names of all employees who work for First Bank Corporation.

Answer: π person-name ( σ company-name = ‘First Bank Corporation’ (works))

b.Find the names and cities of residence of all employees who work for First Bank
Corporation.

Answer: π person-name, city (σ company-name = ‘First Bank Corporation’ (works ⨝ )


employee

c.Find the names, street address, and cities of residence of all employees who work for
Aryan 23000483

First Bank Corporation and earn more than $10,000 per annum.

>10000 (works ⨝ employee))


Answer: π person-name, street, city (σ company-name = ‘First Bank Corporation’ ^ salary

d.Find the names of all employees in this database who live in the same city as the
company for which they work.

Answer: π person-name (σ employee-city = company-city (employee ⋈ works ⋈


company))

e.Find the names of all employees in this database who do not work for First Bank
Corporation.

Answer: π person-name (σ company-name ≠ ‘First Bank Corporation’(works))

f.Find the names of all employees who live in the same city and on the same street as do
their managers.

Answer: π (e1.street = e2.street ^ e1.city = e2.city) (employeee1 ⋈ manages ⋈


employeee2))

g.Find the names of all employees who earn more than every employee of Small Bank
Corporation.

Answer: π person-name (σ salary > all (π salary (σ company-name = ‘Small Bank


Corporation’ (works)))(works))

h. Assume the companies may be located in several cities. Find all companies located in
every city in which Small Bank Corporation is located.

Answer: π company-name(company) π company-name (σ city not in (π city (complany)


where company-name = ‘Small Bank Corporation’))
Aryan 23000483

i. Find the company that has the most employees.

Answer: π company-name (σ count(person-name) = max(count(person-name))(works))

2. Give an SQL DDL definition for relational database of Figure 3.39. Define appropriate
primary key and foreign key for each relation schema and include them in DDL
Definition.

Answer:

Create table employee (

person_name varchar(255) primary key,

street varchar (255),

city varchar(255)

);

Create table company (

company_name varchar (255) primary key,

city varchar(255)

);

Create table works(

person_name varchar (255),

company_name varchar(255),

salary decimal (10 ,2) ,

primary key (person_name, company_name),

foreign key (person_name) references employee (person_name) on cascade,

foreign key (company_name) references company(company_name) on delete cascade

);

Create table manages(

person_name varchar(255),

manager_name varchar(255),

primary key(person_name, manager_name),

foreign key(person_name) references employee (person_name) on delete cascade,


Aryan 23000483

foreign key (manager_name) references employee (person_name) on delete cascade

);

3. Consider Referential-integrity constraints as discussed in class involve exactly two


relations.

Consider the relational database that includes the following relations:


employee (eid, name, street, city)

salaried_worker (eid, office, phone, salary)


hourly_worker (eid, hourly-wage)

Suppose that we wish to require that every eid (represents employee id) that appears in
either salaried_worker or hourly_worker, must refer some tuple in employee.

Give a DDL Definition in SQL for expressing such referential integrity constraints.

Discuss the actions that the system must take to enforce a constraint of this form.

Answer:

DDL definition in SQL:

Alter table salaried_worker

Add constraint fk_employee

Foreign key (eid) references employee (eid) on delete cascade;

Alter table hourly_worker

Add constraint fk_employee

Foreign key (eid) references employee(eid) on delete cascade;

Actions taken by system on enforce constraint

Insertion: before inserting record on salary_worker or hourly_worker, system checks if eid


exist in employee. If not insertion is rejected.

Deletion: if employee record is delete, corresponding records in salaried_worker and


hourly_worker are also delete due to on delete cascade

Update: if eid in employee is modified, it must be updated in dependent tables, or update is


Aryan 23000483

rejected unless cascading updates are enables;

4. Refer above relations and answer:

If eid is a key for the employee relation, could it be a key for the salaried_worker relation
and hourly_worker relation? Justify your answer.

Answer:

if eid is key for employee relation, it can be key for both salaries_worker and
hourly_worker relations, provided that each worker type in uniquely by eid. This is
because eid must uniquely each employee in all relations.

5. Consider the following schema:


Suppliers (s#, sname, status, city) à primary key: s#

Parts (p#, pname, color, weight, city) à primary key: p#

Shipment (s#, p#, qty) à primary key: s#, p#, foreign key: s# references suppliers(s#), p#
reference Parts(p#)

Identify primary keys and foreign keys in above relations.

Write the following queries in relational algebra.

1. Find the s# of suppliers who supply red part.

Answer: π s# (σ color = ‘red’ (parts ⋈ shipment))

Find the names of suppliers who supply red part.


Answer: π sname( σ color = ‘red’ (parts ⋈ shipment ⋈ suppliers))

Find the names of suppliers who supply part P2.

Answer: π sname( σ p# = ‘p2’ (shipment ⋈ suppliers))

Find the s# of suppliers who supply red or green part.


Answer: π s# (σ color = ‘red’ or color = ‘green’ (parts ⋈ shipment))
Aryan 23000483

Find the s# of suppliers who supply red part or are located in london.

Answer: π s# (σ color= ‘red’ V city = ‘london’ (part ⋈ shipment))

Find the s# of suppliers who supply red part and green part.

Answer: π s# (σ color = ‘red’ ^ color = ‘green’ (part ⋈ shipment))

Find the s# of suppliers who supply all parts.

Answer: π s# (shipment) ÷ π p# (parts)

Find the names of suppliers who supply all parts.

Answer: π sname (suppliers) π sname(σ p# ∉ p(suppliers ⋈ shipment * π p# (parts))

Find the s# who supply at least all those parts supplied by supplier s2.

Answer: π s# (shipment) ÷ π p# (π s#= ‘s2’ (shipment))

Find all suppliers who are located in the same city

ρ.Suppliers𝜌.Suppliers
Answer: π suppliers.sname(π suppliers. City = s.city (suppliers

Find the names of suppliers who do not supply part P2.

Answer: π sname( σ p# ≠ ‘p2’ (shipment ⋈ suppliers))

Find the s# of suppliers who supply all red part.

Answer: π s# (σ color = ‘red’ (parts ⋈ shipment))

Find the names of all red parts.

Answer: π pname )σ color = ‘red’ (parts))

6. Consider the relational Database given in previous example Suppliers, parts and
shipments. Here, s# is a key to the relation suppliers, p# is a key to the relation parts. The
foreign-key clause requires that every s# in shipment refers s# in suppliers and p# in
Aryan 23000483

shipment refers p# in parts. Explain exactly what happens when a tuple in the relation
supplier or parts is deleted. Justify your answer.

Answer:
Effect of Deleting a Supplier or Part:

In our database, the Suppliers (s#), Parts (p#), and Shipments (s#, p#, qty) tables are
connected through foreign keys.

When a Supplier (s#) is deleted:

CASCADE – All related shipments are also deleted.

SET NULL – Shipments remain, but s# is set to NULL.

RESTRICT/NO ACTION – Prevents deletion if shipments exist.

When a Part (p#) is deleted:

CASCADE – Deletes all shipments containing that part.

SET NULL – Keeps shipments but sets p# to NULL.

RESTRICT/NO ACTION – Prevents deletion if shipments exist.

7. Consider the database of a sailing club with the following three tables:
Sailor(sid: integer, name: string, rating: integer, age: integer)

Boat(bid: integer, name: string, colour: string)


Reservation(sid: integer, bid: integer, day: date)

Identify primary keys in above relations. Write the following queries in relational algebra.

Find names of sailors who’ve reserved boat #103

Answer: π name(salier ⋈ σ bid = 103( reservation))

Find sailors who’ve reserved a red or a green boat

Answer: π sid.name (sailor ⋈ π color = ‘red’ V color = ‘green’ (boat) ⋈ reservation)


Aryan 23000483

Find the names of sailors who have reserved all boats

Answer: π name(sailor ⋈ π sid (reservation) – π bid (boat))

Find the names of sailors who have reserved all red boats

Answer: π name (sailor ⋈ (π id (reservation) – π bid (σ color = ‘red’ (boat))))

Find the name and age of all sailors with a rating above 7

Answer: π name, age (σ rating > 7 (sailor)

Find the names of sailors with a rating greater 5 and an age > 30.

Answer: π name, age (π rating > 7 ^ age > 0 (sailor))

Find the rating and the age of the sailors who reserved a red boat.

Answer: π rating, age (sailor ⋈ (σ color = ‘red’ (boat)) ⋈ reservation)

Find the IDs of those sailors who reserved the boat ‘Clipper’ and who reserved a green
boat.

Answer: π sid (reservation ⋈ π bid (σ name=’Clipper’(boat))) n π sid (reservation ⋈ π bid


(σ color=’green’ (boat))

Find the IDs of those sailors who has not reserved a red boat.

Answer: π sid(sailor) – 227 sid(reservation ⋈ σ color=’red’ (boat))

List the IDs of those sailors who has reserved all boats

Answer: π sid (reservation) ÷ π bid (boat)

List the ids of those sailors who has reserved all red boats.

Answer: π sid(reservation) – π bid (σ color = ‘red’ (boat))

8. Consider the following relational schema for a library:

member (memb_no, name, dob)


books(isbn, title, authors, publisher)
Aryan 23000483

borrowed (memb_no, isbn, date)

Write the following queries in relational algebra.

Find the names of members who have borrowed any book published by “McGraw-Hill”.

Answer: π name(σ publisher=’McGraw-hill’ (books) ⋈ borrower ⋈ member)

Find the name of members who have borrowed all books published by “McGraw-Hill”.

(books))) ⋈ member)
Answer: π name((π memb_no, isbn(borrower) ÷ σ isbn(σ publisher=’McGraw-Hill’

Find the name and membership number of members who have borrowed more than five
different books published by “McGraw-Hill”.

‘McGraw-Hill’ (books) ⋈ borrowerd ⋈ member)


Answer: π memb_no, name(σ cnt> 5(γ memb_no, cont(isbn) à cnt(π publisher =

For each publisher, find the name and membership number of members who have
borrowed more than five books of that publisher.

cnt(books ⋈ borrowed)) ⋈ member)


Answer: π publisher, memb_no, name(π cnt> γ publisher, memb_no,count(isbn)à

Find the average number of books borrowed per member. Take into account that if a
member does not borrow any books, then that member does not appear in the borrowed
relation at all.

Answer: γ avg(cnt)(π memb_no(member) ⋈ γ memb_no, count(isbn) à cnt(borrowed))

Find the names of members who borrowed a book titled "Database Systems"

Answer: π name(σ title=’Database System’(books) ⋈ borrowerd ⋈ member)

Find the members who have borrowed the book "Data Structures"

Answer: π memb_no, name(σ title=’Data structures’ (books) ⋈ borrowerd ⋈ member)

Find the books borrowed by Smith


Aryan 23000483

Answer: π isbn,title(σ name=’Smith’ (member) ⋈ borrowed ⋈ books)

9. The constraint that values of a given foreign key must match values of the
corresponding primary key is known as a .

Answer: referential integrity constraint.

10 Referential Integrity Rule:


.

Answer: for every value of foreign key in table, there must exist matching value in
corresponding primary key of referenced table, or foreign key value must be null.

11 Define Candidate Key, Primary Key, Alternate Key


.
Candidate key: it is key is column in table that can uniquely identify every row, with no
extra columns needed.
Primary key: it is one candidate key chosen to be main way to identify rows in table.
Alternate key: it is ay candidate key that wasn’t chosen as primary key. It could be unique
identify rows, but it’s not main one used.

12 A is a collection of conceptual tools for describing data, data relationships,


. data semantics, and consistency constraints.

Answer: data model

13 True/ False: The relational algebra is a non-procedural query language


.
True

14 Which of the following operators are unary operators? (Select multiple correct answers if
. exists)

Select (σ)

Project (∏)

Rename (ρ)
Cross Product (X)

I & III

I & II

I, II & III

I , II, III & IV


Aryan 23000483

Answer: c. I, II & III


15 Which of the following operations require that the relations r and s be of the same arity,
. and that the domains of the ith attribute of r and the ith attribute of s be the same.

RUS

R–S

R∩S

All of the above

Answer: d. all of the above


16 r ∩ s = r – (r – s) Justify your answer with example.
.
Answer: in this relational algebra,

Left side: intersection gives tuples present in both r and s.


Right side: r-s find tuples in r but not in s and r-(r-s) then removes those from r, leaving
only tiples in r that are also in s.

For example let r={1,2,3} and r is student IDs in math class


Let s={2,3,4} and s is student ID in science class
R intersection s = {2,3,} à. ID in both class
r-s = {1} à IDs in r not in s.
r-(r-s) = {1,2,3}-{{1} = {2,3}
both side queal {2,3}, so r interception s = r – (r-s) is true.

17. Consider two relations r (R) and s(S). The natural join of r and s, denoted by r ⋈ s, can be
defined as:

r ⋈ s = ∏R ∪ S (σ r.A1 = s.A1 ^ r.A2 = s.A1 ^ .. .. ^ r.An = s.An(r x s))

where R ∩ S = {A1, A2, . . . , An}.

Please note that if r (R) and s(S) are relations without any attributes in common, that is, R

∩ S = ∅, then r ⋈ s = r ⋈ s = r × s

18. Answer the following:


Union Operation in relational algebra can be defined as R U S = {t | t ∈ r or t ∈
Aryan 23000483

s} Set Difference Operation in relational algebra can be defined as R – S = {t | t ∈


r and t∉s}
Cross Product Operation in relational algebra can be defined as R X S = {t | t = (t_r,t_s)
where t_r ∈ R and t_s ∈ S}

19. Consider two relations:


R = {A, B, C, D}, S = {E, B, D}

If R ⋈ S is applied, then

Resultant schema contains which all attributes? {A,B,C,D,E}

R ⋈ S = π A,B,C,D,E (σ R.B = S.B ^ R.D = S.D (R X S))

20. Consider the Banking enterprise example discussed in class. Answer the following:
Find the names of all customer who have both a loan and an account at the bank.
Which of following query generates the correct answer:
πcustomer-name (borrower) intersection πcustomer-name (depositor)

customer-name (borrower) – (πcustomer-name (borrower) - πcustomer-name (depositor))

borrower.customer_name (σborrower.customer_name = depositor.customer_name (borrower x depositor))

customer-name(borrower ⋈ depositor))

I & IV only

I, III & IV only

I & III only

I, II, III & IV

Answer: d. I,II,III & IV

21. Consider the Banking enterprise example discussed in class. Consider the following
relational algebra query.

What is the meaning of each of the following expression?


r1 = πbranch-name (σbranch-city = “Brooklyn” (branch))
r2 = πcustomer-name, branch-name (depositor

account) r2 ÷ r1
Aryan 23000483

Answer:

r1: it is set of branch names located in Brooklyn.

r2: it is table listing customer names and the branch names of their accounts.

r3: it gives customer names of those who have accounts at all Brooklyn branches.

Explain in words what the expression does.


Answer: The full expression (r2 ÷ r1) finds customers who have accounts at every
branch in Brooklyn. It works by first identifying all Brooklyn branches (r1), then listing
customers and their account branches (r2), and finally finding customers whose account
branches include all those in Brooklyn.

22. SELECT Operation in SQL is equivalent to


the σ operation in relational algebra

the σ operation in relational algebra but removes duplicate tuple

the π operation in relational algebra


the π operation in relational algebra, except that SELECT in SQL retains duplicate

Answer: d. π operation in relational algebra, except select in sql retains duplicate.

23. List four significant problems of a file-processing system that prompted the development
of a DBMS.

Answer:

Data redundancy and inconsistency

Difficulty in data access

Data isolation

Lack of security and integrity constraints

24. Differentiate physical data independence and logical data independence.


Answer:

Physical Data Independence Logical Data Independence


Changes in physical storage, indexing, file Changes in tables, attributes,
organizations, etc. relationship, constraints, etc.
No effect on application since logical Application may need modification of
structure remains unchanged they depend on changed structures
Concerned with internal schema Concerned with conceptual schema
25. What are five main functions of a database administrator?
Aryan 23000483

Answer:

Database design and implementation

Performance monitoring and optimization

Security and access control

Backup and recovery management

Database maintenance and upgrade

26. Describe the differences in meaning between the terms relation and relation schema.
Answer:

Relation Relation Schema


Set of tuples representing real data Metadata description of relation
Consist of tuples and attributes Consist of attributes names, data types,
and integrity constraints.
Changes dynamically as data is inserted Static unless database structure is
updated or deleted modified
27. List two reasons why null values might be introduced into the database.
Answer:

Value is unknown at time of data entry for example new employee’s phone number is not
provided yet.

Attributes does not apply to particular record for example middle name for person who
does not have one

28. True / False In Relational Model, for all relations r, the domains of all attributes of r be
atomic. What does it mean? Explain your answer with example.

Answer:

True

In relational model, each attributes should atomic values, meaning no multiple values or
nested in single field.

29. Match the following:


1) Candidate Key a) References primary key values in other relations
2) Primary Key b) The database must not contain any unmatched
foreign key values
3) Foreign Key c) A candidate key that is chosen by the database
designer as the principal means of identifying

tuples within a relation.


4) Referential Integrity d) minimal super keys (Uniqueness Property,
Aryan 23000483

Irreducibility Property)

1-a, 2-b, 3-c, 4-d

1-c, 2-a, 3-b, 4-d

1-d, 2-c, 3-a, 4-b

1-b, 2-d, 3-c, 4-a

Answer: c à 1-d, 2-c, 3-a, 4-b

30. Which of the following statements are true in case of relation?


There are no duplicate tuples in a relation; Tuples are unordered

Attributes are unordered; All attribute values are atomic.

a & b both

There are duplicate tuples in a relation

None of the above

Answer: c à a & both

31. Given the two relations R(A,B,C) and S(B,C,D). Answer which all attributes and tuples
will be included in the output considering each of the following cases. Support your
answer with example.

R natural inner join S

Answer:

Natural Inner Join (R ⋈ S)

Joins on common attributes (B, C).

Keeps only matching rows.

Output:

A B C D

1 x y p

2 z w q

R inner join S on R.B = R.S


Aryan 23000483

Answer:

inner join on R.B = R.SR.B = R.SR.B = R.S

Likely typo (should be R.B = S.B)

Equivalent to natural inner join in this case

Output: same as natural join in (a)

R inner join S using(c)

Answer:

Inner Join Using (C)

Joins only on attribute C.

A B C B D

1 x y x p

2 z w z q

R left outer join S

Answer:

Left Outer Join (R ⟕ S)

Keeps all rows from R.

If there is no match, NULL is used.

A B C D

1 x y p

2 z w q

R right outer join S

Answer:

Right Outer Join (R ⟖ S)

Keeps all rows from S.

If there is no match, NULL is used.

A B C D
Aryan 23000483

1 x y p

2 z w q

R full outer join S

Answer:

Full Outer Join (R ⟗ S)

Keeps all rows from both relations.

If no match, fills with NULL.

A B C D

1 x y p

2 z w q

32. Given relations R(A, B) and S(A, C), the natural join R ⋈ S is equal to R ∩ S.
True B. False

Answer: False

33. Given relations R(A, B, C) and S(D, E), the natural join R ⋈ S is equal to R × S.
True B. False

Answer: True

34. It is possible to express the relational algebra operator intersection ( ∩ ) using the
difference operator ( − ).

True B. False
Answer: True

You might also like