Relational Algebra & SQL

Slides:



Advertisements
Similar presentations
© Abdou Illia MIS Spring 2014
Advertisements

A Guide to SQL, Seventh Edition. Objectives Use joins to retrieve data from more than one table Use the IN and EXISTS operators to query multiple tables.
Relational Database Operators
The Relational Model and Relational Algebra Nothing is so practical as a good theory Kurt Lewin, 1945.
Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield.
Revision for Mid 1 ©Silberschatz, Korth and Sudarshan7.2Database System Concepts Functional Dependencies FDs defined over two sets of attributes: X,
CSC271 Database Systems Lecture # 13. Summary: Previous Lecture  Grouping through GROUP BY clause  Restricted groupings  Subqueries  Multi-Table queries.
COMP 3715 Spring 05. Working with data in a DBMS Any database system must allow user to  Define data Relations Attributes Constraints  Manipulate data.
Relational Algebra 1 Chapter 5.1 V3.0 Napier University Dr Gordon Russell.
Database Systems Chapter 6 ITM Relational Algebra The basic set of operations for the relational model is the relational algebra. –enable the specification.
Chapter 2 The Relational Database Model
Relational Algebra Relational Calculus. Relational Algebra Operators Relational algebra defines the theoretical way of manipulating table contents using.
The Relational Database Model
Inner join, self join and Outer join Sen Zhang. Joining data together is one of the most significant strengths of a relational database. A join is a query.
Chapter 11.1 and 11.2 Data Manipulation: Relational Algebra and SQL Brian Cobarrubia Introduction to Database Management Systems October 4, 2007.
The Relational Database Model
Relational algebra SHIRAJ MOHAMED M | MIS 1. Relational algebra Notation SHIRAJ MOHAMED M | MIS 2.
3 The Relational Model MIS 304 Winter Class Objectives That the relational database model takes a logical view of data That the relational model’s.
The Relational Database Model
Databases Illuminated
RELATIONAL ALGEBRA Objectives
Chapter 3 Section 3.4 Relational Database Operators
DBSQL 3-1 Copyright © Genetic Computer School 2009 Chapter 3 Relational Database Model.
Lecture 2 of Advanced Databases Advanced SQL Instructor: Mr.Ahmed Al Astal.
SQL advanced select using Oracle 1 7. Multiple Tables: Joins and Set Operations 8. Subqueries: Nested Queries.
A Guide to MySQL 5. 2 Objectives Use joins to retrieve data from more than one table Use the IN and EXISTS operators to query multiple tables Use a subquery.
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 )
1 The Relational Database Model. 2 Learning Objectives Terminology of relational model. How tables are used to represent data. Connection between mathematical.
Algjebra Relacionale & SQL
Relational Algebra References: Databases Illuminated by Catherine Ricardo, published by Jones and Bartlett in 2004 Fundamentals of Relational Databases.
M Taimoor Khan Course Objectives 1) Basic Concepts 2) Tools 3) Database architecture and design 4) Flow of data (DFDs)
9/7/2012ISC329 Isabelle Bichindaritz1 The Relational Database Model.
3 1 Chapter 3 The Relational Database Model Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel.
Relational Algebra  Souhad M. Daraghma. Relational Query Languages Query languages: Allow manipulation and retrieval of data from a database. Relational.
Relational Algebra – Part 2
From Relational Algebra to SQL CS 157B Enrique Tang.
CS424 Relational Data Manipulation Relational Data Manipulation Relational tables are sets. Relational tables are sets. The rows of the tables can be considered.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
SQL – Structured Query Language
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 Part a The Relational Algebra and Relational Calculus Hours 1,2.
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 )
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
SQL advanced select using Oracle 1. 2 Select Simple –data from a single table Advanced –data from more tables join sub-queries.
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.
Presented By: Miss N. Nembhard. Relation Algebra Relational Algebra is : the formal description of how a relational database operates the mathematics.
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.
Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 340 Introduction to Database Systems.
3 1 Database Systems The Relational Database Model.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 3 The Relational Database Model.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
Week 2 Lecture The Relational Database Model Samuel ConnSamuel Conn, Faculty Suggestions for using the Lecture Slides.
 MySQL  DDL ◦ Create ◦ Alter  DML ◦ Insert ◦ Select ◦ Update ◦ Delete  DDL(again) ◦ Drop ◦ Truncate.
LECTURE THREE RELATIONAL ALGEBRA 11. Objectives  Meaning of the term relational completeness.  How to form queries in relational algebra. 22Relational.
1 Relational Algebra and SQL. 2 Relational Query Languages Languages for describing queries on a relational database Relational AlgebraRelational Algebra.
Relational Algebra COMP3211 Advanced Databases Nicholas Gibbins
Ritu CHaturvedi Some figures are adapted from T. COnnolly
Chapter (6) The Relational Algebra and Relational Calculus Objectives
COMP3017 Advanced Databases
Relational Algebra.
LECTURE 3: Relational Algebra
The Relational Algebra and Relational Calculus
More Relational Algebra
SQL Fundamentals in Three Hours
DCT 2053 DATABASE CONCEPT Chapter 2.2 CONTINUE
Chapter 4 Relational Algebra
Presentation transcript:

Relational Algebra & SQL Lecture 5 Relational Algebra & SQL

Relational Algebra The Relational Algebra is used to define the ways in which relations (tables) can be operated to manipulate their data. It is used as the basis of SQL for relational databases, and illustrates the basic operations required of any DML. This Algebra is composed of Unary operations (involving a single table) and Binary operations (involving multiple tables).

SQL Structured Query Language (SQL) Commands fall into three groups Standardised by ANSI Supported by modern RDBMSs Commands fall into three groups Data Definition Language (DLL) Create tables, etc Data Manipulation Language (DML) Retrieve and modify data Data Control Language Control what users can do – grant and revoke privileges

Unary Operations Selection Projection

Selection The selection or  operation selects rows from a table that satisfy a condition:    < condition > < tablename > Example:  course = ‘CM’ Students Students stud# name course 100 Fred PH stud# name course 200 Dave CM 200 Dave CM 300 Bob CM 300 Bob CM

Projection The projection or  operation selects a list of columns from a table.  < column list > < tablename > Example:  stud#, name Students Students stud# name course stud# name 100 Fred PH 100 Fred 200 Dave CM 200 Dave 300 Bob CM 300 Bob

Selection / Projection Selection and Projection are usually combined:  stud#, name ( course = ‘CM’ Students) Students stud# name course 100 Fred PH stud# name 200 Dave CM 200 Dave 300 Bob CM 300 Bob

Binary Operations Cartesian Product Theta Join Inner Join Natural Join Outer Joins Semi Joins

Cartesian Product Concatenation of every row in the first relation (R) with every row in the second relation (S): R X S

Cartesian Product - Example Students Courses stud# name course course# name 100 Fred PH PH Pharmacy 200 Dave CM CM Computing 300 Bob CM Students X Courses = stud# Students.name course course# Courses.name 100 Fred PH PH Pharmacy 100 Fred PH CM Computing 200 Dave CM PH Pharmacy 200 Dave CM CM Computing 300 Bob CM PH Pharmacy 300 Bob CM CM Computing

Theta Join A Cartesian product with a condition applied: R ⋈ <condition> S

Theta Join - Example Students Courses stud# name course course# name 100 Fred PH PH Pharmacy 200 Dave CM CM Computing 300 Bob CM Students ⋈ stud# = 200 Courses stud# Students.name course course# Courses.name 200 Dave CM PH Pharmacy 200 Dave CM CM Computing

Inner Join (Equijoin) A Theta join where the <condition> is the match (=) of the primary and foreign keys. R ⋈ <R.primary_key = S.foreign_key> S

Inner Join - Example Students Courses stud# name course course# name 100 Fred PH PH Pharmacy 200 Dave CM CM Computing 300 Bob CM Students ⋈ course = course# Courses stud# Students.name course course# Courses.name 100 Fred PH PH Pharmacy 200 Dave CM CM Computing 300 Bob CM CM Computing

Natural Join Inner join produces redundant data (in the previous example: course and course#). To get rid of this duplication:  < stud#, Students.name, course, Courses.name > (Students ⋈ <course = course#> Courses) Or R1= Students ⋈ <course = course#> Courses R2=  < stud#, Students.name, course, Courses.name > R1 The result is called the natural join of Students and Courses

Natural Join - Example Students Courses stud# name course course# name 100 Fred PH PH Pharmacy 200 Dave CM CM Computing Bob CM R1= Students ⋈ <course = course#> Courses R2=  < stud#, Students.name, course, Courses.name > R1 stud# Students.name course Courses.name 100 Fred PH Pharmacy 200 Dave CM Computing 300 Bob CM Computing

Outer Joins Inner join + rows of one table which do not satisfy the <condition>. Left Outer Join: R <R.primary_key = S.foreign_key> S All rows from R are retained and unmatched rows of S are padded with NULL Right Outer Join: R <R.primary_key = S.foreign_key> S All rows from S are retained and unmatched rows of R are

Left Outer Join - Example Students Courses stud# name course course# name 100 Fred PH PH Pharmacy 200 Dave CM CM Computing 400 Peter EN CH Chemistry Students <course = course#> Courses stud# Students.name course course# Courses.name 100 Fred PH PH Pharmacy 200 Dave CM CM Computing Peter EN NULL NULL

Right Outer Join - Example Students Courses stud# name course course# name 100 Fred PH PH Pharmacy 200 Dave CM CM Computing 400 Peter EN CH Chemistry Students <course = course#> Courses stud# Students.name course course# Courses.name 100 Fred PH PH Pharmacy 200 Dave CM CM Computing NULL NULL NULL CH Chemistry

Combination of Unary and Join Operations Students Courses stud# name address course course# name 100 Fred Aberdeen PH PH Pharmacy 200 Dave Dundee CM CM Computing 300 Bob Aberdeen CM Show the names of students (from Aberdeen) and the names of their courses R1= Students ⋈ <course=course#> Courses R2=  <address=“Aberdeen”> R1 R3=  <Students.name, Course.name> R2 Students.name Courses.name Fred Pharmacy Bob Computing

Union Intersection Difference Set Operations Union Intersection Difference

Union Takes the set of rows in each table and combines them, eliminating duplicates Participating relations must be compatible, ie have the same number of columns, and the same column names, domains, and data types R S R  S A B a2 b2 a3 b3 A B a1 b1 a2 b2 a3 b3 A B a1 b1 a2 b2

Intersection Takes the set of rows that are common to each relation Participating relations must be compatible R S R  S A B a1 b1 a2 b2 A B a2 b2 a3 b3 A B a2 b2

Difference Takes the set of rows in the first relation but not the second Participating relations must be compatible R S R - S A B a1 b1 a2 b2 A B a2 b2 a3 b3 A B a1 b1

Exercise (May 2004 Exam) Employee WorkLoad Project empid name empid* projid* duration projid name E100 Fred E100 P001 17 P001 DB E200 Dave E200 P001 12 P002 Access E300 Bob E300 P002 15 P003 SQL E400 Peter Determine the outcome of the following operations: A natural join between Employee and WorkLoad A left outer join between Employee and WorkLoad A right outer join between WorkLoad and Project

Relational Algebra Operations written in SQL

Unary Operations Selection Projection  course = ‘Computing’ Students In SQL: Select * From Students Where course = ‘Computing’; Projection  stud#, name Students In SQL: Select stud#, name From Students; Selection & Projection  stud#, name ( course = ‘Computing’ Students) In SQL: Select stud#, name From students Where course = ‘Computing’;

Binary Operations/Joins Cartesian Product: Students X Courses In SQL: Select * From Students, Courses; Theta Join: Students ⋈ <stud# =200> Courses In SQL: Select * From Students, Courses Where stud# = 200;

Binary Operations/Joins Inner Join (Equijoin): Students ⋈ <course=course#> Courses In SQL: Select * From Students, Courses Where course=course#; Natural Join: R1= Students ⋈ <course = course#> Courses R2=  < stud#, Students.name, course, Courses.name > R1 In SQL: Select stud#, Students.name, course, Courses.name From Students, Courses Where course=course#;

Outer Joins Left Outer Join Students <course = course#> Courses In SQL: Select * From Students, Courses Where course = course#(+) Right Outer Join Students <course = course#> Courses In SQL: Select * From Students, Courses Where course(+) = course#

Combination of Unary and Join Operations R1= Students ⋈ <course=course#> Courses R2=  <address=“Aberdeen”> R1 R3=  <Students.name, Course.name> R2 In SQL: Select Students.name, Courses.name From Students, Courses Where course=course# AND address=“Aberdeen”;

Set Operations Union: R  S Intersection: R  S In SQL: In SQL: Select * From R Union Select * From S; Intersection: R  S In SQL: Select * From R Intersect Select * From S; Difference: R - S In SQL: Select * From R Minus Select * From S;

SQL Operators Between, In, Like, Not

SQL Operators SELECT * FROM Book WHERE catno BETWEEN 200 AND 400; FROM Product WHERE prod_desc BETWEEN ‘C’ AND ‘S’; WHERE catno NOT BETWEEN 200 AND 400;

SQL Operators SELECT Catno FROM Loan WHERE Date-Returned IS NULL; WHERE Date-Returned IS NOT NULL;

SQL Operators SELECT Name FROM Member WHERE memno IN (100, 200, 300, 400); WHERE memno NOT IN (100, 200, 300, 400);

SQL Operators SELECT Name FROM Member WHERE address NOT LIKE ‘%Aberdeen%’; WHERE Name LIKE ‘_ES%’; Note: In MS Access, use * and # instead of % and _

Selecting Distinct Values Student stud# name address 100 Fred Aberdeen 200 Dave Dundee 300 Bob Aberdeen SELECT Distinct address FROM Student; address Aberdeen Dundee

Exercise Employee(empid, name) Project(projid, name) WorkLoad(empid*, projid*, duration) List the names of employees working on project name ‘Databases’.