Presentation is loading. Please wait.

Presentation is loading. Please wait.

Exam 1 Review: ERDs, Schemas, SQL Out Describe Elements of an ERD Create Schema from ERD Notes: Associative Entities.

Similar presentations


Presentation on theme: "Exam 1 Review: ERDs, Schemas, SQL Out Describe Elements of an ERD Create Schema from ERD Notes: Associative Entities."— Presentation transcript:

1 Exam 1 Review: ERDs, Schemas, SQL Out Describe Elements of an ERD Create Schema from ERD Notes: Associative Entities

2 Section Course Has CRN Seats CourseID Title Describe Elements of this ERD Convert it into a Schema Location 1 table many table

3 ER Diagram Elements Entities? – Rectangles: Section, Course Attributes? – Ovals: CRN, Seats, Location, CourseID, Title Relationships? – Diamonds: Course HAS Section Cardinality of Relationship? – 1 course has at least one, at most many sections – 1 section has at least one, at most one course

4 Create the Schema: Rules Primary key field of “1” table put into “many” table as foreign key field 1:many relationships Relationship Becomes its Own Table Has many:1 relationships with original tables many:many relationships Primary key field of one table put into other table as foreign key field 1:1 relationships 1. Create a table for every entity 2. Create table fields for every entity’s attributes 3. Implement relationships between the tables

5 Section Course Has CRN Seats CourseID Title Make the Schema Course CourseID Title Section CRN Seats Location CourseID Location

6 Faculty Course Teaches FacultyID First name Last Name Last Name Course Number Course name Semester Describe the relationship in this ERD (Notice the many:many Relationship)

7 Create the Schema: Rules Primary key field of “1” table put into “many” table as foreign key field 1:many relationships Relationship Becomes its Own Table Has many:1 relationships with original tables many:many relationships Primary key field of one table put into other table as foreign key field 1:1 relationships 1. Create a table for every entity 2. Create table fields for every entity’s attributes 3. Implement relationships between the tables

8 Faculty Course FacultyID First name Last Name Last Name Course Number Course name Semester When Implementing Many:Many Relations in a Schema, Conceptually you are Doing This… Faculty_Course FacCourse ID Has NOTE: NOT A VALID ERD

9 Faculty FacultyID FirstName LastName Course CourseNumber CourseName Faculty-Course FacultyCourseID FacultyID CourseNumber Semester FacultyCourseIDFacultyIDCourseIDSemester 1510Spring 2011 2512Fall 2011 3310Spring 2011 4312Fall 2011 Converting that ERD into a schema

10 SQL Out SQL SELECT from 1 or Multiple Tables What is a JOIN really? ORDER BY, GROUP BY, MIN(), MAX()

11 SELECT from 1 Table SELECT field1, field2, … FROM table1 WHERE condition1 AND condition2 AND … – Retrieves field1, field2, etc. from table1 where condition1, condition2, etc. are met. Example: SELECT * FROM faculty_course WHERE FacultyID = 3 FacultyCourseIDFacultyIDCourseIDSemester 3310Spring 2011 4312Fall 2011

12 SELECT from N Tables SELECT field1, field2, … FROM table1, table2 WHERE join_condition AND condition1 AND condition2 AND … – Retrieves field1, field2, etc. from table1, table2 where records are related to one another, and where condition1, condition 2 are met. Example: SELECT LastName, Semester FROM faculty, faculty_course WHERE faculty.facultyID = faculty_course.facultyID

13 What Happens For a JOIN Really? SELECT * FROM table1, table2 – Fetches every possible combination (pair) of records from table 1 and table 2, displays all fields Table 1 Record 1 Record 2 Record 3 Table 2 Record 1 Record 2 Results T1.Record 1T2.Record 1 T1.Record 1T2.Record 2 T1.Record 2T2.Record 1 T1.Record 2T2.Record 2 T1.Record 3T2.Record 1 T1.Record 3T2.Record 2

14 OwnerIDName StreetCityStateZip 1Pete Front St.New YorkNY12212 2Tom Dock St.PhiladelphiaPA19122 3Sandy Bloor St.WashingtonDC10009 4Jim Carlton St.SeattleWA48323 5Joe Bathurst St.BostonMA09134 JOIN with No Conditions PetIDOwnerID NameWeightType 12 Rex40Dog 22 Snowball10Cat 34 Goldie1Fish 41 Lizzy6Lizard SELECT * FROM pet, owner

15 PetID NameWeightType OwnerID NameStreetCityStateZip 1Rex40Dog 21 PeteFront St.New YorkNY12212 1Rex40Dog 22 TomDock St.PhiladelphiaPA19122 1Rex40Dog 23 SandyBloor St.WashingtonDC10009 1Rex40Dog 24 JimCarlton St.SeattleWA48323 1Rex40Dog 25 JoeBathurst St.BostonMA09134 2Snowball10Cat 21 PeteFront St.New YorkNY12212 2Snowball10Cat 22 TomDock St.PhiladelphiaPA19122 2Snowball10Cat 23 SandyBloor St.WashingtonDC10009 2Snowball10Cat 24 JimCarlton St.SeattleWA48323 2Snowball10Cat 25 JoeBathurst St.BostonMA09134 3Goldie1Fish 41 PeteFront St.New YorkNY12212 3Goldie1Fish 42 TomDock St.PhiladelphiaPA19122 3Goldie1Fish 43 SandyBloor St.WashingtonDC10009 3Goldie1Fish 44 JimCarlton St.SeattleWA48323 3Goldie1Fish 45 JoeBathurst St.BostonMA09134 4Lizzy6Lizard 11 PeteFront St.New YorkNY12212 4Lizzy6Lizard 12 TomDock St.PhiladelphiaPA19122

16 OwnerIDName StreetCityStateZip 1Pete Front St.New YorkNY12212 2Tom Dock St.PhiladelphiaPA19122 3Sandy Bloor St.WashingtonDC10009 4Jim Carlton St.SeattleWA48323 5Joe Bathurst St.BostonMA09134 JOIN Conditions in WHERE clause PetIDOwnerID NameWeightType 12 Rex40Dog 22 Snowball10Cat 34 Goldie1Fish 41 Lizzy6Lizard SELECT * FROM pet, owner WHERE pet.ownerID = owner.ownerID

17 PetID NameWeightType OwnerID NameStreetCityStateZip 1Rex40Dog 21 PeteFront St.New YorkNY12212 1Rex40Dog 22 TomDock St.PhiladelphiaPA19122 1Rex40Dog 23 SandyBloor St.WashingtonDC10009 1Rex40Dog 24 JimCarlton St.SeattleWA48323 1Rex40Dog 25 JoeBathurst St.BostonMA09134 2Snowball10Cat 21 PeteFront St.New YorkNY12212 2Snowball10Cat 22 TomDock St.PhiladelphiaPA19122 2Snowball10Cat 23 SandyBloor St.WashingtonDC10009 2Snowball10Cat 24 JimCarlton St.SeattleWA48323 2Snowball10Cat 25 JoeBathurst St.BostonMA09134 3Goldie1Fish 41 PeteFront St.New YorkNY12212 3Goldie1Fish 42 TomDock St.PhiladelphiaPA19122 3Goldie1Fish 43 SandyBloor St.WashingtonDC10009 3Goldie1Fish 44 JimCarlton St.SeattleWA48323 3Goldie1Fish 45 JoeBathurst St.BostonMA09134 4Lizzy6Lizard 11 PeteFront St.New YorkNY12212 4Lizzy6Lizard 12 TomDock St.PhiladelphiaPA19122

18 PetID NameWeightType OwnerID NameStreetCityStateZip 1Rex40Dog 21 PeteFront St.New YorkNY12212 1Rex40Dog 22 TomDock St.PhiladelphiaPA19122 1Rex40Dog 23 SandyBloor St.WashingtonDC10009 1Rex40Dog 24 JimCarlton St.SeattleWA48323 1Rex40Dog 25 JoeBathurst St.BostonMA09134 2Snowball10Cat 21 PeteFront St.New YorkNY12212 2Snowball10Cat 22 TomDock St.PhiladelphiaPA19122 2Snowball10Cat 23 SandyBloor St.WashingtonDC10009 2Snowball10Cat 24 JimCarlton St.SeattleWA48323 2Snowball10Cat 25 JoeBathurst St.BostonMA09134 3Goldie1Fish 41 PeteFront St.New YorkNY12212 3Goldie1Fish 42 TomDock St.PhiladelphiaPA19122 3Goldie1Fish 43 SandyBloor St.WashingtonDC10009 3Goldie1Fish 44 JimCarlton St.SeattleWA48323 3Goldie1Fish 45 JoeBathurst St.BostonMA09134 4Lizzy6Lizard 11 PeteFront St.New YorkNY12212 4Lizzy6Lizard 12 TomDock St.PhiladelphiaPA19122

19 Order By, Group By ORDER BY: – Lists records in ascending or descending order, based on the specified field – Default is ascending (ASC). Specify DESC to make it descending. GROUP BY: – Return the SELECT results for each categorical value (group) in the specified field – One result is returned for each group

20 PetID NameWeightType OwnerID NameStreetCityStateZip 3Goldie 1 Fish44JimCarlton St.SeattleWA48323 4Lizzy 6 Lizard11PeteFront St.New YorkNY12212 2Snowball 10 Cat22TomDock St.PhiladelphiaPA19122 1Rex 40 Dog22TomDock St.PhiladelphiaPA19122 SELECT * FROM pet, owner WHERE pet.ownerID = owner.ownerID ORDER BY weight ASC – DESC instead of ASC would reverse the order SELECT count(*) FROM pet, owner WHERE pet.ownerID = owner.ownerID GROUP BY city Count(*) City 1Seattle 1New York 2Philadelphia

21 Min(), Max() SELECT min(field1), field2 FROM table – Returns 1 record; the smallest value in column “field1” and the first value in column “field2” SELECT field FROM table ORDER BY field – Returns all values, but the smallest one will appear first – Safer, because “min(field1)” and “field2” result probably don’t come from the same record!! SELECT max(field) FROM table – Works the same way, but gives biggest value


Download ppt "Exam 1 Review: ERDs, Schemas, SQL Out Describe Elements of an ERD Create Schema from ERD Notes: Associative Entities."

Similar presentations


Ads by Google