Presentation is loading. Please wait.

Presentation is loading. Please wait.

Slide 1 Chapter 7 – Part 2 Chapter 7 – Part 2 Constraints JOIN statement View.

Similar presentations


Presentation on theme: "Slide 1 Chapter 7 – Part 2 Chapter 7 – Part 2 Constraints JOIN statement View."— Presentation transcript:

1 Slide 1 Chapter 7 – Part 2 Chapter 7 – Part 2 Constraints JOIN statement View

2 Slide 2 Contents A.Art Museum Problem B.Solution C.Other Requirements

3 Slide 3 A. Art Museum Problem 1.Problem Description 2.Examples

4 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.

5 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.

6 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

7 Slide 7 B. Solution 1.Create Logical Diagram 2.Create Physical Diagram 3.Write SQL Statement to Create Tables 4.Create Constraints 5.Insert data into tables 6.Write SQL Statement to Display information

8 Slide 8 1. Create Logical Diagram

9 Slide 9 2. Create Physical Diagram

10 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, ARTDeceasedDaydatetime );

11 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, WORDescriptionvarchar(200), Constraint AK_Title_Copy Unique (WORTitle, WORCopy), Constraint FK_Works Foreign Key (ART_ID) References ARTISTS (ART_ID) on update cascade );

12 Slide 12 4. Create Constraints 4.1. Set Constraint For Nationality in Artist 4.2. Set Constraint Between Birth Date and Deceased Date

13 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 ARTISTS Add Constraint Chk_Nationality Check (ARTNationality in ('Canadian','English','French','German','Mexican', 'Russian‘,'Spanish','US'));

14 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 ARTISTS Add Constraint ChkDeceasedDay Check (ARTDeceasedDay > ARTBirthDay);

15 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.NameNationalityBirthdayDeceased day 1Artist 1US10/2/192530/6/1975 2Artist 2US5/4/1955 3Artist 3French6/7/193012/8/1990 4Artist 4Russian8/9/1950 No.TitleCopyDescriptionArtist ID 1Title 1OriginalPainting1 2Title 2First copyPhotograph1 3Title 3OriginalPainting3 4Title 4First copyPainting3 5Title 5Second copyPhotograph3

16 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

17 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 numWork From Artists a, Works w Where a.Art_ID = w.Art_ID Group by a.ArtName;  Result: (in Artist table, there are 4 artists) NamenumWork Artist 12 Artist 33

18 Slide 18  Using Left Join: Select a.ArtName, count(w.Wor_ID) as numWork From Artists a LEFT JOIN Works w ON a.Art_ID = w.Art_ID Group by a.ArtName;  Result NamenumWork Artist 12 Artist 20 Artist 33 Artist 40

19 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: NamenumWork Artist 11 Artist 20 Artist 31 Artist 40

20 Slide 20 C. Other Requirements 1.Creating View Problem 2.Creating View Solution

21 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.

22 Slide 22 2. Creating View Solution 2.1. What is view for? 2.2. Create view

23 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.

24 Slide 24 2.2. Create view 2.2.1. Create View to Show Name and Nationality 2.2.2. Create View to Show Artist Name and the number of Artist Work

25 Slide 25 2.2.1. Create View to Show Name and Nationality Create View ArtistInfo As Select ArtName, ArtNationality From Artists;

26 Slide 26 2.2.2. Create View to Show Artist Name and the number of Artist Work Create View ArtistsWorks As Select a.ArtName, Count(w.Wor_ID) as numWork From Artists a Left Join Works w on a.Art_ID = w.Art_ID Group by a.ArtName;

27 Slide 27


Download ppt "Slide 1 Chapter 7 – Part 2 Chapter 7 – Part 2 Constraints JOIN statement View."

Similar presentations


Ads by Google