0% found this document useful (0 votes)
5 views5 pages

DB - Midterm Solution

The document contains a series of questions and answers related to relational databases, including concepts such as foreign keys, entity sets, SQL queries, and views. It discusses the relationships between tables, how to model data, and provides SQL examples for various scenarios. Additionally, it outlines an E-R diagram scenario involving a pharmaceutical company, detailing the relationships between drugs, pharmacies, doctors, and patients.

Uploaded by

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

DB - Midterm Solution

The document contains a series of questions and answers related to relational databases, including concepts such as foreign keys, entity sets, SQL queries, and views. It discusses the relationships between tables, how to model data, and provides SQL examples for various scenarios. Additionally, it outlines an E-R diagram scenario involving a pharmaceutical company, detailing the relationships between drugs, pharmacies, doctors, and patients.

Uploaded by

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

Question 1:

1. Can a table have multiple foreign keys referencing the same primary key from another
table? Explain with an example. [2 marks]
Answer:
Yes, a table can have multiple foreign keys referencing the same primary key from
another table. Consider a scenario where you have a managers table and
a ProductGroups table. In the ProductGroups table, you might have fields for both
the ProductManager and the DivisionManager, both of which are foreign keys
referencing the ManagerID in the Managers table.

2. What is the relationship between domains and data types in a relational database? [2
marks]
Answer:
In a relational database, domains and data types are closely related concepts. Each
domain must have an assigned data type. This means that the domain's set of
acceptable values is constrained by the data type. For example, a domain for
"Employee Age" might be defined as an integer between 18 and 65, which
corresponds to an INTEGER data type.

3. What happens if you try to insert a value into a foreign key column that does not exist in
the referenced table? [2 marks]
Answer:
If you insert a value into a foreign key column that doesn't exist in the referenced
table, the database will return a foreign key constraint violation error, and the
insertion will fail to maintain referential integrity.

4. Explain the difference between a weak and a strong entity set. [2 marks]
Answer:
A strong entity set has its own primary key and can exist independently. It can stand
alone in the database.
A weak entity set lacks a primary key and depends on a strong entity for
identification, using a foreign key from the strong entity for uniqueness. It cannot
exist without the corresponding strong entity

5. Describe how you would model a specialization relationship in an ER diagram. Provide


an example involving Vehicle and its subclasses Car and Motorcycle. [3 marks]
Answer:
In an ER diagram, a specialization relationship is modeled using an inheritance-like
structure where a superclass (e.g., Vehicle) is connected to its subclasses (e.g., Car
and Motorcycle) with a line and a triangle symbol.
Example:
The Vehicle entity represents the superclass with common attributes like
Vehicle_ID and Model.
Car and Motorcycle are subclasses, each with specific attributes like Car_Type for
Car and Engine_Size for Motorcycle.
This structure shows that Car and Motorcycle inherit the attributes of Vehicle but
also have their own unique attributes.

Question 2:
Suppose we have a relational database with three tables

table Key attribute

S(sid, A) sid

T(tid, B) tid

R(sid,tid,C) sid,tid,C

The following referential integrity constraints hold on the table R. Every sid-value in R is
the key of a record in table S and tid-value in R is the key of a record in table T.
1. What does it mean that the attribute C is included in R’s key? [2 marks]
Answer:
Including the attribute C in the key of table R means that the combination of (sid, tid,
C) uniquely identifies each record in table R. This indicates that each pair of (sid, tid)
can have multiple entries in R differentiated by different values of C.

2. Write an SQL query to return records of the form sid where sid is the key of an S record
that is not R-related to any records in table T. [4 marks]
Answer:
SELECT sid
FROM S
WHERE sid NOT IN (SELECT sid FROM R);
Or
SELECT S.sid
FROM S
LEFT OUTER JOIN R
ON S.sid = R.sid
WHERE R.sid IS NULL;

Question 3:
Consider a photo sharing Website, where users can post pictures, as well as comment and
rate other user’s pictures. The schema is:
Users(uid, name)
Comment(uid, pid, score, txt)
Picture(pid, uid, img)
1. Write a SQL query that returns all users who have given a score of 8 or higher to 50
pictures or more. For each user, your query should return the user ID and the name. [5
marks]
2. A picture is considered highly rated if it received at least one score of 10, from a user
other than its author. A cautious user is a user who comments only on highly rated
pictures. (A user who did not comment at all is also cautious.) Write a SQL query that
finds all cautious users. Your query should return a list of uid, name pairs. [8 marks]
3. Write a query to find the distinct IDs of users who have uploaded pictures that received
more than 10 comments with a score greater than 8. [5 marks]
Answers:
1. SELECT u.uid, u.name FROM Users u JOIN Comment c ON u.uid = c.uid
WHERE c.score >= 8 GROUP BY u.uid, u.name HAVING COUNT(DISTINCT
c.pid) >= 50;

2. SELECT u.uid, u.name FROM Users u LEFT JOIN Comment c ON u.uid = c.uid
WHERE c.pid IN ( SELECT pid FROM Comment WHERE score = 10 AND
uid != Picture.uid JOIN Picture ON Comment.pid = Picture.pid )
GROUP BY u.uid, u.name HAVING COUNT(c.pid) = 0 OR COUNT(DISTINCT
c.pid) = COUNT(DISTINCT c.pid) WHERE c.pid IN ( SELECT pid
FROM Comment WHERE score = 10 );

3. SELECT DISTINCT p.uid FROM Picture p JOIN Comment c ON p.pid = c.pid


WHERE c.score > 8 GROUP BY p.uid HAVING COUNT(c.pid) > 10;

Question 4:
Consider a view v whose definition references only relation r.
i. If a user is granted select authorization on v, does that user need to have select
authorization on r as well? Why or why not? [2 marks]
ii. If a user is granted update authorization on v, does that user need to have update
authorization on r as well? Why or why not? [2 marks]
iii. Give an example of an insert operation on a view v to add a tuple t that is not visible in
the result of select * from v. Explain your answer. [2 marks]

Answer:
1. No, a user does not need to have select authorization on the underlying relation r if they
are granted select authorization on the view v. The view acts as an intermediary, and the
database system ensures that the user can access the data through the view without
needing explicit permissions on the underlying tables or relations.
2. Yes, a user typically needs to have update authorization on the underlying relation r if
they are granted update authorization on the view v. This is because updating the view
involves manipulating the data in the underlying relation.
3. To insert a tuple t into a view v that is not visible in the result of SELECT * FROM v,
you need to ensure that the view is updatable and that the insert operation complies with
the view's definition.
-- Create a table
CREATE TABLE r (id INT, name VARCHAR(50), age INT);

-- Create a view that filters out people under 18


CREATE VIEW v AS SELECT * FROM r WHERE age >= 18;

-- Insert a tuple that is not visible in the view (age < 18)
INSERT INTO v (id, name, age) VALUES (1, 'John Doe', 17);
Question 5:
Suppose that we have a relation marks (ID, score) and we wish to assign grades to students
based on the score as follows: grade F if score < 40, grade C if 40 ≤ score < 60, grade B if 60
≤ score < 80, and grade A if 80 ≤ score. Write SQL queries to do the following: [4 marks]
i. Display the grade for each student, based on the marks relation.
ii. Find the number of students with each grade.

Answer:
1. SELECT ID, score,
CASE
WHEN score < 40 THEN 'F'
WHEN score >= 40 AND score < 60 THEN 'C'
WHEN score >= 60 AND score < 80 THEN 'B'
WHEN score >= 80 THEN 'A'
END AS grade
FROM marks;
2. SELECT
CASE
WHEN score < 40 THEN 'F'
WHEN score >= 40 AND score < 60 THEN 'C'
WHEN score >= 60 AND score < 80 THEN 'B'
WHEN score >= 80 THEN 'A'
END AS grade,
COUNT(*) AS number_of_students
FROM marks
GROUP BY
CASE
WHEN score < 40 THEN 'F'
WHEN score >= 40 AND score < 60 THEN 'C'
WHEN score >= 60 AND score < 80 THEN 'B'
WHEN score >= 80 THEN 'A'
END;

Question 6:

A pharmaceutical company manufactures one or more drugs, and each drug is manufactured
and marketed by exactly one pharmaceutical company. Drugs are sold in pharmacies. Each
pharmacy has a unique identification. Every pharmacy sells one or more drugs, but some
pharmacies do not sell every drug. Drug sales must be recorded by prescription, which are
kept as a record by the pharmacy. A prescription clearly identifies the drug, physician, and
patient, as well as the date it is filled. Doctors prescribe drugs for patients. A doctor can
prescribe one or more drugs for a patient and a patient can get one or more prescriptions, but
a prescription is written by only one doctor. Pharmaceutical companies may have long-term
contracts with pharmacies and a pharmacy can contract with zero, one, or more pharmaceutical
companies. Each contract is uniquely identified by a contract number. Draw the appropriate
entity-relationship (E-R) diagram. Clearly show all cardinality constraints, cardinality limits,
and existence dependencies. [10 marks]
Answer:

You might also like