Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
6 views

Exercises InterfacePythonWithMySQL

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Exercises InterfacePythonWithMySQL

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Interface Python with MySQL - Exercises

We consider a dataset that includes data from a research by Stanley Coren, a professor of canine
psychology at the University of British Columbia (Canada), as well as breed size data for dogs from
the American Kennel Club (AKC).
The dataset consists of two tables, stored as CSV (comma-separated values) files:
dog_intelligence.csv and breed_info.csv.
The first line of each file contains the headers of the columns (separated by commas). The other
lines are the rows of the table, which contain the values of the corresponding fields.

dog_intelligence.csv
The first four lines of the file and the corresponding table:
breed,classification,obey,reps_lower,reps_upper
Affenpinscher,Above Average Working Dogs,0.7,16,25
Afghan Hound,Lowest Degree of Working/Obedience Intelligence,0.2,81,100
Airedale Terrier,Above Average Working Dogs,0.7,16,25

breed classification obey reps_lower reps_upper


Affenpinscher Above Average Working Dogs 0.7 16 25
Afghan Hound Lowest Degree of Working/Obedience Intelligence 0.2 81 100
Airedale Terrier Above Average Working Dogs 0.7 16 25
Description of the attributes:
• breed: dog breed
• classification: intelligence of dogs of that breed
• obey: probability that the breed obeys the first command
• reps_lower: lower limit of repetitions to understand new commands
• reps_upper: upper limit of repetitions to understand new commands

breed_info.csv
The first four lines of the file and the corresponding table:
breed,height_low_inches,height_high_inches,weight_low_lbs,weight_high_lbs
Affenpinscher,9,12,8,12
Afghan Hound,25,27,50,60
Airedale Terrier,22,24,45,45

breed height_low_inches height_high_inches weight_low_lbs weight_high_lbs


Affenpinscher 9 12 8 12
Afghan Hound 25 27 50 60
Airedale Terrier 22 24 45 45
Description of the attributes:
• breed: dog breed
• height_low_inches: lower limit of height (in inches)
• height_high_inches: upper limit of height (in inches)
• weight_low_lbs: lower limit of weight (in lbs)
• weight_high_lbs: upper limit of weight (in lbs)
Exercise 1
Use MySQL Connector Python to do the following.
1. Create a MySQL database called “dogs”.
2. Create a table breed_info and fill it. To do this, read the file breed_info.csv. Recall that the
first line contains the headers of the columns, while the other lines are the rows of the
table. Once you have read the first line, you can create the table, where breed is the
primary key. You can fill the table by reading the other lines.
3. Create a table dog_intelligence and fill it. To do this, read the file dog_intelligence.csv.
Recall that the first line contains the headers of the columns, while the other lines are the
rows of the table. Once you have read the first line, you can create the table, where breed
is the primary key. You can fill the table by reading the other lines. Finally, add a referential
integrity constraint between breed and the primary key of breed_info.
4. Add a column called height_avg_inches in the breed_info table that represents the average
height of a breed and whose value is the mean between the values height_low_inches and
height_high_inches.
5. Add a column called weight_avg_lbs in the breed_info table that represents the average
weight of a breed and whose value is the mean between the values weight_low_lbs and
weight_high_lbs.

Exercise 2
We want to understand whether there is a correlation between dog size and intelligence. To do
this, perform the following queries in the database “dogs” (use MySQL Connector Python):
1. Report all the breeds that are classified as “Brightest Dogs”, their average height and
weight.
2. Report all the breeds that are classified as “Lowest Degree of Working/Obedience
Intelligence”, their average height and weight.
3. Report all the breeds whose obey field is at least 0.7, their average height and weight.
4. Report all the breeds whose obey field is at most 0.3, their average height and weight.
5. For each classification, report: the maximum and the minimum height; the maximum and
the minimum weight.
Assume the following:
• a breed is small if weight_avg_inches < 23;
• a breed is medium if 22 < weight_avg_inches < 56;
• a breed is large if weight_avg_inches > 55.
6. Report the number of small breeds whose obey field is at least 0.7.
7. Report the number of medium breeds whose obey field is at least 0.7.
8. Report the number of large breeds whose obey field is at least 0.7.
9. Report the number of small breeds whose obey field is at most 0.3.
10. Report the number of medium breeds whose obey field is at most 0.3.
11. Report the number of large breeds whose obey field is at most 0.3.
12. Report all the small breeds and their obey field and order them by descending obey.
13. Report all the medium breeds and their obey field and order them by descending obey.
14. Report all the large breeds and their obey field and order them by descending obey.

You might also like