This assignment has two parts.
Download A4.zip. Answer the questions in A4.ipynb.
You will design a database for SFU. This database will include information about departments, students, courses (and their offerings):
Please note that the following two sentences are not constraints (you don't need to enforce them in your ER-diagram). They just tell you what data might be like.
Assume that for a course to be offered during a term, it has at least one student enrolled
Assume that a student can take courses “owned” by departments with which the student is not affiliated
Render the SFU database in the version of the E/R model that we studied in class, with exactly the constraints and requirements specified above.
Please follow the above E/R Diagram and write SQL queries to create required tables in sfu.db
%load_ext sql
%sql sqlite:///sfu.db
u'Connected: @sfu.db'
#REPLACE WITH YOUR CODE
Consider a relational schema and a set of functional dependencies:
Decompose $R(A,B,C,D,E)$ into BCNF. Show all of your work and explain, at each step, which dependency violations you are correcting. You have to write down a description of your decomposition steps. (2 points)
A set of attributes $X$ is called closed (with respect to a given set of functional dependencies) if $X^+=X$. Consider a relation with schema $R(A,B,C,D)$ and an unknown set of functional dependencies. For each closed attribute set below, give a set of functional dependencies that is consistent with it.
a. All sets of attributes are closed (1 point)
b. The only closed sets are $\{\}$ and $\{A,B,C,D\}$ (1 point)
c. The only closed sets are $\{\}$, $\{A,B\}$, and $\{A,B,C,D\}$ (1 point)
Suppose Mike is the owner of a small store. He uses the following database (mike.db) to store monthly sales of his store.
Sales
(name, discount, mouth, price)%load_ext sql
%sql sqlite:///mike.db
The sql extension is already loaded. To reload it, use: %reload_ext sql
u'Connected: @mike.db'
%sql select * from Sales limit 5
* sqlite:///mike.db sqlite:///sfu.db Done.
name | discount | month | price |
---|---|---|---|
bar1 | 0.15 | apr | 19 |
bar8 | 0.15 | apr | 19 |
gizmo3 | 0.15 | apr | 19 |
gizmo7 | 0.15 | apr | 19 |
mouse1 | 0.15 | apr | 19 |
However, Mike finds that the database is difficult to update (i.e., when inserting new data into the database). Your job is to help Mike to normalize his database. You should do the following steps(a-d):
a. Find all nontrivial functional dependencies in the database. This is a reverse engineering task, so expect to proceed in a trial and error fashion. Search first for the simple dependencies, say $name \rightarrow discount$ then try the more complex ones, like $name, discount \rightarrow month$, as needed. To check each functional dependency you have to write a SQL query.
Your challenge is to write this SQL query for every candidate functional dependency that you check, such that:
the query's answer is always short (say: no more than ten lines - remember that 0 results can be instructive as well)
you can determine whether the FD holds or not by looking at the query's answer. Try to be clever in order not to check too many dependencies, but don't miss potential relevant dependencies. For example, if you have A → B and C → D, you do not need to derive AC → BD as well.
Write down all FDs that you found. (1 point)
For each FD above, write down the SQL query that discovered it (remember short queries are preferred) (1 point)
# REPLACE WITH YOUR CODE
b. Decompose the Sales
table into BCNF. Like Task 1, show a description of your decomposition steps. (1 point)
c. Write down SQL queries to create the BCNF tables in the mike.db. Create keys and foreign keys where appropriate. (1 point)
# REPLACE WITH YOUR CODE
d. Populate the BCNF tables using the data from the sales table. (1 point)
Hint: see SQL INSERT INTO SELECT Statement
# REPLACE WITH YOUR CODE
Download A4.zip. Answer the questions in A4.ipynb. Put A4.ipynb
, ER-diagram.png
, sfu.db
, and the mike.db
(with populated BCNF tables) into A4-submission.zip.
Submit A4-submission.zip to the CourSys activity Assignment 4.