DOC PREVIEW
SJSU CS 157A - Lecture 3

This preview shows page 1-2-21-22 out of 22 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Relational AlgebraIntroductionIntroduction – cont’dSelectionExample 1:Example 2:Example 3Selection - PropertiesProjectionProjection - ExampleSlide 11Slide 12SET OperationsUnion CompatibilityUnion Compatibility - ExamplesUnion, Intersection, DifferenceExamplesSlide 18Cartesian ProductSlide 20QuestionA Sample DB: student-course-enrollment01/13/191Relational AlgebraRelational AlgebraEugene ShengComputer Science DepartmentNIU01/13/192Introduction Introduction one of the two formal query languages of the relational modelcollection of operators for manipulating relationsOperators: two types of operators – Set Operators: Union(),Intersection( ), Difference(-), Cartesian Product (x)–New Operators: Select (), Project (), Join (⋈)01/13/193Introduction – cont’dIntroduction – cont’dA Relational Algebra Expression: a sequence of relational algebra operators and operands (relations), formed according to a set of rules.The result of evaluating a relational algebra expression is a relation.01/13/194Selection Selection Denoted by c(R)Selects the tuples (rows) from a relation R that satisfy a certain selection condition c.It is a unary operatorThe resulting relation has the same attributes as those in R.01/13/195Example 1:SNO SNAME AGE STATES1 MIKE 21 ILS2 STEVE 20 LAS3 MARY 18 CAS4 MING 19 NYS5 OLGA 21 NYS:state=‘IL’(S)01/13/196Example 2:CNO CNAME CREDIT DEPTC1 Database 3 CSC2 Statistics 3 MATHC3 Tennis 1 SPORTSC4 Violin 4 MUSICC5 Golf 2 SPORTSC6 Piano 5 MUSICC:CREDIT  3(C)01/13/197Example 3SNO CNO GradeS1 C1 90S1 C2 80S1 C3 75S1 C4 70S1 C5 100S1 C6 60S2 C1 90S2 C2 80S3 C2 90S4 C2 80S4 C4 85S4 C5 100E:SNO=‘S1’and CNO=‘C1’(E)01/13/198Selection - PropertiesSelection - PropertiesSelection Operator is commutative C1(C2 (R)) = C2(C1 (R)) The Selection is an unary operator, it cannot be used to select tuples from more than one relations.01/13/199ProjectionProjectionDenoted by L(R), where L is list of attribute names and R is a relation name or some other relational algebra expression.The resulting relation has only those attributes of R specified in L.The projection is also an unary operation.8Duplication is removed from the result.01/13/1910Projection - ExampleProjection - ExampleExample 1: STATE (S)SNO SNAME AGE STATES1 MIKE 21 ILS2 STEVE 20 LAS3 MARY 18 CAS4 MING 19 NYS5 OLGA 21 NYSTATEILLACANY01/13/1911Projection - ExampleProjection - ExampleExample 2: CNAME, DEPT(C)CNO CNAME CREDIT DEPTC1 Database 3 CSC2 Statistics 3 MATHC3 Tennis 1 SPORTSC4 Violin 4 MUSICC5 Golf 2 SPORTSC6 Piano 5 MUSICCNAME DEPTDatabase CSStatistics MATHTennis SPORTSViolin MUSICGolf SPORTSPiano MUSIC01/13/1912Projection - ExampleProjection - ExampleExample 3: S#(STATE=‘NY'(S))SNO SNAME AGE STATES1 MIKE 21 ILS2 STEVE 20 LAS3 MARY 18 CAS4 MING 19 NYS5 OLGA 21 NYSNOS4S501/13/1913SET OperationsSET OperationsUNION: R1  R2INTERSECTION: R1  R2DIFFERENCE: R1 - R2CARTESIAN PRODUCT: R1  R201/13/1914Union CompatibilityUnion CompatibilityFor operators , , -, the operand relations R1(A1, A2, ..., An) and R2(B1, B2, ..., Bn) must have the same number of attributes, and the domains of the corresponding attributes must be compatible; that is, dom(Ai)=dom(Bi) for i=1,2,...,n.The resulting relation for , , or - has the same attribute names as the first operand relation R1 (by convention).01/13/1915Union Compatibility - ExamplesUnion Compatibility - ExamplesAre S(SNO, SNAME, AGE, STATE) and C(CNO, CNAME, CREDIT, DEPT) union compatible?Are S(S#, SNAME, AGE, STATE) and C(CNO, CNAME, CREDIT_HOURS, DEPT_NAME) union compatible?01/13/1916Union, Intersection, DifferenceUnion, Intersection, DifferenceT= R U S : A tuple t is in relation T if and only if t is in relation R or t is in relation ST = R  S: A tuple t is in relation T if and only if t is in both relations R and ST= R - S :A tuple t is in relation T if and only if t is in R but not in S01/13/1917ExamplesExamplesA1 A21 Red3 White4 greenB1 B23 White2 BlueR S01/13/1918ExamplesExamplesA1 A21 Red3 White4 Green2 BlueA1 A23 WhiteR SR  SS - RB1 B22 BlueA1 A21 Red4 GreenR - S01/13/1919Cartesian ProductCartesian ProductR(A1, A2, ..., Am) and S(B1, B2, ... , Bn)T(A1, A2, ... , Am, B1, B2, ..., Bn) = R(A1, A2, ..., Am) X S(B1, B2, ..., Bn)A tuple t is in T if and only if t[A1,A2, ... ,Am] is in R and t[B1, B2, ..., Bn] is in S.- If R has N1 tuples and S has N2 tuples, then T will have N1*N2 tuples.01/13/1920Cartesian ProductCartesian ProductA Ba1 2a2 4RSRx SC D E4 d1 e13 d2 e15 d3 e2A B C D Ea1 2 4 d1 e1a1 2 3 d2 e1a1 2 5 d3 e2a2 4 4 d1 e1a2 4 3 d2 e1a2 4 5 d3 e201/13/1921QuestionQuestionCompute S x CCompute S x E01/13/1922A Sample DB: A Sample DB: student-course-enrollmentstudent-course-enrollmentSNO SNAME AGE STATES1 MIKE 21 ILS2 STEVE 20 LAS3 MARY 18 CAS4 MING 19 NYS5 OLGA 21 NYCNO CNAME CREDIT DEPTC1 Database 3 CSC2 Statistics 3 MATHC3 Tennis 1 SPORTSC4 Violin 4 MUSICC5 Golf 2 SPORTSC6 Piano 5 MUSICSNO CNO GradeS1 C1 90S1 C2 80S1 C3 75S1 C4 70S1 C5 100S1 C6 60S2 C1 90S2 C2 80S3 C2 90S4 C2 80S4 C4 85S4 C5


View Full Document

SJSU CS 157A - Lecture 3

Documents in this Course
SQL

SQL

18 pages

Lecture

Lecture

44 pages

Chapter 1

Chapter 1

56 pages

E-R Model

E-R Model

16 pages

Lecture

Lecture

48 pages

SQL

SQL

15 pages

SQL

SQL

26 pages

Lossless

Lossless

26 pages

SQL

SQL

16 pages

Final 3

Final 3

90 pages

SQL

SQL

25 pages

Load more
Download Lecture 3
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture 3 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture 3 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?