You are on page 1of 8

Introduction to SQL Session 15 Assignment

Overview
For this assignment you will modify the ‘college’ database or schema as follows:
1. Create a new table called parking_permits using given parameters – 75 points
2. Import data into the parking_permits table using supplied CSV file – 25 points
3. Create two views for the parking_permits table (the query requires a join) – 25 points
4. Create one stored procedure for the parking_permits table (the query requires a join) – 25 points
5. Add a foreign key constraint to the ‘members’ table to reference the ‘advisors’ table – 25 points
6. Export the ‘college’ database or schema to submit your work – 25 points

Points - 200 points


How to Submit
You will export the ‘college’ database/schema and upload it on Canvas. I would suggest putting it in a zip file. You
may name the file whatever you wish. I will import the database/schema to my computer to check your work.

Due
By the end of the semester

#1 Create a new table called parking_permits as indicated below

Column Name Data Type Not Null Other


id INT(11) yes Auto increment, primary key
student_id SMALLINT(5) yes
plate_number VARCHAR(7) yes
state CHAR(2) yes Default: CA
vehicle_type ENUM(‘car’, ‘truck’, ‘motorcycle’) yes Default: car
vehicle_make VARCHAR(25) yes
vehicle_model VARCHAR(25) yes
school_year CHAR(5) yes

1
To access “Create Table” menu:

Table is created:

#2 Import data into the parking_permits table using supplied CSV file
Right click on the table name and use the menu for the import wizard:

Note: The wizard only allows you to import CSV or JSON files

You can also import/export data in an .sql file from the Server menu -> Data Import and from the Management tab at
the bottom of the navigator.

You can also import data by running the sql commands: File menu - > Run SQL Script

2
#3 Create two views for the parking_permits table (the query requires a join)
SQL Query #1:
Show the student_id (parking_permits), last name (members), first name (members) phone (members), plate number,
vehicle make, vehicle model, vehicle type where the school year is 15-16
Note that you are not required to show the year in the result grid, however, you may want to include it when you
develop the query to make sure you are getting the correct results.

SQL Query #2:


Show the student_id (parking_permits), last name (members), first name (members) phone (members), plate number,
vehicle make, vehicle model, vehicle type where the school year is 16-17
Note that you are not required to show the year in the result grid, however, you may want to include it when you
develop the query to make sure you are getting the correct results.

3
To execute the view:

#4 Create one stored procedure for the parking_permits table (the query requires a join)
SQL Query:
Show the student_id (parking_permits), last name (members), first name (members) phone (members), plate number,
vehicle make, vehicle model, vehicle type using the school year as a parameter passed to the procedure
To call a stored procedure:

Stored procedure results:

4
#5 Add a foreign key constraint to the ‘members’ table to reference the ‘advisors’ table
The purpose of adding a foreign key is to place a ‘constraint’ on a column in one table based on its existence in
another table. One main reason is to enforce referential integrity – to make sure that a value exists in one
table before it can be entered in another table.
In this assignment, we will only be adding a foreign key to the ‘members’ table for the ‘advisor_id’ column, to
make sure that an advisor id cannot be added in the members table that does not exist in the advisors table. In
a production environment, you would have many constraints and indexes as needed.

If I try to add an advisor id to a record in the members table that does not exist in the advisors table, I am
prevented from doing so:

5
How to set up a foreign key:

6
#6 Export the ‘college’ database or schema to submit your work:

7
CHECK THIS BOX

Dump Stored Procedures and function

You might also like