rdb2 Lab
rdb2 Lab
Create the database “CD management” containing the following tables (the primary keys are
bold and underlined; the foreign keys are in italic):
Artist (IdArtist, Name, Nationality)
Album (IdAlbum, AlbumTitle, Year, IdArtist)
Song (Number, SongTitle, IdAlbum, Duration)
Part 2:
When you create the database, insert the following data:
Artist
IdArtist Name Nationality
1 Paul France
2 JO Germany
3 Fabio Italy
Album
IdAlbum AlbumTitle Year IdArtist
1 Automne 1992 1
2 Harrys Bar 2002 2
3 Stars 1996 1
Song
Number SongTitle IdAlbum Duration
1 Wonderful 2 4
2 Someone I knew 2 7
3 Forgiven 3 5
4 Merci 1 15
Part 3:
1. Write the statement allowing modifying the table “Artist” to add to it a column
“nbAlbum” (that corresponds to the number of albums recorded by the artist).
2. Write the statement allowing modifying the table “Artist” to add to it a column
“FName” (that corresponds to the name of the artist).
3. Add to the table “Song” the column “Day” that corresponds to the day of songs’
recording.
4. Add to the table “Song” the column “Type” that corresponds to the songs’ type.
5. Add to the table “Artist” the column “BirthDate” that corresponds to the birth date of
the artist.
6. Add to the table “Artist” a constraint in order that the age of the artist must be greater
than 16 years.
7. Add to the table “Song” a constraint in order that the duration of songs must be less
than 2 minutes.
8. Write a statement allowing renaming the column “SongTitle” of the table “Song” in
“Title”.
9. Write the statement allowing renaming the column “Type” of the table “Song” in
“SongType”.
10. Write the statement allowing renaming the column “Name” if the table “Artist” in
“ArtistName”.
11. Delete the column “Day” of the table “Song”.
12. Delete the column “ArtistName” on the table “Artist”.
13. Delete the column “SongType” of the table “Song”.
14. Delete, from the table “Song”, the constraint concerning the duration of songs.
15. Write the statement allowing modifying (to varchar(15)) the type of the column
“Nationality” of the table “Artist”.
16. Write the statement allowing modifying (to varchar(30)) the type of the column
“AlbumTitle” of the table “Album”.
17. Write the statement allowing modifying (to varchar(30)) the type of the column
“Title” of the table “Song”.
18. Write a SQL query allowing modifying the duration of the song Wonderful to 6
minutes.
19. Write a SQL query allowing modifying to 2008 the year of registration of the album
“Harrys Bar”.
20. Write the statement allowing decreasing of 3 minutes the duration of the song
“Merci”.
21. Write the statement allowing increasing of 7 minutes the duration of songs of the
album “Harrys Bar”.
22. Delete the songs having duration less than 10.
23. Delete the Belgian artist.
24. Delete the albums recorded before 2000.
Part 4:
One the data inserted in the database, write the SQL queries allowing to obtain
1. The artist having the Belgium nationality;
2. The artists having the French nationality; the result must be classed by decreasing
order in function of the first name of the artist;
3. The duration of the song “Merci”;
4. The albums created before the year 2000; the result must be classed by increasing
order in function of the title of the album;
5. The albums registered between 1991 and 1999;
6. The song having the maximum duration;
7. The songs of the album “Harrys Bar”; the result must be classed by decreasing order
in function of the duration of songs;
8. The title and the year of the album registering containing the song having the
maximum duration;
9. The albums registered by the French artist;
10. The fist name and the nationality of the artist that they sang the song “Forgiven”;
11. The number of songs of the album “Automne”;
12. The first name of the artists that they did not sang any song;
13. The artists that they sang at least a song;
14. The artists that they sang at least 2 songs;
15. The title of the albums and the number of songs of each album;
16. The number and the first name of artists with the number of songs of each one;
Part 5:
1. Write a view by table containing all the fields of the table.
2. Write a view view_Artist containing the first name and the nationality of artists.
3. Write a view view_Song containing the songs having duration greater than 10 minutes.
4. Write a view containing the albums registered by an Italian artist.
5. Write a view containing the albums registered in 1996.
6. Write a view containing the albums registered between 1990 and 2009.
7. Write a view containing the artists having the nationality starting by the letter “L”.
8. Write a view containing the songs of the album “Stars”.
9. Write a view containing the albums registered by the French and Italian artists.
10. Write a view containing the songs of the albums “Stars” and “Harrys Bar”.
11. Write a view containing the title of the song singed by a German artist and having
duration greater to 10 minutes.
12. Write a view containing the title and the registration year of registered albums by an
Italian artist.
13. Write a view french_song containing the songs of French artists.
14. Write a view containing the title and the duration of songs that have registered after
2002.
15. Write a view containing the first name and the nationality of the artist that sang the
song having the maximum duration.
16. Write a view containing the title and the registration year of the album containing the
song having the minimum duration.
17. Write a view containing the songs having duration greater than the average.
Part 6:
1. Write a trigger on the table Album allowing the automatic update of the column
nbAlbum of the table Artist following an operation of registration of the album by an
artist.
2.
a. Write the statement allowing modifying the table Album to add to it a column
nbSong (that corresponds to the number of songs containing in the album).
b. Write a trigger on the table Song allowing the automatic update of the column
nbSong of the table Album following an operation of insertion of a song in the
album.
3. Write a trigger on the table Song allowing the automatic update of the column nbSong
of the table Album following an operation of deleting of a song of an album.
4. Write a trigger on the table Song allowing the automatic update of the column nbSong
of the table Album following an operation of insertion, deleting, or also deleting of a
song of an album and the registration in an another album (update).
5.
a. Write the statement allowing modifying the table Artist to add to it a column
BirthDate (corresponding to the year of birthday of the artist)
b. Write a trigger allowing rejecting the invalid registrations of albums per artist.
An invalid registration has a date earlier than the birthday of the artist. In this
case, we must display a message “An artist cannot register an album before his
birthday”.
6. Write a function returning the title of the album number 2.
7. Write a function returning the name of the artist that registered the album “Stars”.
8. Write a function returning the number of the album containing the song “Merci”
9. Write a function returning the number (count) of the albums registered by “Paul”.
10. Write a function returning the number (count) of the registered songs in the album
“Stars”.
11. Write a function returning the number of the song having maximum duration.
12. Write a function returning the title of the oldest album
13. Write a function returning the name of the artist that sang the song having the
minimum duration.
14. Write a function returning the name of the artist that registered the newest album.
15. Write a function returning the title of the album having the number given as parameter.
16. Write a function returning the number of the registered songs in an album having a
title given as parameter of the function.
17. Write a function returning the name of the artist that registered an album having a title
given as parameter.
18. Write a function returning the year of registration of an album registered by an artist
having the name given as parameter.
19. Write a stored procedure displaying the list of German artists.
20. Write a stored procedure displaying the title of the newest album.
21. Write a stored procedure displaying the list of registered albums by the artist “Paul”.
22. Write a stored procedure displaying the list of songs having the maximum duration.
23. Write a stored procedure displaying the title of the song having the minimum duration.
24. Write a stored procedure displaying the title and the duration of songs having duration
greater than the average duration of songs.
25. Write a stored procedure displaying the name and the nationality of the artist that
registered the newest album.
26. Write a stored procedure displaying the list of registered albums by an artist having
the name given as parameter.
27. Write a stored procedure displaying the list of registered albums by an artist having
the nationality given as parameter.
28. Write a stored procedure displaying the list of registered songs in an album having the
number given as parameter.
29. Write a stored procedure displaying the name and the nationality of the artist that
registered the album having the title given as parameter.
30. Write a stored procedure displaying the name and the nationality of the artist that sang
a song having a number given as parameter.
31. Write a stored procedure displaying the list of registered songs by an artist having the
name given as parameter.
32. Write a stored procedure displaying the list of registered songs at a date (year) given
as parameter.
33. Write a stored procedure displaying the list of artists that registered albums between 2
given dates as parameters.
34. By using a cursor, write a function allowing calculating the number of album
registered before the year 2000 by an artist having an identifier given as parameter.
35. By using a cursor, write a function allowing calculating the number of album
registered before the year 2000 by an artist having a name given as parameter.
36. By using a cursor, write a function allowing calculating the total duration of songs of
an album having title given as parameter.
37. By using a cursor, write a stored procedure allowing increasing of 4 minutes the
duration of songs of album number 2.
38. By using a cursor, write a stored procedure allowing decreasing of 3 minutes the
duration of songs of album having title given as parameter.
39. By using a cursor, write a stored procedure allowing deleting all songs having
duration less than 3 minutes.
40. By using a cursor, write a stored procedure allowing displaying the last n albums
registered at a given date. The procedure receives in parameter the integer n and the
date of the album registration.
Part 7:
1. Write a transaction allowing adding 2 albums in the table album (choose values); if
the number of albums doesn’t exceed 6, the transaction is validated; else the insertion
operation is cancelled.
2. Write a transaction allowing adding three songs in the table Song ; if the number of
songs doesn’t exceed 5 or the total summation of songs durations is less than 40
minutes so the transaction is validated, else the operation is cancelled.
3. Write a transaction allowing placing all songs having durations less than 10 minutes
in a table archive_song having the same structure of the table Song.
4. Write a transaction allowing placing all artists having ages less than 60 years old in a
table old_artists having the same structure of the table Artist.
5. Write a transaction allowing placing all registered albums before the year 1999
toward Italian artist (supposed unique); if the number of albums of this artist is less
than 2 so the transaction is valid, else the transaction is cancelled.
6. Write a transaction allowing placing all songs having duration less than 10 minutes in
the album containing the song having the minimal duration; if the number of song of
this album is less than 3 so the transaction is valid, else the transaction is cancelled.