Relations and Relational Algebra Database Systems Lecture 2 Natasha Alechina www.cs.nott.ac.uk/~nza/G51DBS.

Slides:



Advertisements
Similar presentations
Foundations of Relational Implementation (2) IS 240 – Database Management Lecture #14 – Prof. M. E. Kabay, PhD, CISSP Norwich University
Advertisements

Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 6 The Relational Algebra.
พีชคณิตแบบสัมพันธ์ (Relational Algebra) บทที่ 3 อ. ดร. ชุรี เตชะวุฒิ CS (204)321 ระบบฐานข้อมูล 1 (Database System I)
Chapter 3 : Relational Model
CSC271 Database Systems Lecture # 5. Summary: Previous Lecture  Database languages  Functions of a DBMS  DBMS environment  Data models and their categories.
Relational Algebra Indra Budi Fakultas Ilmu Komputer UI 2 n Basic Relational Operations: l Unary Operations  SELECT   PROJECT 
Discrete Mathematics I Lectures Chapter 6
Basic Structures: Sets, Functions, Sequences, Sums, and Matrices
Basic Structures: Sets, Functions, Sequences, Sums, and Matrices
Relational Algebra Ch. 7.4 – 7.6 John Ortiz. Lecture 4Relational Algebra2 Relational Query Languages  Query languages: allow manipulation and retrieval.
Database Systems Chapter 6 ITM Relational Algebra The basic set of operations for the relational model is the relational algebra. –enable the specification.
CS 454 Theory of Computation Sonoma State University, Fall 2011 Instructor: B. (Ravi) Ravikumar Office: 116 I Darwin Hall Original slides by Vahid and.
CS5371 Theory of Computation Lecture 1: Mathematics Review I (Basic Terminology)
1 Pertemuan 04 MODEL RELASIONAL Matakuliah: >/ > Tahun: > Versi: >
Chapter 11.1 and 11.2 Data Manipulation: Relational Algebra and SQL Brian Cobarrubia Introduction to Database Management Systems October 4, 2007.
Database Systems Lecture 5 Natasha Alechina
Security and Integrity
Lecture 2 The Relational Model. Objectives Terminology of relational model. How tables are used to represent data. Connection between mathematical relations.
Chapter 4 The Relational Model Pearson Education © 2014.
Relational Model Session 6 Course Name: Database System Year : 2012.
Chapter 4 The Relational Model.
Missing Information Database Systems Lecture 10 Natasha Alechina.
A Brief Summary for Exam 1 Subject Topics Propositional Logic (sections 1.1, 1.2) –Propositions Statement, Truth value, Proposition, Propositional symbol,
The Relational Model Database Systems Lecture 3 Natasha Alechina.
CS 3630 Database Design and Implementation. 2 Mathematical Relation A mathematical relation is a subset of a Cartesian Product. A1  A2  A3  …  An.
DBSQL 3-1 Copyright © Genetic Computer School 2009 Chapter 3 Relational Database Model.
Chapter 3 The Relational Model. 2 Chapter 3 - Objectives u Terminology of relational model. u How tables are used to represent data. u Connection between.
Physical DB Issues, Indexes, Query Optimisation Database Systems Lecture 13 Natasha Alechina.
Lecture 05 Structured Query Language. 2 Father of Relational Model Edgar F. Codd ( ) PhD from U. of Michigan, Ann Arbor Received Turing Award.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 6 The Relational Algebra.
ICOM 6005 – Database Management Systems Design Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 2 – Relational Model.
CSE314 Database Systems The Relational Algebra and Relational Calculus Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson Ed Slide Set.
1 The Relational Database Model. 2 Learning Objectives Terminology of relational model. How tables are used to represent data. Connection between mathematical.
Lecture 15: Relational Algebra
1 Mathematical Relation A mathematical relation is a subset of a Cartesian Product. A1  A2  A3  …  An = {(x1, x2, x3, …, xn): xi  Ai} R  A1  A2.
Relational Algebra A presentation for CS 457 By Dawn Haddan.
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.
CS 103 Discrete Structures Lecture 10 Basic Structures: Sets (1)
The Relational Model Pertemuan 03 Matakuliah: M0564 /Pengantar Sistem Basis Data Tahun : 2008.
Relational Algebra (Chapter 7)
Relational Algebra  Souhad M. Daraghma. Relational Query Languages Query languages: Allow manipulation and retrieval of data from a database. Relational.
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.
Chapter 6 The Relational Algebra Copyright © 2004 Ramez Elmasri and Shamkant Navathe.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Database Management Systems Chapter 4 Relational Algebra.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 Part a The Relational Algebra and Relational Calculus Hours 1,2.
Advanced Relational Algebra & SQL (Part1 )
Mathematical Preliminaries
Copyright © Cengage Learning. All rights reserved. CHAPTER 8 RELATIONS.
1 CS 430 Database Theory Winter 2005 Lecture 5: Relational Algebra.
Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 340 Introduction to Database Systems.
The Relational Model © Pearson Education Limited 1995, 2005 Bayu Adhi Tama, M.T.I.
CS 3630 Database Design and Implementation. 2 Mathematical Relation A mathematical relation is a subset of a Cartesian Product. A1  A2  A3  …  An.
Chapter 3 The Relational Model. Objectives u Terminology of relational model. u How tables are used to represent data. u Connection between mathematical.
Chapter 2 1. Chapter Summary Sets (This Slide) The Language of Sets - Sec 2.1 – Lecture 8 Set Operations and Set Identities - Sec 2.2 – Lecture 9 Functions.
Relational Algebra COMP3211 Advanced Databases Nicholas Gibbins
Database Systems Chapter 6
Chapter (6) The Relational Algebra and Relational Calculus Objectives
COMP3017 Advanced Databases
Chapter # 6 The Relational Algebra and Calculus
Relational Algebra - Part 1
Relational Math CSC 240 (Blum).
Discrete Structures for Computer Science
Data Base System Lecture 9: Rational Algebra and Relational Calculus
Data Base System Lecture 6: Relational Model
Chapter 4 The Relational Model Pearson Education © 2009.
The Relational Algebra and Relational Calculus
A Brief Summary for Exam 1
Terminology and Symbols
Presentation transcript:

Relations and Relational Algebra Database Systems Lecture 2 Natasha Alechina

In this Lecture The Relational Model Relational data structure Relational data manipulation For more information Connolly and Begg – Chapters and 4 Ullman and Widom – Chapter 3.1, 5.1 Codd’s paper – on

Relations We will use tables to represent relations : Anne Bob Chris

Relations This is a relation between people and addresses Anne Bob Chris

Relations A mathematician would say that it is a set of pairs:,, and. Anne Bob Chris

Relations Each value in the first column is a name, each value in the second column is an address. In general, each column has a domain – a set from which all possible values can come. Anne Bob Chris

Relations A mathematical relation is a set of tuples: sequences of values. Each tuple corresponds to a row in a table:

Relations: terminology Degree of a relation: how long the tuples are, or how many columns the table has In the first example (name, ) degree of the relation is 2 In the second example (name, ,telephone) degree of the relation is 3 Often relations of degree 2 are called binary, relations of degree 3 are called ternary etc. Cardinality of the relation: how many different tuples are there, or how many different rows the table has.

Relations: mathematical definition Mathematical definition of a relation R of degree n, where values come from domains A 1, …, A n : R  A 1 A 2  …  A n (relation is a subset of the Cartesian product of domains) Cartesian product: A 1 A 2  …  A n = { : a 1  A 1, a 2  A 2, …, a n  A n }

Relational model: data manipulation Data is represented as relations. Manipulation of data (query and update operations) corresponds to operations on relations Relational algebra describes those operations. They take relations as arguments and produce new relations. Think of numbers and corresponding operators +,,\, * or booleans and corresponding operators &,|,! (and, or, not). Relational algebra contains two kinds of operators: common set-theoretic ones and operators specific to relations (for example projecting on one of the columns).

Union Standard set-theoretic definition of union: A  B = {x: x  A or x  B} For example, {a,b,c}  {a,d,e} = {a,b,c,d,e} For relations, we want the result to be a relation again: a set R  A 1  …  A n for some n and domains A 1,…,A n. (or, in other words, a proper table, with each column associated with a single domain of values). So we require in order to take a union of relations R and S that R and S have the same number of columns and that corresponding columns have the same domains.

Union-compatible relations Two relations R and S are union- compatible if they have the same number of columns and corresponding columns have the same domains.

Example: not union-compatible (different number of columns) Anne Bob Chris aaa bbb ccc Tom Sam Steve

Example: not union-compatible (different domains for the second column) Anne Bob Chris aaa bbb ccc Tom Sam Steve

Example: union-compatible Anne Bob Chris Tom Sam Steve

Union of two relations Let R and S be two union-compatible relations. Then their union R  S is a relation which contains tuples from both relations: R  S = {x: x  R or x  S}. Note that union is a partial operation on relations: it is only defined for some (compatible) relations, not for all of them. Similar to division for numbers (result of division by 0 is not defined).

Example: shopping lists R S R  S Cheese1.34 Milk0.80 Bread0.60 Eggs1.20 Soap1.00 Cream5.00Cheese1.34 Milk0.80 Bread0.60 Eggs1.20 Soap1.00 Cream5.00 Soap1.00

Difference of two relations Let R and S be two union-compatible relations. Then their difference R  S is a relation which contains tuples which are in R but not in S: R  S = {x: x  R and x  S}. Note that difference is also a partial operation on relations.

Example R S R  S Cheese1.34 Milk0.80 Bread0.60 Eggs1.20 Soap1.00 Cream5.00Cheese1.34 Milk0.80 Bread0.60 Eggs1.20 Soap1.00

Intersection of two relations Let R and S be two union-compatible relations. Then their intersection is a relation R  S which contains tuples which are both in R and S: R  S = {x: x  R and x  S} Note that intersection is also a partial operation on relations.

Example R S R  S Cheese1.34 Milk0.80 Bread0.60 Eggs1.20 Soap1.00 Cream5.00Soap1.00 Soap1.00

Cartesian product Cartesian product is a total operation on relations. Usual set theoretic definition of product: R  S = { : x  R, y  S} Under the standard definition, if  R and  S, then, >  R  S (the result is a pair of tuples).

Extended Cartesian product Extended Cartesian product flattens the result in a 4-element tuple: For the rest of the course, “product” means extended product.

Extended Cartesian product of relations Let R be a relation with column domains {A 1,…,A n } and S a relation with column domains {B 1,…,B m }. Then their extended Cartesian product R  S is a relation R  S = { :  R,  S}

Example Cheese1.34 Milk0.80 Bread0.60 Eggs1.20 Soap1.00 Cream5.00Cheese1.34 Milk0.80 Bread0.60 Eggs1.20 Soap1.00 Cream5.00 Cream5.00 Cream5.00 Cream5.00 Cheese1.34Soap1.00 Milk0.80Soap1.00 Bread0.60Soap1.00 Eggs1.20Soap1.00 Soap1.00Soap1.00 Soap1.00Cream5.00 RS RSRS

Projection Let R be a relation with n columns, and X is a set of column identifiers (at the moment, we will use numbers, but later we will give then names, like “ ”, or “Telephone”). Then projection of R on X is a new relation  X (R) which only has columns from X. For example,  1,2 (R) is a table with only the 1 st and 2 nd columns from R.

Example:  13 ( R) R:R:

Example:  13 ( R)  13 ( R): Anne Bob Chris

Selection Let R be a relation with n columns and  is a property of tuples. Selection from R subject to condition  is defined as follows:   (R) = {  R:  (a 1,…,a n )}

What is a reasonable property? We assume that properties are written using {and, or, not} and expressions of the form col(i)  col(j) (where i,j are column numbers) or col(i)  v, where v is a value from the domain A i.  is a comparator which makes sense when applied to values from i and j columns (=, , maybe also , , ,  if there is a natural order on values).

What is a meaningful comparison We can always at least tell if two values in the same domain are equal or not (database values are finitely represented). In some cases, it makes sense to compare values from different column domains: for example, if both are domains contain strings, or both contain dates. For example, 1975 > 1987 is a meaningful comparison, “Anne” = 1981 is not. We can only use a comparison in selection property if its result is true or false, never undefined.

Example: selection  col(3) < 2002 and col(2) = Nolan (R) InsomniaNolan2002 Magnolia Insomnia Anderson1999 Skjoldbjaerg1997 MementoNolan2000 GattacaNiccol1997 R

Example: selection  col(3) < 2002 and col(2) = Nolan (R) InsomniaNolan2002 Magnolia Insomnia Anderson1999 Skjoldbjaerg1997 Memento Nolan 2000 GattacaNiccol1997

Example: selection  col(3) < 2002 and col(2) = Nolan (R) InsomniaNolan2002 Magnolia Insomnia Anderson1999 Skjoldbjaerg1997 MementoNolan2000 GattacaNiccol1997

Example: selection  col(3) < 2002 and col(2) = Nolan (R) InsomniaNolan2002 Magnolia Insomnia Anderson1999 Skjoldbjaerg1997 MementoNolan2000 GattacaNiccol1997

Example: selection  col(3) < 2002 and col(2) = Nolan (R) InsomniaNolan2002 Magnolia Insomnia Anderson1999 Skjoldbjaerg1997 MementoNolan2000 GattacaNiccol1997

Example: selection  col(3) < 2002 and col(2) = Nolan (R) InsomniaNolan2002 Magnolia Insomnia Anderson1999 Skjoldbjaerg1997 MementoNolan2000 GattacaNiccol1997

Example: selection  col(3) < 2002 and col(2) = Nolan (R) MementoNolan2000

Summary Data is represented as tables Operations on tables: union of two union-compatible tables (tables with the same number of columns and the same domains for corresponding columns) set difference of two union-compatible tables intersection of two union-compatible tables extended Cartesian product of two tables project a table on some of its columns select rows in a table satisfying a property Result of an operation is again a table, so operations can be chained

Example exam question What is the result of  1,3 ( col(2) = col(4) (R  S) ), where R and S are: Anne Bob R Chris Dan S (5 marks)

Other operations Not all SQL queries can be translated into relational algebra operations defined in the lecture. Extended relational algebra includes counting and other additional operations.

Next Lecture The Relational Model Relational data integrity For more information Connolly and Begg chapter 3