Description
Part 1
- Using the extended Zoo database (ZooDatabase_extended.sql), write the following queries in SQL:
- Find all the rare animals and sort the query output by feeding time (from small to large)
- Find the animal names and categories for animals related to a bear (hint: use the LIKE operator)
- Find the names of the animals that are related to the tiger and are not common
- Find the names of the animals that are not related to the tiger
- List the animals (animal names) and the ID of the zoo keeper assigned to them.
- Now repeat the previous query and make sure that the animals without an assigned handler also appear in the answer.
- Report, for every zoo keeper name, the average number of hours they spend feeding all animals in their care.
- Report every handling assignment (as a list of assignment date, zoo keeper name and animal name). Sort the result of the query by the assignment date in an ascending order.
- Repeat the following queries using python (i.e., by reading data from animal.txt, without using a database)
- Find the names of the animals that are related to the tiger and are not common
- Find the names of the animals that are not related to the tiger
Part 2
- You are given a following schema in 1NF:
(First, Last, Address, Job, Salary, Assistant) and the following set of functional dependencies:
First, Last à Address
Job à Salary, Assistant
Decompose the schema to make sure it is in Third Normal Form (3NF).
- Write the necessary SQL DDL statements (CREATE TABLE) to define these the tables you created
- Write a python script that is going to create your tables and populate them with data automatically from data_module4_part2.txt (file attached). You do not have to use executemany, your python code can load data row-by-row. Make sure that you are inserting a proper NULL into the database. HINT: You can use INSERT OR IGNORE statement (instead of a regular INSERT statement) in SQLite to skip over duplicate primary key inserts without throwing an error.
For example:
cursor.execute(“INSERT OR IGNORE INTO Animal VALUES(?,?,?,?)”, [11, ‘Llama’, None, 3.5]);
would automatically ignore the insert if animal with ID 11 already exists in the database and insert a NULL into the third column. If you use ‘NULL’ value instead, animal category would be set to the 4-character string ‘NULL’
- Verify that your NULLS are loaded correctly, by finding all jobs with no salary specified using Salary IS NULL