Relational Query Operators (Algebra). Relational Query Operators Set operations: Union, intersection, difference, Cartesian product Relational operations:

Slides:



Advertisements
Similar presentations
COMP 5138 Relational Database Management Systems Semester 2, 2007 Lecture 5A Relational Algebra.
Advertisements

Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield.
Relational Algebra Rohit Khokher. Relational Algebra Set Oriented Operations UnionIntersectionDifference Cartesian Product Relation Oriented Operations.
Normalization ISYS 464. Database Design Based on ERD Strong entity: Create a table that includes all simple attributes –Composite Weak entity: add owner.
SQL Sub (or Nested ) Query. Examples Q: Find students whose GPA is below the average. –The criteria itself requires a SQL statement. –SELECT * FROM student.
Concepts of Database Management Sixth Edition
Concepts of Database Management Seventh Edition
1 Relational Algebra & Calculus. 2 Relational Query Languages  Query languages: Allow manipulation and retrieval of data from a database.  Relational.
Data Modeling and Relational Database Design ISYS 650.
Querying Database ISYS 363. Basic Query Language Operations Selection Projection Join Aggregates: Max, Min, Sum, Avg, Count –Totals and SubTotals –GroupBy.
SQLPlus Commands. Oracle Account Server: libra.sfsu.edu Telnet: libra.sfsu.edu How to use Oracle: –
Introduction to Database ISYS 363. File Concepts File consists of a group of records. Each record contains a group of fields. Example: Student file –SIDSnameMajorSexGPA.
Structured Query Language Review ISYS 650. Language Overview Three major components: –Data definition language, DDL Create, Drop and Alter Tables or Views.
Chapter 2 The Relational Database Model
Data Modeling with ERD ISYS 363. Entity-Relationship Diagram An entity is a “thing” in the real world, such as a person, place, event for which we intend.
Complex Conditions. Logical Operators: AND, OR, NOT AND Cond1Cond2Cond1 AND Cond2T TF FTF OR Cond1Cond2Cond1 OR Cond2T TF FTF NOT CondNOT Cond T F.
Introduction to Database ISYS 363. File Concepts File consists of a group of records. Each record contains a group of fields. Example: Student file –SIDSnameMajorSexGPA.
Introduction to Database. File Formats Comma delimited file –"s1","peter",3 –"s2","paul",2.5 –"s3","mary",3.5 –Demo: Excel – Data/Import Extended Markup.
Basic SQL Select Commands. Basic Relational Query Operations Selection Projection Natural Join Sorting Aggregation: Max, Min, Sum, Count, Avg –Total –Sub.
Querying Database ISYS 363. Basic Query Language Operations Selection Projection Join Aggregates: Max, Min, Sum, Avg, Count –Totals and SubTotals –GroupBy.
Introduction to Access BUS 782. Access Objects Tables –Open –Design –New –Wizard Queries Forms Reports Pages.
Relational Algebra. Set operations: Union, intersection, difference, Cartesian product Relational operations: Selection, projection, join, division.
Querying Database. Basic Query Language Operations Selection Projection Join Aggregates: Max, Min, Sum, Avg, Count –Totals and SubTotals –GroupBy Calculated.
Relational Operators, SQL, and Access Query ISYS 562.
The Relational Database Model
Structured Query Language Introduction to SQL What is SQL? – –When a user wants to get some information from a database file, he can issue a query.1.
RELATIONAL ALGEBRA Objectives
SQL: Interactive Queries (1) John Ortiz Lecture 11SQL: Interactive Queries (1)2 Basic Select Statement  Basic form of the select statement: select target-attribute-list.
Relational Model & Relational Algebra. 2 Relational Model u Terminology of relational model. u How tables are used to represent data. u Connection between.
Copyright © Curt Hill The Relational Algebra What operations can be done?
Relational Query Operators (ii). Other Joins Theta join: Apply any specified condition on the product of two relations. Example: BoyStudent: BID, Bname,
CHAPTER 8: MANAGING DATA RESOURCES. File Organization Terms Field: group of characters that represent something Record: group of related fields File:
RELATIONAL ALGEBRA Relational Database Handout - 3.
Concepts of Database Management Seventh Edition
Advanced SQL for Decision Support ISYS 650. Set Operators Union Intersect Difference Cartesian product.
Relational Algebra Relational Operators – Set operations –Special relational operators Relational Algebra Expressions PROJECT (SELECT FROM (A TIMES B)…
1 Relational Algebra & Calculus Chapter 4, Part A (Relational Algebra)
Introduction to Access ISYS 363. Creating a New Database MS Office button/New –Blank database –New database name and location.
Advanced Relational Algebra & SQL (Part1 )
Data Modeling with ERD BUS 782. Entities An entity is a person, place, object, event, or concept in the user environment about which the organization.
Querying Database ISYS 363.
Relational Algebra Relational Calculus
CMPT 258 Database Systems Relational Algebra (Chapter 4)
CSC271 Database Systems Lecture # 7. Summary: Previous Lecture  Relational keys  Integrity constraints  Views.
Introduction to Access BUS 782. Creating a New Database MS Office button/New –Blank database –New database name and location.
Introduction to Access ISYS 363. Access Objects Tables –Open –Design –New –Wizard Queries Forms Reports Pages.
MIS 3053 Database Design & Applications The University of Tulsa Professor: Akhilesh Bajaj RM/SQL Lecture 4 © Akhilesh Bajaj, 2000, 2002, 2004, All.
SQL BY MR.PRASAD SAWANT INDIA Introduction to SQL What is SQL? –When a user wants to get some information from a database file, he can issue a query.1.
Introduction to Access ISYS 363. Access Objects Tables –Open a table: Double click the table name –Home/View: Datasheet view Design view Queries Forms.
Set Operators. Union Intersect Difference Cartesian product.
Data Modeling with ERD ISYS 363.
LECTURE THREE RELATIONAL ALGEBRA 11. Objectives  Meaning of the term relational completeness.  How to form queries in relational algebra. 22Relational.
Relational Algebra COMP3211 Advanced Databases Nicholas Gibbins
Normalization ISYS 464. Database Design Based on ERD Strong entity: Create a table that includes all simple attributes –Composite Weak entity: add owner.
Database Systems (資料庫系統)
COMP3017 Advanced Databases
Introduction to Database
Introduction to Database
Theory behind the relational engine
Theory behind the relational engine
Relational Algebra.
Relational Databases Relational Algebra (2)
LECTURE 3: Relational Algebra
Relational Databases Relational Algebra (1) Select, project, join.
Querying Database ISYS 363.
DCT 2053 DATABASE CONCEPT Chapter 2.2 CONTINUE
CS639: Data Management for Data Science
Introduction to Database
Relational Algebra & Calculus
Presentation transcript:

Relational Query Operators (Algebra)

Relational Query Operators Set operations: Union, intersection, difference, Cartesian product Relational operations: Selection, projection, join, division

Union Set1={A, B, C} Set2={C, D, E} Union: Members in Set 1 or in Set 2 –Set1 U Set 2 = {A, B, C, D, E} Logical operator OR –Major = ‘CIS’ OR Major = ‘Acct’ –JobSkill = ‘Java’ OR JobSkill = ‘VB’

Intersect Members in Set 1 and in Set 2 –Set1 ∩ Set2={C} Logical operator AND –Major = ‘CIS’ AND GPA > 3.0 –JobSkill = ‘Java’ AND Experience > 5 –Note: Major = ‘CIS’ AND Major = ‘Acct’ is nonsense

Difference Set1={A, B, C} Set2={C, D, E} Set1 – Set2: Members in Set1 but not in set2 = {A,B} Set2 – Set1:Members in Set2 but not in set1 = {D, E} Set1-Set2 ≠ Set2 – Set1 Logical operator: NOT

Union Compatibility Two relations that have the same number of attributes and same type of attributes. Union, Intersect and difference operators require the two relations to be union compatible.

Union Compatibility Examples File 1: –SID – 9 characters –Sname – 25 characters File 2: –SSN – 9 characters –Ename – 25 characters File 3: –Ename – 25 characters –EID – 9 characters File 1 and file 2 are union compatible; file 1 and file 3 are not; file 2 and file 3 are not.

Use Union and Difference to Simulate Intersect Set1 ∩ Set2 = Set1 – (Set1 – Set2)

Venn Diagram Set 1: Young: Age<30 Set 2: Rich: Income>100,000’ Set 3: Smart: IQ > 150

Files as Sets Business students’ file: BusSt Science student’s file: SciSt –BusSt U SciSt: –BusSt ∩ SciSt –BusSt – SciSt Spring 06 Student file: S06St Fall 06 Student file: F06St –S06St – F06St –F06St – S06St

Product Set1 = {a, b, c} Set2 = {X, Y, Z} Set1 X Set2 = {aX, aY aZ, bX, bY, bZ, cX, cY, cZ}

Faculty File: FIDFname F1Chao F2Smith Student File: SIDSname FID S1PeterF1 S2PaulF2 S3SmithF1 Faculty X Student:

Selection Selection operation works on a single relation and defines a relation that contains records that satisfy the criteria. – σ criteria ( Relation) – σ Major = ‘Bus’ AND GPA > 3.0 (Student)

Projection Projection operation works on a single relation and defines a vertical subset of the relation, extracting the values of specified attributes and eliminating duplicates. π a1, a2, … (Relation) π sid, sname (Student)

Student file: SID, SnameSexMajor S1PeterMBus S2PaulMArt S3MaryFBus S4NancyFSci S5PeterMArt π sid, sname (Student) π sname, sex (Student) π sname, sex ( σ Major = ‘Bus’ (Student)) –Does not equal to: σ Major = ‘Bus’ ( π sname, sex (Student))

Duplications due to Projection WorkLog file: EIDPjIDHours E1P25 E1P14 E2P26 E2P18 E3P14 π eid (WorkLog) Relation contrains: no duplication Eliminating duplicates may cause problems: –π Hours ( σ PjID = ‘P1 (WorkLog)) In practice, users determine whether to eliminate duplicates: –SELECT DISTINCT EID FROM WorkLog; –SELECT HOURS FROM WorkLog WHERE PjID = ‘P1’;

Natural Join The two relations must have common attributes. Combines two relations to form a new relation where records of the two relations are combined if the common attributes have the same value. One occurrence of each common attribute is eliminated.

Faculty File: FIDFname F1Chao F2Smith Student File: SIDSname FID S1PeterF1 S2PaulF2 S3SmithF1 Faculty Join Student = π All except the duplicated attributes (σ Faculty.FID = Student.FID ( Faculty X Student )) Note: Use RelationName.FieldName to make a field name unique.

Examples University database: –Student: SID, Sname, Sex, Major, GPA, FID –Account: SID, Balance –Faculty: FID, Fname, Phone –Course: CID, Cname, Credits, FID –StudentCourse: SID, CID

Questions Q1: Display College of Business students’ ID and name. Q2: Display students’ ID and name who owe university more than $2000. Q3: Display faculty’s name and phone if the student’s GPA is lower than 2.0. Q4: Display faculty’s name and phone if the faculty advises at least one student. Q5: Display faculty’s name and phone if the faculty does not advise any student. Q6: Display students’ ID and name who are taking at least one course. Q7: Display students’ ID and name who do not take any course. Q8: Display students’ ID and name who are taking 464 and GPA < 2.0 Q9: Display students’ ID and name who are taking Chao’s courses. Q10: Display students’ ID and name who are taking 464 and 363.