Relational Model, Relational Algebra, and SQL

Similar documents
Relational Algebra and SQL

Relational Model and Relational Algebra A Short Review Class Notes - CS582-01

Relational Algebra. Procedural language Six basic operators

Chapter 2: Intro to Relational Model

The Extended Algebra. Duplicate Elimination. Sorting. Example: Duplicate Elimination

RELATIONAL DATA MODEL: Relational Algebra

Chapter 2: Intro to Relational Model

Chapter 6: Formal Relational Query Languages

Relational Databases

Chapter 2: Relational Model

Midterm Review. Winter Lecture 13

Database System Concepts

Relational Algebra. Algebra of Bags

Fundamentals of Database Systems

Relational Algebra and SQL. Basic Operations Algebra of Bags

Relational Model and Relational Algebra. Rose-Hulman Institute of Technology Curt Clifton

Relational Database: The Relational Data Model; Operations on Database Relations

CMP-3440 Database Systems

Relational Model: History

Chapter 3: Relational Model

CS 377 Database Systems

CS34800 Information Systems. The Relational Model Prof. Walid Aref 29 August, 2016

CS 317/387. A Relation is a Table. Schemas. Towards SQL - Relational Algebra. name manf Winterbrew Pete s Bud Lite Anheuser-Busch Beers

Relational Algebra BASIC OPERATIONS DATABASE SYSTEMS AND CONCEPTS, CSCI 3030U, UOIT, COURSE INSTRUCTOR: JAREK SZLICHTA

RELATIONAL ALGEBRA. CS 564- Fall ACKs: Dan Suciu, Jignesh Patel, AnHai Doan

Databases Relational algebra Lectures for mathematics students

Ian Kenny. November 28, 2017

Relational Algebra. Relational Algebra Overview. Relational Algebra Overview. Unary Relational Operations 8/19/2014. Relational Algebra Overview

Web Science & Technologies University of Koblenz Landau, Germany. Relational Data Model

2.2.2.Relational Database concept

Relational Algebra. Relational Algebra. 7/4/2017 Md. Golam Moazzam, Dept. of CSE, JU

Introduction to Query Processing and Query Optimization Techniques. Copyright 2011 Ramez Elmasri and Shamkant Navathe

RELATIONAL ALGEBRA II. CS121: Relational Databases Fall 2017 Lecture 3

WEEK 3. EE562 Slides and Modified Slides from Database Management Systems, R.Ramakrishnan 1

QUIZ 1 REVIEW SESSION DATABASE MANAGEMENT SYSTEMS

Database System Concepts, 5 th Ed.! Silberschatz, Korth and Sudarshan See for conditions on re-use "

Textbook: Chapter 6! CS425 Fall 2013 Boris Glavic! Chapter 3: Formal Relational Query. Relational Algebra! Select Operation Example! Select Operation!

Chapter 2 The relational Model of data. Relational algebra

Chapter 6 The Relational Algebra and Calculus

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

Relational Algebra. Relational Query Languages

Chapter 14: Query Optimization

Informationslogistik Unit 4: The Relational Algebra

CAS CS 460/660 Introduction to Database Systems. Relational Algebra 1.1

The Relational Model and Relational Algebra

Database Systems SQL SL03

Algorithms for Query Processing and Optimization. 0. Introduction to Query Processing (1)

Relational model continued. Understanding how to use the relational model. Summary of board example: with Copies as weak entity

Chapter 14 Query Optimization

Chapter 14 Query Optimization

Chapter 14 Query Optimization

RELATIONAL ALGEBRA. CS121: Relational Databases Fall 2017 Lecture 2

1 Relational Data Model

Database Systems SQL SL03

Selection and Projection

Contents Contents Introduction Basic Steps in Query Processing Introduction Transformation of Relational Expressions...

SQL: Data Manipulation Language. csc343, Introduction to Databases Diane Horton Winter 2017

Relational Algebra. B term 2004: lecture 10, 11

SQL. Lecture 4 SQL. Basic Structure. The select Clause. The select Clause (Cont.) The select Clause (Cont.) Basic Structure.

Chapter 3. Algorithms for Query Processing and Optimization

Relational Algebra Part I. CS 377: Database Systems

Relational Algebra. ICOM 5016 Database Systems. Roadmap. R.A. Operators. Selection. Example: Selection. Relational Algebra. Fundamental Property

Chapter 4: SQL. Basic Structure

Relational Query Languages: Relational Algebra. Juliana Freire

3. Relational Data Model 3.5 The Tuple Relational Calculus

ARTICLE RELATIONAL ALGEBRA

Introduction to Data Management CSE 344. Lectures 8: Relational Algebra

Relational Query Languages. Relational Algebra. Preliminaries. Formal Relational Query Languages. Relational Algebra: 5 Basic Operations

Lecture Query evaluation. Combining operators. Logical query optimization. By Marina Barsky Winter 2016, University of Toronto

Relational Model History. COSC 304 Introduction to Database Systems. Relational Model and Algebra. Relational Model Definitions.

Introduction to Data Management CSE 344. Lectures 8: Relational Algebra

SQL: csc343, Introduction to Databases Renée J. Miller and Fatemeh Nargesian and Sina Sina Meraji. Winter 2018

Data Structure: Relational Model

CS 4604: Introduc0on to Database Management Systems. B. Aditya Prakash Lecture #3: SQL and Rela2onal Algebra- - - Part 1

UNIT- II (Relational Data Model & Introduction to SQL)

CS3DB3/SE4DB3/SE6DB3 TUTORIAL

CS121 MIDTERM REVIEW. CS121: Relational Databases Fall 2017 Lecture 13

Database Management Systems. Chapter 4. Relational Algebra. Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1

v Conceptual Design: ER model v Logical Design: ER to relational model v Querying and manipulating data

Relational Algebra. [R&G] Chapter 4, Part A CS4320 1

II. Structured Query Language (SQL)

Database Management Systems Paper Solution

Database Systems. Basics of the Relational Data Model

Modeling access. Relational query languages. Queries. Selection σ P (R) Relational model: Relational algebra

Database Technology Introduction. Heiko Paulheim

E-R diagrams and database schemas. Functional dependencies. Definition (tuple, attribute, value). A tuple has the form

Chapter 6: RELATIONAL DATA MODEL AND RELATIONAL ALGEBRA

Chapter 6 - Part II The Relational Algebra and Calculus

Database Usage (and Construction)

Darshan Institute of Engineering & Technology Relational Model

Chapter 13: Query Optimization. Chapter 13: Query Optimization

Chapter 3: SQL. Database System Concepts, 5th Ed. Silberschatz, Korth and Sudarshan See for conditions on re-use

Chapter 6 Part I The Relational Algebra and Calculus

THE RELATIONAL DATABASE MODEL

Set theory is a branch of mathematics that studies sets. Sets are a collection of objects.

Chapter 3: SQL. Chapter 3: SQL

CS 582 Database Management Systems II

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

The Relational Algebra

Silberschatz, Korth and Sudarshan See for conditions on re-use

Transcription:

Relational Model, Relational Algebra, and SQL August 29, 2007 1 Relational Model Data model. constraints. Set of conceptual tools for describing of data, data semantics, data relationships, and data integrity Relational Model. A data model in which relations are used for all purposes! Relation. A subset of D 1 D 2... D n where each D i is a domain. Each relation is a set of n-tuples (a 1,..., a n ) where a i D i. We say that a relation, which is a subset of D 1 D 2... D n, has n-attributes. We associate a name to each attribute of a relation. The domain of an attribute is the set of possible values that it can take. For n attributes A 1,..., A n, R = (A 1,..., A n ) is called a relation schema. A schema denotes a set of relations whose tuples are of the form (a 1,..., a n ) where a i belongs to the domain of A i. Each of these relations is called a relation on the schema R. For a tuple t = (v 1,..., v n ) that belongs to the instance r(r), t[a j ] denotes v j and t[s], where S {A 1,..., A n }, denotes a tuple created by the values of attributes belonging to S r(r) denotes a relation instance of R which is a relation on the schema Key: (also called superkey) A set of attributes K of a schema R is a key if the values for K (or more precisely, the values for attributes in K) uniquely determined a tuple of each possible relation r(r); The definition of key also implies that no instance r(r) could have two tuples whose values for K are identical. Candidate key: a key which is minimal with respect to the set inclusion operator. Primary key: a designated candidate key. Foreign key: a set of attributes of a relation that correspond to the primary key of another relation. 2 Database query language Used for retrieving information stored in a database. Most important relational query language is SQL. SQL is declarative (only specifies what should be in the answer and not how to compute it). See the difference between SQL and C, C++? Relational Algebra. An important form of query language for the relational model. Provided the necessary background for designing complex queries. Consists of a small number of basic operators. A query is simply an expression in relational algebra. 1

2.1 Basic operators of the relational algebra They are divided into the following classes: 1. Set operators: union ( ), intersection ( ), and set difference ( ) 2. Operators that remove part of the relation: projection or selection (π or σ) 3. Operators that combine part of the relation: Cartesian product or joint ( or ) 4. Renaming operators: rename the schema of the relation (ρ) Set operators Union ( ): r s represents a new relation whose tuples are either tuples belonging to r, to s, or both. Set difference ( ): r s represents a new relation whose tuples are tuples belonging to r but not to s. Intersection ( ) : r s represents a new relation whose tuples are tuples belonging to r and s. Note: Set operators only work on relations with the same schema. Example 1 Let r be the following relation instance: and s be the following relation instance: 4 1 3 4 4 3 4 Then: r s is r s is 4 1 3 4 4 3 4 and r s is 2

NOTE: If the schema of s changes, say s(a1, B1, C1) then all of the three operations (,, \) will yield the empty relation. Operators that remove part of the relation Projection (π) This operation removes some columns from a relation. We write π A1,...,A k (r) and the result is a new relation that has only the columns A 1,..., A k of r and whose schema is the set of attributes {A 1,..., A k } of the relation r. For the relation instance r in Example 1, π A,B (r) is the relation instance: A B 1 2 2 1 3 2 4 1 Selection (σ) The operation selects some tuples from the current relation and defines a new relation. σ C (r) represents a new relation that has tuples belonging to r and each of them satisfying the conditional expression (or condition) C. C is a conditional expression constructed from attributes of r or constants and/or basic logical operations such as AND, OR, and NOT. For example, A < B + C would be a valid conditional expression with respect to the relation r in Example 1; another example is (A < B + C) OR B = C. For the relation r in Example 1, σ A<B+C (r) is the following relation instance: Operators that combine relations Cartesian Product ( ) Given two relations r and s. The Cartesian product r s is the set of pairs that can be formed by choosing the first element of the pair to be any element of r and the second an element of s. Example 2 Let r be the following relation instance: 3

and s be the following relation instance: Then: r s is E D 2 1 E D 2 1 2 1 2 1 2 1 NOTE: When r and s share some attributes, the name of the shared attributes need to be redefined. Two common ways: (i) attaching the relation name to the attributes of the result; (ii) using the renaming operator. Suppose s is given by Then: r s is A D 2 1 r. s.a D 2 1 2 1 2 1 2 1 Alternatively, we can write r s[a, B, C, D, E] and the result is D E 2 1 2 1 2 1 2 1 2.2 Additional Relational Algebra Operations Join (also called theta join) The join operator combines two relations r, and s but using only tuples that are matching in some way. The general join operator has the form: r join cond s 4

where join cond is an expression of the form r.a 1 op 1 s.b 1 AND r.a 2 op 2 s.b 2... AND r.a n op n s.b n op i s are either =,, >,, <,, r.a i s are attributes of r, and s.b j s are attributes of s. (a conjunction of comparisons between attributes of r and s) Example 3 Let r be the following relation instance: and s be the following relation instance: Then: r r.b=s.b s is B D 3 1 s.b D The first tuple of r r.b=s.b s is combined from the 1 st tuple of r and the 1 st tuple of s. The second tuple of r r.b=s.b s is combined from the 3 st tuple of r and the 1 st tuple of s. For the second tuple of s, the value of B is 3 and none of the tuples in r has B = 3. So, no new tuple for r r.b=s.b s can be formed using the second tuple of s. Special cases: Equi-join is a join between r and s whose join condition is a conjunction of equality comparisons; natural join is an equi-join whose join condition is the conjunction of all equality comparisons between shared attributes of r and s. Often, a join without join-condition is understood as a natural join. Join is a derived operator : r C s is given by r C s = σ C (r s). Renaming (ρ): ρ s(a1,...,a k )(r) creates a new relation, named s, and attributes {A 1,..., A k } with all the tuples of r. Example 4 Let r be the following relation instance: Then, ρ T (M,N,P ) (r) is a new relation T with the following instance NOTE: ρ s (r) only changes the name of the relation. M N P 5

Division operator For r with the set of attributes {A 1,..., A n, B 1,..., B m } and s with the set of attributes {B 1,..., B m } (or r(r) where R = (A 1,..., A n, B 1,..., B m ) and s(s) where S = (B 1,..., B m )) r : s is a relation over the set of attributes {A 1,..., A n } (or R S) with the set of tuples t where { t } s r. This is equivalent to say that t belongs to r : s 1. t is in Π r s 2. for each t s in s there exists a tuple t r in r such that (a) t r [S] = t s [S] and (b) t r [R S] = t Example 5 Let r be the following relation instance: and s be the following relation instance: Then: r : s is 1 3 1 B C 3 1 A 1 Combining Operations to Form Queries. The operators of relational algebra allows us to form expressions of arbitrary complexity by applying operators to either a given relation or to the relations that are the result of applying operators to relations. Example like the theta-join operator, or something like π A,B (σ C>D (r s)) etc. Assignment. Uses for storing temporary results. r exp where exp is a relational expression indicates that r is the relation resulting from exp. For example, we write r Π A,B (r) s Π A,B (s) result = r s to represent the complex expression Π A,B (r) Π A,B (s) 2.3 Extended Relational Operations This includes the generalized projection, aggregate functions, and outer joints. 6

Outer-, Left Outer-, and Right Outer-join The join operator might remove some tuples of the relations involved in the join. To retain the dangling tuples (for different purposes), outer-, left outer-, or right outer-join can be used. They are defined as follows: Outer join r outer C s consists of (i) (the tuples in) r C s, (ii) the tuples in r that do not join with any tuple in s (NULL valued padded to the part of s), and (iii) the tuples in s that do not join with any tuple in r (NULL valued padded to the part of r). Left outer join r left C s consists of tuples in (i) and (iii) (of outer join) Right outer join r right C s consists of tuples in (i) and (ii) (of outer join) Example 6 Let r be the following relation instance: and s be the following relation instance: Then: r r.b=s.b s is B D 3 1 s.b D r outer r.b=s.b s is s.b D NULL NULL NULL NULL NULL NULL NULL 3 1 r right r.b=s.b s is s.b D NULL NULL NULL NULL r left r.b=s.b s is s.b D NULL NULL NULL 3 1 7

Generalized Projection. Π F1,...,F n (E) where E is a relational algebra expression and F i is either an arithmetic expression involving the attributes and constants in the schema of E. The result is a relation with the schema of n attributes. t = (v 1,..., v n ) is a tuple belonging to the result if there exists a tuple e in E such that v i is the value of F i with respect to e. Example 7 Let r be the following relation instance: and Π A+B,B C (r) is the following relation instance: A + B B - C 3-1 3 0 5 0 5 3 We can also use Π F1 as A 1,...,F n as A n (E) to specify the generalized projection. The attributes of the result relation will be A 1,..., A n instead of F 1,..., F n. Aggregates. For a sequence of numbers S (also a multiset of numbers, a set in which elements can be repeated), sum, avg, min, and max returns the sum, average, min, or max. An aggregate operation G has the following form where E is a relational algebra expression G 1,..., G n is a list of attributes F i is an aggregate function A i is attribute name G 1,G 2,...,G n G F1 (A 1 ),F 2 (A 2 ),...,F m (A m )(E) The relation resulting from the above aggregate operation is computed in the following steps r E Divide r into different relations r 1,..., r k where 1. for each pair of tuples t and t in r i t ri [G 1, G 2,..., G n ] = t r i [G 1, G 2,..., G n ] 2. for each pair of tuples t and t where t is in r i and t is in r j t ri [G 1, G 2,..., G n ] t r j [G 1, G 2,..., G n ] For each relation r i, compute the value F j (A j ) and create a tuple (g 1,..., g n, v F1 (A 1 )(r i ),..., v Fm (A m )(r i )) for the resulting relation. Example 8 Let r be the following relation instance: 1 1 3 8

and G sum(a) (r) is the following relation instance: sum(a) 11 BG sum(a) (r) is the following relation instance: B sum(a) 2 5 1 6 We can also use as... to specify the new attribute name in aggregate operations. For example, B G sum(a) as sa (r) is the following relation instance (r refers to the relation in the previous example): B sa 2 5 1 6 9