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