SIT772 Database and Information Retrieval WEEK 6. RELATIONAL ALGEBRAS. The foundation of good database design

Similar documents
Chapter # 7 Introduction to Structured Query Language (SQL) Part II

Introduction to SQL. From Rob and Coronel (2004), Database Systems: Design, Implementation, and Management

Database Systems: Design, Implementation, and Management Tenth Edition. Chapter 7 Introduction to Structured Query Language (SQL)

SQL functions fit into two broad categories: Data definition language Data manipulation language

Chapter 7. Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel

Preview. Advanced SQL. In this chapter, you will learn:

Querying Data with Transact SQL

Structured Query Language: Introduction

Database Principles: Fundamentals of Design, Implementation, and Management Ninth Edition Carlos Coronel, Steven Morris, and Peter Rob

INDEX. 1 Basic SQL Statements. 2 Restricting and Sorting Data. 3 Single Row Functions. 4 Displaying data from multiple tables

CSC Web Programming. Introduction to SQL

Chapter 3B Objectives. Relational Set Operators. Relational Set Operators. Relational Algebra Operations

Handout 9 CS-605 Spring 18 Page 1 of 8. Handout 9. SQL Select -- Multi Table Queries. Joins and Nested Subqueries.

THE RELATIONAL DATABASE MODEL

Oracle Syllabus Course code-r10605 SQL

CMP-3440 Database Systems

Database Systems: Design, Implementation, and Management Tenth Edition. Chapter 8 Advanced SQL

1 Writing Basic SQL SELECT Statements 2 Restricting and Sorting Data

Concepts of Database Management Eighth Edition. Chapter 2 The Relational Model 1: Introduction, QBE, and Relational Algebra

Querying Data with Transact-SQL

Oracle Database 11g: SQL and PL/SQL Fundamentals

Relational Algebra. Ron McFadyen ACS

1) Introduction to SQL

Index. Bitmap Heap Scan, 156 Bitmap Index Scan, 156. Rahul Batra 2018 R. Batra, SQL Primer,

DB2 SQL Class Outline

Test Bank for Database Processing Fundamentals Design and Implementation 13th Edition by Kroenke

Introduction to Computer Science and Business

A l Ain University Of Science and Technology

Database Systems: Design, Implementation, and Management Tenth Edition. Chapter 8 Advanced SQL

Full file at

Copyright 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Chapter 6 Outline. Unary Relational Operations: SELECT and

2. What is the difference between UNION and UNION ALL? Write the syntax for each.

Querying Data with Transact-SQL

MTA Database Administrator Fundamentals Course

Relational Model History. COSC 416 NoSQL Databases. Relational Model (Review) Relation Example. Relational Model Definitions. Relational Integrity

Data Manipulation Language (DML)

T-SQL Training: T-SQL for SQL Server for Developers

Unit Assessment Guide

Oracle Database 10g: Introduction to SQL

The Relational Algebra

Teradata SQL Features Overview Version

A subquery is a nested query inserted inside a large query Generally occurs with select, from, where Also known as inner query or inner select,

Querying Data with Transact SQL Microsoft Official Curriculum (MOC 20761)

Relational Databases

SQL Data Query Language

20761 Querying Data with Transact SQL

Table of Contents. PDF created with FinePrint pdffactory Pro trial version

Some Basic Aggregate Functions FUNCTION OUTPUT The number of rows containing non-null values The maximum attribute value encountered in a given column

Querying Microsoft SQL Server 2014

Oracle Database: SQL and PL/SQL Fundamentals NEW

Course Outline and Objectives: Database Programming with SQL

Lecture 06. Fall 2018 Borough of Manhattan Community College

Relational Database Management Systems for Epidemiologists: SQL Part I

Database design process

5. Single-row function

Oracle Database: SQL and PL/SQL Fundamentals Ed 2

2) SQL includes a data definition language, a data manipulation language, and SQL/Persistent stored modules. Answer: TRUE Diff: 2 Page Ref: 36

SQL: Data Querying. B0B36DBS, BD6B36DBS: Database Systems. h p:// Lecture 4

Querying Microsoft SQL Server

20461: Querying Microsoft SQL Server

Relational Algebra and SQL

Sql Server Syllabus. Overview

Mobile MOUSe MTA DATABASE ADMINISTRATOR FUNDAMENTALS ONLINE COURSE OUTLINE

SQL Data Querying and Views

1Z Oracle Database 11g - SQL Fundamentals I Exam Summary Syllabus Questions

Relational Query Languages. Preliminaries. Formal Relational Query Languages. Example Schema, with table contents. Relational Algebra

Exam code: Exam name: Database Fundamentals. Version 16.0

SQL (Structured Query Language)

20461: Querying Microsoft SQL Server 2014 Databases

MANAGING DATA(BASES) USING SQL (NON-PROCEDURAL SQL, X401.9)

Database Management Systems,

AVANTUS TRAINING PTE LTD

[AVNICF-MCSASQL2012]: NICF - Microsoft Certified Solutions Associate (MCSA): SQL Server 2012

Intermediate SQL: Aggregated Data, Joins and Set Operators

4. SQL - the Relational Database Language Standard 4.3 Data Manipulation Language (DML)

Querying Data with Transact-SQL

CS 377 Database Systems

II. Structured Query Language (SQL)

II. Structured Query Language (SQL)

CS317 File and Database Systems

WHAT IS SQL. Database query language, which can also: Define structure of data Modify data Specify security constraints

QUERY PROCESSING & OPTIMIZATION CHAPTER 19 (6/E) CHAPTER 15 (5/E)

Databases - 4. Other relational operations and DDL. How to write RA expressions for dummies

Oracle Database: Introduction to SQL Ed 2

Querying Microsoft SQL Server

ASSIGNMENT NO Computer System with Open Source Operating System. 2. Mysql

Language. f SQL. Larry Rockoff COURSE TECHNOLOGY. Kingdom United States. Course Technology PTR. A part ofcenqaqe Learninq

COURSE OUTLINE MOC 20461: QUERYING MICROSOFT SQL SERVER 2014

12. MS Access Tables, Relationships, and Queries

Querying Microsoft SQL Server (MOC 20461C)

Data about data is database Select correct option: True False Partially True None of the Above

Introduction. Sample Database SQL-92. Sample Data. Sample Data. Chapter 6 Introduction to Structured Query Language (SQL)

Relational Algebra. Note: Slides are posted on the class website, protected by a password written on the board

Structured Query Language Continued. Rose-Hulman Institute of Technology Curt Clifton

SQL Fundamentals. Chapter 3. Class 03: SQL Fundamentals 1

This course is aimed at those who need to extract information from a relational database system.

SQL - Subqueries and. Schema. Chapter 3.4 V4.0. Napier University

Comparison Operators. Selecting Rows with Conditional Restrictions

Chapter 5 Relational Algebra. Nguyen Thi Ai Thao

CS 582 Database Management Systems II

Transcription:

SIT772 Database and Information Retrieval WEEK 6. RELATIONAL ALGEBRAS The foundation of good database design

Outline 1. Relational Algebra 2. Join 3. Updating/ Copy Table or Parts of Rows 4. Views (Virtual Tables) 5. Ordering a Listing 6. Listing Unique Values 7. Aggregate Functions 9. Brief Introduction to SQL Functions 10. Summary WEEK 6 RELATIONAL ALGEBRA 2

Relational Algebra 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 operation: composed of operators and operands Operations Arithmetic Operators (input: values, output: values) +,, /, *, %,. Relational Operators (input: values, output: Boolean) >, <, > =, < =. < >,!= Logical Operators (input: Boolean, output: Boolean) AND, OR, NOT WEEK 6 RELATIONAL ALGEBRA 3

Example of Operations: (mark > 44) (mark > 69) AND (attrate > 80%) (mark >= 80) AND (attrate > 60% OR assignment < 60) (mark < 50) AND (Exam < 20% OR assignment >= 80) Unary operations (involving a single table) Selection Projection Binary operations (involving multiple tables) Cartesian Product Theta Join Inner Join Natural Join Outer Joins WEEK 6 RELATIONAL ALGEBRA 4

Select condition (as in if statements) Yields either all rows or those rows matching a specified criterion Yields a horizontal subset of a table selects rows from a table that satisfy a condition WEEK 6 RELATIONAL ALGEBRA 5

PROJECT L (L is list of attributes) Yields all values for selected attributes Yields a vertical subset of a table selects a list of columns from a table WEEK 6 RELATIONAL ALGEBRA 6

Unary Operations WEEK 6 RELATIONAL ALGEBRA 7

Select command list the vendor and product from 2 tables using the join or link FK. Operator SELECT V_name, P_descrioption FROM Vendor, Product WHERE Vendor.V_code = Product.V_code AND P_QoH <= P_Min; Operand WEEK 6 RELATIONAL ALGEBRA 8

JOIN Real power behind the relational database because JOIN can combine data from two or more tables linked by common attributes Types of JOIN Natural Join : Selects rows that have common values in their common attribute(s) Theta join allows for arbitrary comparison relationships (such as ). Equijoin is a theta join using the equality operator. WEEK 6 RELATIONAL ALGEBRA 9

Join Theta Join: product with a condition applied Inner Join is a Theta join where the <condition> is the match (=) of the primary and foreign keys Natural Join: Inner join produces/gets rid of redundant data. Left Outer Joins: Inner join shows rows in left table which is matching / satisfy the <condition> in the right table. Right Outer Joins: Inner join shows rows in right table which is matching / satisfy the <condition> in the left table. WEEK 6 RELATIONAL ALGEBRA 10

WEEK 6 RELATIONAL ALGEBRA 11

Relational Set Operators Relational algebra Defines a theoretical way of manipulating table contents using relational operators Purpose: Use relational algebra operators on existing tables (relations) to produce new tables that contain required data Operators (recalled in week 3) SELECT, PROJECT, JOIN PRODUCT, UNION, INTERSECT, DIFFERENCE, WEEK 6 RELATIONAL ALGEBRA 12

PRODUCT X Yields all possible pairs of rows from two tables (a.k.a. Cartesian Product) concatenation of every row in the first relation (R) with every row in the second relation (S): R X S WEEK 6 RELATIONAL ALGEBRA 13

Relational Set Operators UNION INTERSECT MINUS Work properly if relations are union-compatible Names of relation attributes must be the same and their data types must be identical Assumptions: we assume that all the tables and attributes used in the following examples have already been defined. WEEK 6 RELATIONAL ALGEBRA 14

UNION Combines all rows from two tables, excluding duplicate rows Tables must be union compatible, i.e., tables share the same number of columns, and their corresponding columns share compatible domains WEEK 6 RELATIONAL ALGEBRA 15

INTERSECT Yields only the rows that appear in both tables Tables must be union-compatible WEEK 6 RELATIONAL ALGEBRA 16

DIFFERENT or MINUS Yields all rows in the 1 st table not found in the 2 nd table, i.e., it subtracts the 2 nd table from the 1st. Tables must be union-compatible WEEK 6 RELATIONAL ALGEBRA 17

Binary Operations WEEK 6 RELATIONAL ALGEBRA 18

Binary Operations WEEK 6 RELATIONAL ALGEBRA 19

Binary Operations WEEK 6 RELATIONAL ALGEBRA 20

Combination of Unary and Join Operations WEEK 6 RELATIONAL ALGEBRA 21

Relational Set Operators WEEK 6 RELATIONAL ALGEBRA 22

UPDATE modify data in a table Syntax: UPDATE tablename SET columnname = expression [, columname = expression] [WHERE conditionlist]; If more than one attribute is to be updated in row, separate corrections with commas UPDATE PRODUCT SET P_INDATE = 18 Jan 2006, P_PRICE = 18.99 WHERE P_CODE = 13 Q2/P2 ; Updating Table Rows UPDATE Product SET P_SALECODE = 1 WHERE P_CODE = 2232/QWE OR P_CODE = 2232/QTY ; UPDATE Product SET P_SALECODE = 1 WHERE P_INDATE >= 16 Jan 2006 AND P_INDATE <= 10 Feb 2006 ; WEEK 6 RELATIONAL ALGEBRA 23

Copying Parts of Tables INSERT Insert multiple rows from another table Uses SELECT subquery Query that is embedded (or nested) inside another query Executed first Syntax: INSERT INTO tablename ( columnlist ) SELECT columnlist FROM tablename; The values returned by the SELECT subquery should match the attributes and data types of the table in the INSERT statement. WEEK 6 RELATIONAL ALGEBRA 24

Copying Parts of Tables Example 1 First create the PART table structure which is the same as PRODUCT. CREATE TABLE PART ( PART_CODE CHAR(8) NOT NULL UNIQUE, PART_DESCRIP CHAR(35), PART_PRICEDECIMAL(8,2), V_CODE INTEGER, PRIMARY KEY (PART_CODE)); Next add rows to new PART table using rows from the PRODUCT table INSERT INTO PART (PART_CODE, PART_DESCRIPT, PART_PRICE, V_CODE) SELECT P_CODE, P_DESCRIPT, P_PRICE, V_CODE FROM PRODUCT; WEEK 6 RELATIONAL ALGEBRA 25

SELECT * FROM PART; WEEK 6 RELATIONAL ALGEBRA 26

Copying Parts of Tables Example 2 Create the table, retrieve and populate using one command. CREATE TABLE PART AS SELECT P_CODE, P_DESCRIPT, P_PRICE, V_CODE FROM PRODUCT; Virtual Tables: Creating a View A view is a virtual table based on a SELECT query Can contain columns, computed columns, aliases, and aggregate functions from one or more tables Base tables are tables on which a view is based Create a view by using CREATE VIEW Virtual Tables: Creating a View (updateable) WEEK 6 RELATIONAL ALGEBRA 27

Ordering a Listing Example 1 SELECT P_CODE, P_DESCRIPT, P_INDATE, P_PRICE FROM PRODUCT ORDER BY P_PRICE ; WEEK 6 RELATIONAL ALGEBRA 28

Ordering a Listing Example 2 SELECT EMP_LNAME, EMP_FNAME, EMP_INITIAL, EMP_AREACODE, EMP_PHONE FROM EMPLOYEE ORDER BY EMP_LNAME, EMP_FNAME, EMP_INITIAL ; WEEK 6 RELATIONAL ALGEBRA 29

Ordering a Listing Example 3 SELECT P_DESCRIP, V_CODE, P_INDATE, P_PRICE FROM PRODUCT WHERE P_INDATE < 21-Jan-2006 AND P_PRICE <= 50 ORDER BY V_CODE, P_PRICE DESC ; WEEK 6 RELATIONAL ALGEBRA 30

Listing Unique Values Example SELECT DISTINCT V_CODE FROM PRODUCT; Aggregate Functions WEEK 6 RELATIONAL ALGEBRA 31

Aggregate Functions COUNT WEEK 6 RELATIONAL ALGEBRA 32

Aggregate Functions MAX and MIN WEEK 6 RELATIONAL ALGEBRA 33

Aggregate Functions MAX and MIN Aggregate Functions Question: Can we use the following query to select products whose price equals the highest price of all the products? SELECT P_CODE, P_DESCRIPT, P_PRICE FROM PRODUCT WHERE P_PRICE = MAX(P_PRICE); Answer: NO WEEK 6 RELATIONAL ALGEBRA 34

Aggregate Functions SUM Total values of all items in the PRODUCT table. WEEK 6 RELATIONAL ALGEBRA 35

Aggregate Functions AVG WEEK 6 RELATIONAL ALGEBRA 36

Grouping Data Syntax: SELECT columnlist FROM tablelist [WHERE conditionlist] [GROUP BY columnlist] [HAVING conditionlist] [ORDER BY columnlist [ASC DESC]] The GROUP BY clause is generally used when you have attributes combined with aggregation functions (e.g. COUNT, MAX, MIN, SUM, AVG) in the SELECT statement. WEEK 6 RELATIONAL ALGEBRA 37

GROUP BY Clause Output Examples P_SALECODE nulls included P_SALECODE nulls included WEEK 6 RELATIONAL ALGEBRA 38

Incorrect and Correct Use of the GROUP BY Clause WEEK 6 RELATIONAL ALGEBRA 39

An Application of the HAVING Clause WHERE vs. HAVING HAVING always goes along with GROUP BY Good for evaluating conditions that applies to a group of records Aggregate functions are involved WHERE operates on individual records Aggregates can be used too WEEK 6 RELATIONAL ALGEBRA 40

WEEK 6 RELATIONAL ALGEBRA 41

Summary 1. Relational Algebra 2. Join 3. Updating/ Copy Table or Parts of Rows 4. Views (Virtual Tables) 5. Ordering a Listing 6. Listing Unique Values 7. Aggregate Functions 9. Brief Introduction to SQL Functions 10. Summary WEEK 6 RELATIONAL ALGEBRA 42