Download Adv_SQL_Student_Handout

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Database model wikipedia , lookup

Relational model wikipedia , lookup

Clusterpoint wikipedia , lookup

ContactPoint wikipedia , lookup

Transcript
CSCE156
Spring 2004
Lab 3 – Advanced SQL Queries Lab
Student Handout
1. Lab Objectives
Following the lab you should be able to:
 Combine different queries to perform complex database operations.
 Populate a database using SQL queries.
 Populate a database using a web interface
 Use different functions.
2. Prior to the laboratory
 Review the laboratory handout.
 Bring a few (1-3) music albums of your choice to the Lab.
 Take the pretest.
3. Topics
Topic
Combining different queries to perform complex database operations
Entering data into a database using SQL and a web interface
Use of different functions
Notes:
1. You can use MySQL Control Center or Command Line for doing the activities.
2. For all the activities you will be using the Album Database.
4. Activities/Exercises
1. Manipulating the Database with SQL queries.
2. Write pseudo code for the process of entering album information into the Album database.
3. Enter an Album of your choice into the Database using a web interface.
4. [OPTIONAL] Use of different functions.
Date:5/12/2017
Fig: The ER-Diagram of the Album database
1
CSCE156
Spring 2004
Activity 1: Manipulating the Database with SQL queries
 Connect to your database with MySQL Control Center or the command-line MySQL.
 In lab 2, you inserted the tables and data you will need for this lab.
 In this activity you will be inserting some new data into the Album database.
 Consider an audio CD with the following information:
o Album Title: Black and Blue
o Album Release Year: 2000
o Album Number: 3 (It’s the third album of this band.)
o Musicians Name: Carter, Nick
Dorough, Howard Dwaine
Littrell, Brian
McLean, A.J.
Richardson, Kevin
o Musician Country: USA (All of them)
o Band Name: Backstreet Boys
o GenreName: Pop
o Song List:
Track # Length
Song Title
(min:sec)
3:23
The Call
1.
3:49
Shape Of My Heart
2.
3:06
Get Another Boyfriend
3.
3:22
Shining Star
4.
4:22
I Promise You
5.
3:15
The Answer To Our Life
6.
3:31
Everyone
7.
3:43
More Than That
8.
3:54
Time
9.
3:15
Not For Me
10.
3:50
Yes I Will
11.
4:13
It's True
12.
4:03
How Did I Fall In Love With You
13.



You need to insert all of the given information into appropriate tables in the Album database.
Before inserting any information, you should figure out whether or not it is already in the database.
First, enter the band into the database
o Query the database to see whether the band “Backstreet Boys” is already in the database using
the following query:
SELECT * FROM Bands WHERE BandName LIKE “%Backstreet Boys%”;
o This query should return an empty set, which means that the band is not in the database.
o Thus, you need to add the band to the database using the following SQL query:
INSERT INTO Bands (BandName) VALUES ('Backstreet Boys');
o Now you can get the BandID, which you will need later, by running the following query:
SELECT BandID from Bands WHERE BandName = “Backstreet Boys”;
o Write down the BandID on the worksheet.
Date:5/12/2017
2
CSCE156
Spring 2004
 Now add the musicians to the database.
o Before entering the names of the musicians, you should check whether or not each musician is
already in the database.
o Since we know they are not present, and we assume you can figure out how you would write that
query, we will skip that step. (If you don’t trust us, you can write and run the queries.)
o Now enter the musicians of this band by running the following queries:
INSERT INTO Musicians (MusicianFirstName, MusicianLastName, MusicianCountry)
VALUES ('Nick', ‘Carter’, ‘USA’);
INSERT INTO Musicians (MusicianFirstName, MusicianLastName, MusicianCountry)
VALUES (‘Howard Dwaine’, ‘Dorough’ , ‘USA’);
INSERT INTO Musicians (MusicianFirstName, MusicianLastName, MusicianCountry)
VALUES (‘Brian’, ‘Littrell’, ‘USA’);
o For the sake of time, we will skip the other two members, but it should be clear how to do it.
o Using proper SQL queries, get the MusicianIDs for the three musicians entered in the previous
step and write them down on the worksheet.
 Now you need to add the members to the band—the previous step only added the musicians to the
database, but did not make them members of any band.
o You have the BandID and MusicianIDs, which will be used to associate the musicians and band.
o Run following query for every musician of the band, replacing the <MusicianID> and <BandID>
with the appropriate values (How many times do you have to run this?):
INSERT INTO BandMusicians VALUES (<MusicianID>,<BandID>);
 Enter the Album Information
o Before entering the album information, check whether or not the album is already present in the
database using the following query:
SELECT * FROM Albums WHERE AlbumTitle LIKE “%Black and Blue%”;
o As it is not in the database, you need to insert the relevant information into the Album table
using the following query, where again you should use the appropriate value for <BandID>:
INSERT INTO Albums (AlbumTitle,AlbumYear,BandID,AlbumNumber) VALUES
('Black and Blue’, 2000, <BandID>, 3);
 Now you need insert all of the songs of this album into the Songs table.
o Before entering each song into the database, you should check whether or not the song is in the
database. Again, for sake of time, we will skip that step.
o Use the following queries to enter the songs:
INSERT INTO Songs (SongTitle) VALUES ('The Call');
INSERT INTO Songs (SongTitle) VALUES ('Shape Of My Heart');
INSERT INTO Songs (SongTitle) VALUES ('Get Another Boyfriend’);
o Similarly you can enter the other songs, but you can skip them for sake of time.
 Next, the songs have to be associated with the album
o Convert the track lengths to seconds. For example, 3:42 translates to 3*60+42=222 seconds.
o Use the following query by replacing proper values for <SongTitle>, <TrackNumber> and
<TrackLength> to enter the songs into the AlbumSongs table:
INSERT INTO AlbumSongs (SongID, AlbumID, TrackNumber, TrackLength)
SELECT (SongID,AlbumID,<TrackNumber>,<TrackLength>)
FROM Songs, Albums WHERE
SongTitle = ’<SongTitle>’ AND AlbumTitle = 'Black and Blue’;
o How many times did you run this query?
 All information (well, except a few band members, and most of the songs) has been inserted.
Date:5/12/2017
3
CSCE156
Spring 2004
 Now write a query that lists the songs you have entered based only on the Album title “Black and
Blue”.
 Answer Question 2 on the worksheet for Activity 1.
Activity 2: Write pseudo code for the process of entering album information into the Album database
 Now you know all of the steps you need to follow to enter an album into database.
 Give pseudo code (list of steps) for entering an album into the Album database on the worksheet.
Activity 3: Enter an Album of your choice into the database using the web interface
 In this activity you need to enter all of the information for an Album of your choice into a common
shared database using a web interface.
 Before you start make sure you have the following information of the Album of your choice
o Album Title
o Album Release Year
o Album Number (optional)
o Musician Names
o Musician Countries
o Band Name
o All the track titles with their respective track lengths (converted into seconds).
 Go to the link below which contains the web interface to enter an album:
http://cse.unl.edu/~cusack/ShowFiles/PHP/AlbumDB/BandAndAlbum
 This web interface is created using simple PHP scripts that will allow you to enter information about
albums into the database.
 Enter the appropriate information in the text fields and proceed by clicking the “Next Step” button.
 Enter all of the information as you proceed through the pages.
 After you are done, answer Question 1 of Activity 3.
 Now write a query (similar to the one you have written in Activity 1) that lists the songs you have
entered above based only on the Album title.
 Run the query on the album database and generate the output.
 Now write another query that lists the band members entered above based only on the band name.
 Give the query to list the band members on the worksheet.
Activity 4: [OPTIONAL] Use of different functions
 Write a SQL query that would generate the name of the longest album (sum of all the song lengths)
in the database.
 Write a SQL query that would generate the name of the album with the shortest song on it.
 Write a SQL query that would generate the name of the album with maximum average song length.
 Write the names of the three albums on the worksheet, in order.
5. Think About




Why do we need to be extra careful while manipulating a shared database?
How does data integrity plays an important role while using shared databases?
Can we create a similar web interface (Activity 3) for entering data in Java or C++?
What advantages and disadvantages do interfaces have over using SQL commands directly?
Date:5/12/2017
4