Chapter 11.1 and 11.2 Data Manipulation: Relational Algebra and SQL Brian Cobarrubia Introduction to Database Management Systems October 4, 2007.

Slides:



Advertisements
Similar presentations
Relational Database Operators
Advertisements

Chapter 6 The Relational Algebra
พีชคณิตแบบสัมพันธ์ (Relational Algebra) บทที่ 3 อ. ดร. ชุรี เตชะวุฒิ CS (204)321 ระบบฐานข้อมูล 1 (Database System I)
Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield.
The Relational Algebra
Database Systems Chapter 6 ITM Relational Algebra The basic set of operations for the relational model is the relational algebra. –enable the specification.
Relational Algebra Relational Calculus. Relational Algebra Operators Relational algebra defines the theoretical way of manipulating table contents using.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 8 Advanced SQL.
CPSC-608 Database Systems Fall 2011 Instructor: Jianer Chen Office: HRBB 315C Phone: Notes #3.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 The Relational Algebra and Relational Calculus.
The Relational Database Model. 2 Objectives How relational database model takes a logical view of data Understand how the relational model’s basic components.
The Relational Algebra and Calculus. Relational Algebra Overview Relational algebra is the basic set of operations for the relational model These operations.
The Relational Database Model
RELATIONAL ALGEBRA Objectives
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Chapter 4: SQL Basic Structure Set Operations Aggregate Functions Null Values Nested Subqueries.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: Introduction.
Chapter 3 Section 3.4 Relational Database Operators
Relational Algebra Example Database Application (COMPANY) Relational Algebra –Unary Relational Operations –Relational Algebra Operations From Set Theory.
Final Review Dr. Bernard Chen Ph.D. University of Central Arkansas.
Lecture 2 of Advanced Databases Advanced SQL Instructor: Mr.Ahmed Al Astal.
Chapter 9 Joining Data from Multiple Tables
M Taimoor Khan Course Objectives 1) Basic Concepts 2) Tools 3) Database architecture and design 4) Flow of data (DFDs)
Relational Algebra - Chapter (7th ed )
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 6 The Relational Algebra.
CSE314 Database Systems The Relational Algebra and Relational Calculus Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson Ed Slide Set.
The Relational Database Model
Chapter 11 – Data Manipulation: Relational Algebra and SQL1 Unit 7: Data Manipulation: Relational Algebra and SQL IT238: Data Modeling and Database Design.
METU Department of Computer Eng Ceng 302 Introduction to DBMS The Relational Algebra by Pinar Senkul resources: mostly froom Elmasri, Navathe and other.
M Taimoor Khan Course Objectives 1) Basic Concepts 2) Tools 3) Database architecture and design 4) Flow of data (DFDs)
Oracle DML Dr. Bernard Chen Ph.D. University of Central Arkansas.
From Relational Algebra to SQL CS 157B Enrique Tang.
Chapter 6 The Relational Algebra Copyright © 2004 Ramez Elmasri and Shamkant Navathe.
Chapter 5 Relational Algebra and Relational Calculus Pearson Education © 2009.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 6- 1.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 6- 1.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 Part a The Relational Algebra and Relational Calculus Hours 1,2.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 The Relational Algebra and Relational Calculus.
Al-Maarefa College for Science and Technology INFO 232: Database systems Chapter 3 “part 2” The Relational Algebra and Calculus Instructor Ms. Arwa Binsaleh.
Relational Algebra MBAD 613 R. Nakatsu. Relational Data Manipulation Language Query-by-Example; Query-by-Form Transform-Oriented Languages Relational.
Advanced Relational Algebra & SQL (Part1 )
Lecture 8 – SQL Joins – assemble new views from existing tables INNER JOIN’s The Cartesian Product Theta Joins and Equi-joins Self Joins Natural Join.
Views, Algebra Temporary Tables. Definition of a view A view is a virtual table which does not physically hold data but instead acts like a window into.
1 CS 430 Database Theory Winter 2005 Lecture 5: Relational Algebra.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 The Relational Algebra and Relational Calculus.
Dr. Mohamed Hegazi1 The Relational Algebra and Relational Calculus.
Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 340 Introduction to Database Systems.
CHAPTER 2 : RELATIONAL DATA MODEL Prepared by : nbs.
Chapter 6 The Relational Algebra Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
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
Chapter 11 – Data Manipulation: Relational Algebra and SQL1 Unit 9: Data Manipulation: Relational Algebra and SQL IT238: Data Modeling and Database Design.
Ritu CHaturvedi Some figures are adapted from T. COnnolly
CSE202 Database Management Systems
Database Systems Chapter 6
Chapter (6) The Relational Algebra and Relational Calculus Objectives
Relational Algebra Database Management Systems, 3rd ed., Ramakrishnan and Gehrke, Chapter 4.
COMP3017 Advanced Databases
Relational Algebra - Part 1
Chapter 3 Introduction to SQL(3)
Lecture 2 The Relational Model
Elmasri/Navathe, Fundamentals of Database Systems, 4th Edition
Chapter 4 The Relational Algebra and Calculus
The Relational Algebra and Calculus
LECTURE 3: Relational Algebra
The Relational Algebra and Relational Calculus
Chapter 8 Advanced SQL.
Database Systems: Design, Implementation, and Management
Chapter 4 Relational Algebra
Presentation transcript:

Chapter 11.1 and 11.2 Data Manipulation: Relational Algebra and SQL Brian Cobarrubia Introduction to Database Management Systems October 4, 2007

Agenda 11.1 – Relational Algebra Unary Operators Binary Operators 11.2 – Structured Query Language (SQL) SQL Queries on a Single Table SQL Queries on Binary‏ Operators Subqueries

Relational Algebra Unary Operators Select Operator is used to select a horizontal subset of the tuples that satisfy a selection condition from a relation σ<selection condition> (R) Project Operator is used to select a vertical subset of certain attributes from a relation Π<attribute list> (R)

Relational Algebra Binary Operators Cartesian Product Operator Set Theoretic Operators Union Intersection Difference Join Operators Natural Join Operator Equijoin Operator Theta Join Operator Outer Join Operator The Divide Operator

Relational Algebra Binary Operators Cartesian Product Operator is used to combine tuples from any two relations in a combinatorial fashion Relational Algebra Syntax: RELATION_R X RELATION_S

Relational Algebra Binary Operators Set Theoretic Operators are used to combine the tuples from two relations, which are then applied to two sets, the two relations must be union compatible Two relations (R and S) are said to be Union Compatible if they have: The same degree (i.e. Same number of attributes) Each pair of corresponding attributes in R and S share the same domain The operators are Union, Intersection, and Difference Union is denoted by R U S, it is an operator that includes all tuples that belong to either R or S or to both R and S. Duplicates are eliminated Intersection is denoted by R ∩ S, it is an operator that includes all the tuples that are only in R and S Difference is denoted by R – S, it is an operator that includes all tuples that are in R but not in S

Relational Algebra Join Operators Join Operators specify how to relate tables in the query, it is used to combine related tuples from two relations into single tuples In order to join the two relations R and S, they must be join compatible, which means that the join condition must involve attributes from R and S which share the same domain

Relational Algebra Join Operators Natural Join Operator produces all the different combinations of the tuples from the two relations R and S that satisfy a join condition R * <join condition> S

Relational Algebra Join Operators Equijoin Operator is the most common join that produces all the combinations of tuples from R and S that satisfy a join condition with only equality comparisons, there will be duplication of the joining attributes R [X]<join condition> S note that [X] also means Let R be STAFF table and S be DEPT: The resulting equijoin operation would produce:

Relational Algebra Join Operators Theta Join Operator produces the combinations of tuples from R and S that satisfy a join condition which does not have to involve equality comparisons (if it did then they are called an Equijoin) R [X]<join condition> S Let R be the CAR relation and S be the BOAT relation if a customer wants to buy both a car and boat but doesn’t want to spend more money on a boat then a car we would use a theta join operator to give a new relation with all the possible options CarPrice ≥ BoatPrice

Relational Algebra Join Operators Outer Join Operators are Inner Join operations that only look at matching tuples from one relation to the other, attributes of tuples with no matching tuples are set to NULL Left Outer Join is denoted by ]X|, and keeps every tuple in the left or first relation (R) Right Outer Join is denoted by |X[, and keeps every tuple in the right or second relation (S) Full Outer Join is denoted by ]X[, and keeps all the tuples in both the left and right relations when no matching tuples are found

Relational Algebra Join Operators Outer Join examples take two relations EMPLOYEE and DEPT Left Outer Join Right Outer Join Full Outer Join

Relational Algebra Divide Operator Divide Operator returns every tuple from R that match all tuples in S; R and S must be division compatible R ÷ S Let R be COMPLETED and S be DBPROJECT we can get the result as

Structured Query Language (SQL) Overview SQL Queries based on a Single Table Sample SQL Queries Handling null values SQL Queries based on Binary Operators Subqueries

Structured Query Language (SQL) Overview When using SQL with Relational Algebra rows are tuples, columns are attributes, and tables are relations One of the core components of using SQL is the SELECT statement, the basic form of the SQL SELECT statement is called a select-from-where block that contains three clauses: SELECT <column list> Takes the table and returns the columns listed as arguments FROM <table list> Produces a table from the argument given WHERE <condition> We can specify from this clause to search in the rows for specific value types using all the comparison operators (i.e. > ≥ = ≤ <) and different conditional operations

Structured Query Language (SQL) Overview Other useful clauses for a SELECT statement include: GROUP BY group_by_expression Forms groups of rows with the same value HAVING group_condition Filters the groups subject to some condition ORDER BY column name(s) Specifies the order of the output After your SELECT statement has been written a semi-colon ‘;’ is needed at the end to close up the statement

Structured Query Language (SQL) SQL Queries Based on a Single Table We can use an asterisk (*) to denote that all columns from a table is to be selected We can also use the logical operators AND OR and NOT with the WHERE clause to form more precise clauses When writing a query the hierarchy order in any expression follows The comparison operators (> ≥ = ≠ ≤ <) = is equals <> is not equals >= is greater than or equal to <= is less than or equal to > Is greater than < is less than NOT operator AND operator OR operator

Structured Query Language (SQL) SQL Queries Based on a Single Table Example 1 Suppose we want to know courses that are 3 hours a week SELECT * FROM COURSE WHERE COURSE.CO_HOURS = 3;

Structured Query Language (SQL) SQL Queries Based on a Single Table Example 2 Suppose we want to know all professors who make more than $6000 a month and the professors who work in a certain department (known as code 3) SELECT * FROM PROFESSOR WHERE PROFESSOR.SALARY/12 > 6000 AND PROFESSOR.PR_DPT_DCODE = 3;

Structured Query Language (SQL) SQL Queries Based on a Single Table We can also simulate a projection operation by using SELECT We can get all the colleges of a university from the co_college column with this command SELECT COURSE.CO_COLLEGE FROM COURSE;

Structured Query Language (SQL) SQL Queries Based on a Single Table Other operations to use and play with BETWEEN Used to search for rows in a specific range of values LIKE Used to search for a pattern in strings IN and NOT IN IN is evaluated in the context of being “equal to any member of” a set of values TRUNC Truncates a decimal value ROUND Rounds a decimal value DISTINCT Gets rid of duplicate rows

Structured Query Language (SQL) SQL Queries Based on a Single Table Aggregate functions take a set of values as input and returns one value as output Common Aggregate functions are: COUNT(x) Counts the number of non-null values in a set of values SUM(x) Sums all numbers in a set of values AVG(x) Computes the average of a set of numbers in a set of values MAX(x) Computes the maximum of a set of numbers in a set of values MIN(x) Computes the minimum of a set of numbers in a set of values

Structured Query Language (SQL) SQL Queries Based on a Single Table Example 3 Suppose we want to know the number of salaries, the total salary, average salary, the maximum, and the minimum salary from all the professors SELECT COUNT(PR_SALARY), SUM(PR_SALARY), AVG(PR_SALARY), MAX(PR_SALARY), MIN(PR_SALARY) FROM PROFESSOR;

Structured Query Language (SQL) SQL Queries Based on a Single Table Handling null values A data field without a value in it contains a null value, they occurs when a value is unknown and where a value is not meaningful If you try to query a column or row with a null value then it will include those spaces even though there isn’t a value associated to it Must use IS NULL and IS NOT NULL for comparison operators with null values This example will display that the query returned 9 rows SELECT TEXTBOOK.TX_PUBLISHER FROM TEXTBOOK;

Structured Query Language (SQL) SQL Queries Based on Binary Operators To perform Binary Operations on tables we use the follow keywords combined with JOIN in the FROM clause SELECT * FROM R CROSS JOIN S; INNER JOIN is used in the FROM clause when the Cartesian Product operation is accompanied by a Selection operation SELECT R.some_column(s) FROM R INNER JOIN S ON some_condition; ON clause is used to specify the join conditions

Structured Query Language (SQL) SQL Queries Based on Binary Operators To perform Set Theoretic Operators in SQL we use the following: SELECT * FROM R some_set_operator FROM S; Where some_set_operator can be: UNION INTERSECT MINUS

Structured Query Language (SQL) SQL Queries Based on Binary Operators To perform joins in SQL we use the JOIN statement and compare columns that match, we can also use the predefined joins for example in Natural Joins we can use either: SELECT * FROM R NATURAL JOIN S SELECT * FROM R JOIN S USING (COLUMN_1, COLUMN_2, ...); SELECT some_R_column FROM SECTION JOIN S ON some_R_column = some_S column, ...;

Structured Query Language (SQL) SQL Queries Based on Binary Operators Example 4 Take a Natural Join from two tables SECTION and TAKES with this input SELECT * FROM SECTION NATURAL JOIN TAKES;

Structured Query Language (SQL) SQL Queries Based on Binary Operators There is a FULL OUTER JOIN statement with the syntax SELECT * FROM GRAD_STUDENT FULL OUTER JOIN TAKES ON GRAD_STUDENT.GS_ST_SID = TAKES.TK_ST_SID;

Structured Query Language (SQL) Subqueries A complete SELECT statement that is embedded within another SELECT statement is a subquery Subqueries are used in: The SELECT list of a SELECT statement In the FORM clause In the WHERE clause In the ORDER BY clause We can use the comparison operators in combination with the ALL or ANY operator to treat the subquery as a set of values rather than individual values

Structured Query Language (SQL) Subqueries Example 4 Displays the name and salaries of those professors who earn more than all professors in department number 3 SELECT PROFESSOR.PR_NAME, PROFESSOR.PR_SALARY FROM PROFESSOR WHERE PROFESSOR.PR_SALARY > ALL (SELECT PROFESSOR.PR_SALARY WHERE PROFESSOR.PR_DPT_DCODE = 3);

The End