35More on Matching (2)CS 200 Fall 2013SQLConsider the two tables StudentsRegisterIDN IDNName Course Term Mark with Students data and Register data(What are the primary keys for these tables? The foreign keys?) 36IDNName10Aaron11Sarah12Jose13MarieIDNCourseTermMark10CS20097018910Biol45897017511CS20097018111Econ335970194A model for how the where clause works (1)
CS 200 Fall 2013SQLSuppose you said select * fromStudents asS, Register Here’s what that produces — 37S.IDNS.NameR.IDNR.CourseR.TermR.Mark10Aaron10CS20097018910Aaron10Biol45897017510Aaron11CS20097018110Aaron11Econ33597019411Sarah10CS20097018911Sarah10Biol45897017511Sarah11CS20097018111Sarah11Econ33597019412Jose10CS20097018912Jose10Biol45897017512Jose11CS20097018112Jose11Econ33597019413Marie10CS20097018913Marie10Biol45897017513Marie11CS20097018113Marie11Econ335970194Every possible combinationof a Student and a Register record!A model for how the where clause works (2)(Note the convention for definingthe “table aliases” S and R.)More likely what you want is something like select* fromStudents asS, Register aswhereS.IDN = A model for how the where clause works (3)asRCS 200 Fall 2013SQLR 38S.NameR.IDNR.CourseR.TermR.Mark10Aaron10CS20097018910Aaron10Biol45897017511Sarah11CS20097018111Sarah11Econ335970194
CS 200 Fall 2013SQLOr more elegantly, selectS.IDN, Name, Course fromStudents asS, Register asR whereS.IDN = R.IDN which produces 39S.IDNS.NameR.Course10AaronCS20010AaronBiol45811SarahCS20011SarahEcon335How the where clause works (4)CS 200 Fall 2013SQLselectS.IDN, Name, Course fromStudents asS, Register asR where (Name = 'Aaron') and (S.IDN = R.IDN) which produces 40S.IDNS.NameR.Course10AaronCS20010AaronBiol458Just the courses for Aaron
CS 200 Fall 2013SQLselectCourse, R.IDN, Name fromStudents asS, Register asR where(Course = 'CS200') and(R.IDN = S.IDN) which produces 41R.CourseR.IDNS.NameCS20010AaronCS20011SarahList the students taking CS 200CS 200 Fall 2013SQLSuppose we add a third table StudentsRegisterCoursesIDN IDN Name Name Course Room Term Time Mark Description NB: Students.Name and Courses.Name hold dierent things Here’s some data for Courses What’s the primary key for Courses? 42NameRoomTimeDescriptionCS100DC1351M 1230Introduction to Computer UsageCS200MC4060M 1230Advanced Concepts for Computer UsageBiol458B2 350MWF 830Behavioural EcologyEcon335ML212TR 1000Economic Development