Slide 1 Chapter 7 – Part 2 Chapter 7 – Part 2 Constraints Constraints JOIN statement JOIN statement View View
Slide 1
Chapter 7 – Part 2Chapter 7 – Part 2
ConstraintsConstraintsJOIN statementJOIN statement
ViewView
Slide 2
Contents
A. Art Museum ProblemB. SolutionC. Other Requirements
Slide 3
A. Art Museum Problem
1. Problem Description2. Examples
Slide 4
1. Problem Description
DAWN museum where a lot of valuable works have been exhibited needs to manage and classify its works. Therefore, a database is one of the best solutions. The owner also requests all of works to be arranged by the artist who created these works. To identify the artist, the following information will be used: artist Id, artist name, nationality, birthday, deceased day. In addition, each artist may have many works or not. The work of each artist will be identified by work id, title, copy and description.
Slide 5
Besides the general information, there are some special requirements such as: Artist’s nationality must be within these following countries:
Canadian, English, French, German, Mexican, Russian, Spanish and US.
Deceased day (if any) must be greater than artist’s birthday. Artist name must be unique Both Work title and work copy must be unique
As a database developer, you are requested to design the above database.
After that, write SQL statement to display artist name and the number of works (count of works) of each artist.
Finally, write SQL statement to display artist name and the number of works (count of works) of each artist and these work must be first copy.
Slide 6
2. Examples Artist name: LEIGHTON, SIR FREDERICK. 1830-1896. Nationality: English Birthday: 1830 Deceased day: 1896 Some Chief Works:
First work:• Title: Hercules Wrestling with Death• Copy: First copy• Description: Painting
Second work;• Title: Daphnephoria• Copy: Original• Description: Painting
Slide 7
B. Solution
1. Create Logical Diagram2. Create Physical Diagram3. Write SQL Statement to Create Tables 4. Create Constraints5. Insert data into tables6. Write SQL Statement to Display information
Slide 8
1. Create Logical Diagram
Slide 9
2. Create Physical Diagram
Slide 10
3. Write SQL Statement to Create Tables
Create Table ARTISTS ( ART_ID int not null Primary
Key Identity,
ARTName varchar(50) not null Unique ,
ARTNationality varchar(50) not null, ARTBirthDay datetime not null, ARTDeceasedDay datetime);
Slide 11
Create Table WORKS ( WOR_ID bigint not null Primary
Key Identity, ART_ID int not null, WORTitle varchar(100) not null, WORCopy varchar(20) not null, WORDescription varchar(200), Constraint AK_Title_Copy Unique (WORTitle,
WORCopy), Constraint FK_Works Foreign Key (ART_ID)
ReferencesARTISTS (ART_ID) on update cascade
);
Slide 12
4. Create Constraints
4.1. Set Constraint For Nationality in Artist 4.2. Set Constraint Between Birth Date and
Deceased Date
Slide 13
4.1. Set Constraint For Nationality in Artist
Artist’s nationality must be within these following countries: Canadian, English, French, German, Mexican, Russian, Spanish, US.
Alter Table ARTISTSAdd Constraint Chk_Nationality Check (ARTNationality in
('Canadian','English','French','German','Mexican','Russian‘, 'Spanish','US'));
Slide 14
4.2. Set Constraint Between Birth Date and Deceased Date
Deceased day (if any) must be greater than artist’s birthday
Alter Table ARTISTSAdd Constraint ChkDeceasedDay Check
(ARTDeceasedDay > ARTBirthDay);
Slide 15
5. Insert data into tables Exercise: Using DML, write SQL statements to
insert the following data into tables (Artists and Works):
Artists table:
Works table:
No. Name Nationality Birthday Deceased day
1 Artist 1 US 10/2/1925 30/6/1975
2 Artist 2 US 5/4/1955
3 Artist 3 French 6/7/1930 12/8/1990
4 Artist 4 Russian 8/9/1950
No. Title Copy Description Artist ID
1 Title 1 Original Painting 1
2 Title 2 First copy Photograph 1
3 Title 3 Original Painting 3
4 Title 4 First copy Painting 3
5 Title 5 Second copy Photograph 3
Slide 16
6. Write SQL Statement to Display information
6.1. Count Number of Artist’s Work 6.2. Count Number of Artist’s Work which is
First Copy
Slide 17
6.1. Count Number of Artist’s Work
Write SQL statement to display all artist name and the number of works (count of works) of each artist.
Using Joins:Select a.ArtName, count(w.Wor_ID) as numWorkFrom Artists a, Works wWhere a.Art_ID = w.Art_IDGroup by a.ArtName;
Result:
(in Artist table, there are 4 artists)
Name numWork
Artist 1 2
Artist 3 3
Slide 18
Using Left Join:Select a.ArtName, count(w.Wor_ID) as
numWorkFrom Artists a LEFT JOIN Works w ON a.Art_ID
= w.Art_IDGroup by a.ArtName;
ResultName numWork
Artist 1 2
Artist 2 0
Artist 3 3
Artist 4 0
Slide 19
6.2. Count Number of Artist’s Work which is First Copy
Write SQL statement to display all artist name and the number of works (count of works) of each artist and these work must be first copy.
Select a.ArtName, count(w.Wor_ID) as numWork
From Artists a LEFT JOIN Works w ON a.Art_ID = w.Art_ID
Where w.WorCopy =‘First copy’ or w.WorCopy is NULL
Group by a.ArtName; Result:
Name numWork
Artist 1 1
Artist 2 0
Artist 3 1
Artist 4 0
Slide 20
C. Other Requirements
1. Creating View Problem2. Creating View Solution
Slide 21
1. Creating View Problem Code a SQL statement to create a view that shows
NAME and NATIONALITY for all artist. Code a SQL statement to create a view that show
artist NAME and computes the number of works for each artist.
Slide 22
2. Creating View Solution
2.1. What is view for?2.2. Create view
Slide 23
2.1. What is view for? A SQL view is a virtual table that us constructed
from other tables or views. A view has no data of its own, but obtain data from tables or other views. View are constructed from SQL SELECT statements. Using views to hide columns and rows. Using views to display results of computed columns. Using views to hide complicated SQL syntax.
Slide 24
2.2. Create view
2.2.1. Create View to Show Name and Nationality2.2.2. Create View to Show Artist Name and the
number of Artist Work
Slide 25
2.2.1. Create View to Show Name and Nationality
Create View ArtistInfo AsSelect ArtName, ArtNationalityFrom Artists;
Slide 26
2.2.2. Create View to Show Artist Name and the number of Artist Work
Create View ArtistsWorks AsSelect a.ArtName, Count(w.Wor_ID) as
numWorkFrom Artists a Left Join Works w on a.Art_ID
= w.Art_IDGroup by a.ArtName;
Slide 27