Information Systems. Relational Databases. Nikolaj Popov

Similar documents
Database Systems Relational Model. A.R. Hurson 323 CS Building

Unit 3 The Relational Model

A Sample Solution to the Midterm Test

UFCEKG 20 2 : Data, Schemas and Applications

Supplier-Parts-DB SNUM SNAME STATUS CITY S1 Smith 20 London S2 Jones 10 Paris S3 Blake 30 Paris S4 Clark 20 London S5 Adams 30 Athens

Lecture 15: Database Normalization

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

Chapter 8 INTEGRITY 1

Database Redesign. 1. Additional SQL Statements 3 1) Correlated Sub-Query 3 2) EXISTS 4 3) NOT EXISTS 7 4) double NOT EXISTS (FOR ALL) 9.

CS5300 Database Systems

MySQL Introduction. Practical Course Integrative Bioinformatics. March 7, 2012

A l Ain University Of Science and Technology

Relational Data Structure and Concepts. Structured Query Language (Part 1) The Entity Integrity Rules. Relational Data Structure and Concepts

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

CS 377 Database Systems

Relational Model. Rab Nawaz Jadoon DCS. Assistant Professor. Department of Computer Science. COMSATS IIT, Abbottabad Pakistan

Information Systems. Database System Architecture. Relational Databases. Nikolaj Popov

CSC Discrete Math I, Spring Sets

3. Relational Data Model 3.5 The Tuple Relational Calculus

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

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

3 Data Models and Data Sublanguages

The Relational Model and Normalization

Relational Databases

2.1 Sets 2.2 Set Operations

LECTURE 8: SETS. Software Engineering Mike Wooldridge

Faloutsos - Pavlo CMU SCS /615

Overview. Carnegie Mellon Univ. School of Computer Science /615 - DB Applications. Concepts - reminder. History

Database Applications (15-415)

Ian Kenny. November 28, 2017

T h e M u l t i r e l a t i o n a l A p p r o a c h

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

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

CPS 510 Data Base I. There are 3 forms of database descriptions the ANSI/SPARK, 1975 and so on

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

Relational Model: History

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

Introduction The SELECT statement: basics Nested queries Set operators Update commands Table management

1 Relational Data Model

Relational terminology. Databases - Sets & Relations. Sets. Membership

9/19/12. Why Study Discrete Math? What is discrete? Sets (Rosen, Chapter 2) can be described by discrete math TOPICS

Further GroupBy & Extend Operations

Relational Query Languages: Relational Algebra. Juliana Freire

Chapter 4. The Relational Model

Databases - Relational Algebra. (GF Royle, N Spadaccini ) Databases - Relational Algebra 1 / 24

This lecture. Projection. Relational Algebra. Suppose we have a relation

Chapter 6 Part I The Relational Algebra and Calculus

Relational Algebra. Lecture 4A Kathleen Durant Northeastern University

CS5300 Database Systems

RELATIONAL DATA MODEL: Relational Algebra

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

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

Relational Data Model

Relational Query Languages

Database Design Process

COSC344 Database Theory and Applications. σ a= c (P) S. Lecture 4 Relational algebra. π A, P X Q. COSC344 Lecture 4 1

6. Relational Algebra (Part II)

CSE 544 Principles of Database Management Systems. Magdalena Balazinska Fall 2006 Lecture 3 - Relational Model

Relational Algebra. Study Chapter Comp 521 Files and Databases Fall

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

Relational Model, Relational Algebra, and SQL

Schema Refinement: Dependencies and Normal Forms

Database Technology Introduction. Heiko Paulheim

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

Chapter 7 Relational Calculus

CS2300: File Structures and Introduction to Database Systems

Stored and Inherited Relations for SQL Databases

Chapter 6 The Relational Algebra and Relational Calculus

CMP-3440 Database Systems

Introduction Relational Algebra Operations

Relational Algebra and SQL. Basic Operations Algebra of Bags

Comp 5311 Database Management Systems. 2. Relational Model and Algebra

Sets 1. The things in a set are called the elements of it. If x is an element of the set S, we say

CS317 File and Database Systems

RELATIONAL DATA MODEL

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

ECE 650 Systems Programming & Engineering. Spring 2018

Query Optimization. Shuigeng Zhou. December 9, 2009 School of Computer Science Fudan University

Unit 4 Relational Algebra (Using SQL DML Syntax): Data Manipulation Language For Relations Zvi M. Kedem 1

Plan of the lecture. G53RDB: Theory of Relational Databases Lecture 1. Textbook. Practicalities: assessment. Aims and objectives of the course

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

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

[Ch 6] Set Theory. 1. Basic Concepts and Definitions. 400 lecture note #4. 1) Basics

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

The Relational Algebra

Chapter 2: Relational Model

Relational Algebra and Calculus

Chapter 3. The Relational Model. Database Systems p. 61/569

Slides for Faculty Oxford University Press All rights reserved.

Unit 4 Relational Algebra (Using SQL DML Syntax): Data Manipulation Language For Relations Zvi M. Kedem 1

CS233:HACD Introduction to Relational Databases Notes for Section 4: Relational Algebra, Principles and Part I 1. Cover slide

Relational Design: Characteristics of Well-designed DB

Chapter 4: SQL. Basic Structure

A set with only one member is called a SINGLETON. A set with no members is called the EMPTY SET or 2 N

Lecture 3 SQL. Shuigeng Zhou. September 23, 2008 School of Computer Science Fudan University

Chapter 3: Introduction to SQL. Chapter 3: Introduction to SQL

Relational Database Model. III. Introduction to the Relational Database Model. Relational Database Model. Relational Terminology.

Databases 1. Daniel POP

Unit 4 SQL language: other definitions

Schema Refinement: Dependencies and Normal Forms

Transcription:

Information Systems Relational Databases Nikolaj Popov Research Institute for Symbolic Computation Johannes Kepler University of Linz, Austria popov@risc.uni-linz.ac.at

Outline The Relational Model (Continues from the Previous Lecture) Data Structure. Types and Relations Data Manipulation. Relational Algebra

Outline The Relational Model (Continues from the Previous Lecture) Data Structure. Types and Relations Data Manipulation. Relational Algebra

Relations Up to now we discussed type, values, and variables in general. Now: Relations types, values, and variables in particular. Since relations are built out of tuples, we examine tuple types, values, and variables.

Tuples Tuple Given a collection of (not necessarily distinct) types T i, 1 i n, a tuple value (or tuple) t on those types is a set of ordered triples of the form A i, T i, v i, where Ai is an attribute name, T i is a type name, v i is a value of type T i. The value n is the degree or arity of t. The ordered triple Ai, T i, v i is a component of t. The ordered pair Ai, T i is an attribute of t and is uniquely identified by A i. (A i s are all distinct.) vi is the attribute value for A i. T i is the attribute type for A i. The complete set of attributes is the heading of t. The tuple type of t is determined by the heading of t. The tuple type name is precisely TUPLE { A 1 T 1, A 2 T 2,..., A n T n }.

Tuple Example Sample tuple: { MAJOR_P#, P#, P2, MINOR_P#, P#, P4, QTY, QTY, 7 } MAJOR_P# : P# MINOR_P# : P# QTY : QTY P2 P4 7 Attribute names: MAJOR_P#, MINOR_P#, QTY. The corresponding type names: P#, P#, and QTY. The corresponding values: P2, P4, 7. The degree of the tuple is three. The heading: MAJOR_P# : P# MINOR_P# : P# QTY : QTY The type: TUPLE { MAJOR_P# P#, MINOR_P# P#, QTY QTY}

Tuple In informal contexts type names are often omitted from a tuple heading, showing just the attribute names. For instance, writing instead of MAJOR_P# MINOR_P# QTY P2 P4 7 MAJOR_P# : P# MINOR_P# : P# QTY : QTY P2 P4 7

Tuple Properties Every tuple contains exactly one value for each attribute. The order of components of a tuple does not matter. Every subset (including the empty subset) of a tuple is a tuple.

Tuple Type Generators Example: VAR ADDR TUPLE { STREET CHAR, CITY CHAR, STATE CHAR, ZIP CHAR } ; Defines the variable ADDR to be of type TUPLE { STREET CHAR, CITY CHAR, STATE CHAR, ZIP CHAR } Tuple selector operator: TUPLE { STREET 1600 Penn. Ave., CITY Washington, STATE DC, ZIP 20500 }

Operations on Tuples Tuple equality: Tuples t 1 and t 2 are equal (t 1 = t 2 ) iff 1. they have the same attributes Attr 1,..., Attr n, and 2. the value v i of Attr i in t 1 is equal to the value v i of Attr i in t 2.

Operations on Tuples Assume the current value of the ADDR variable is TUPLE { STREET 1600 Penn. Ave., CITY Washington, STATE DC, ZIP 20500 } Tuple projection: ADDR { CITY, ZIP } denotes the tuple TUPLE { CITY Washington, ZIP 20500 }. Extraction: ZIP FROM ADDR denotes 20500. Tuple type inference: Tuple type of the result of ADDR { CITY, ZIP } is TUPLE { CITY CHAR, ZIP CHAR }.

Operations on Tuples WRAP and UNWRAP: Consider the tuple types: TT1: TUPLE { NAME NAME, ADDR TUPLE { STREET CHAR, CITY CHAR, STATE CHAR, ZIP CHAR } }. TT2: TUPLE { NAME NAME, STREET CHAR, CITY CHAR, STATE CHAR, ZIP CHAR }. NADDR1, NADDR2: The variables of types TT1, TT2, resp. The expression NADDR2 WRAP {STREET, CITY, STATE, ZIP} AS ADDR takes the current value of NADDR2 and wraps STREET, CITY, STATE, ZIP components into a single tuple-valued ADDR component. The result is of of type TT1. The expression NADDR1 UNWRAP ADDR takes the current value of NADDR1 and unwraps ADDR into four separate components. The result is of type TT2.

Relations Relation A relation value (or relation) r consists of a heading and a body, where The heading of r is a tuple heading. Relation r has the same attributes and the same degree as that heading does. The body of r is the set of tuples, all having that same heading; the cardinality of that set is said to be the cardinality of r.

Relation type A relation type of r is determined by the heading of r. It has the same attributes (and hence attribute names and types) and degree as that heading does. The relation type name is RELATION { A1 T1,..., An Tn }

Relations Example MAJOR_P# : P# MINOR_P# : P# QTY : QTY P1 P2 5 P1 P3 3 P2 P3 2 P2 P4 7 P3 P5 4 P4 P6 8 Type: RELATION { MAJOR_P# : P#, MINOR_P# : P#, QTY : QTY }

Relations n-ary relation: relation of degree n. Every subset of a heading is a heading. Every subset of a body is a body.

Relation Properties Within the same relation every tuple contains exactly one value for each attribute, no left-to-right ordering to the attributes, no top-to-bottom ordering to the tuples, no duplicate tuples.

Relations with No Attributes Every relation has a set of attributes. This set, in particular, can be empty: No attributes at all. Does not mean the empty relation! Empty relation: relation with the empty body. Relation with no attributes: relation with the empty heading.

Relations with No Attributes Relation with no attributes can contain at most one tuple, the 0-tuple. The 0-tuple contains no components. Hence, two relations of degree 0: one that contains just one tuple, and one that contains no tuples at all.

Operators on Relations Comparisons: =,,,,,, IS_EMPTY. They can appear whenever a boolean expression is expected. Example: S { CITY } = P { CITY }: Is the projection of suppliers over CITY equal to the projection of parts over city?

Operators on Relations Other operators: Test whether the given tuple t appears in a given relation r: t r. Extracting the single tuple from a relation of cardinality one: TUPLE FROM r Other operators like join, restrict, project, etc. Considered in the relational algebra part.

Operators on Relations Relation type inference: Given the suppliers relvar S, the expression S { S#, CITY } yields a relation whose type is RELATION { S# S#, CITY CHAR }

Relation Variables Example Defining base relvars S, P, and SP: VAR S BASE RELATION VAR P BASE RELATION { S# S#, { P# P#, SNAME NAME, PNAME NAME, STATUS INTEGER, COLOR COLOR, CITY CHAR } WEIGHT WEIGHT, PRIMARY KEY { S# } ; CITY CHAR } PRIMARY KEY { P# } ; VAR SP BASE RELATION { S# S#, P# P#, QTY QTY } PRIMARY KEY { S#, P# } FOREIGN KEY { S# } REFERENCES S FOREIGN KEY { P# } REFERENCES P

Explanation The relation type of the relvar S is RELATION {S# S#, SNAME NAME, STATUS INTEGER, CITY CHAR } The terms heading, body, attributes, tuple, degree, etc. are interpreted to apply to relvars. When a base relvar is defined, it is given an initial value that is the empty relation of appropriate type.

Updating Relvars Assume S and SP are base relvars. The type of S is the same as the type of S. The type of SP is the same as the type of SP. Some valid examples of relation assignment: 1. S := S, SP := SP; 2. S := S WHERE CITY = London 3. S := S WHERE NOT (CITY = Paris ) Each assignment (a) retrieves the relation specified on the right hand side and (b) updates the relvar specified on the left hand side.

Outline The Relational Model (Continues from the Previous Lecture) Data Structure. Types and Relations Data Manipulation. Relational Algebra

Relational Algebra Theoretical basis for database query languages. Attracted attention after Edgar F. Codd introduced the relational model in 1970-ies. Formal system for manipulating relations: Operands: relations. Operators: union, intersection, difference, Cartesian product, restrict, project, join, divide, rename. Operations operate on relations and produce relations (closure).

Rename Purpose: Rename attributes within a specified relation. Action: Takes a given relation and returns another one that is identical to the given one except that one of its attributes has a different name. Example: S S# SNAME STATUS CITY S1 Smith 20 London S2 Jones 10 Paris S3 Blake 30 Paris S RENAME CITY AS SCITY S# SNAME STATUS SCITY S1 Smith 20 London S2 Jones 10 Paris S3 Blake 30 Paris

Union Specification: Given two relations a and b of the same type, a UNION b is a relation of the same type, with body consisting of all tuples t such that t appears in a or in b or both. Example: A S# SNAME STATUS CITY S1 Smith 20 London S4 Clark 20 London B S# SNAME STATUS CITY S1 Smith 20 London S2 Jones 10 Paris A UNION B S# SNAME STATUS CITY S1 Smith 20 London S4 Clark 20 London S2 Jones 10 Paris

Intersection Given two relations a and b of the same type, a INTERSECT b is a relation of the same type, with body consisting of all tuples t such that t appears in both a and b. Example: A S# SNAME STATUS CITY S1 Smith 20 London S4 Clark 20 London B S# SNAME STATUS CITY S1 Smith 20 London S2 Jones 10 Paris A INTERSECT B S# SNAME STATUS CITY S1 Smith 20 London

Difference Given two relations a and b of the same type, a MINUS b is a relation of the same type, with body consisting of all tuples t such that t appears a and not in b. Example: A S# SNAME STATUS CITY S1 Smith 20 London S4 Clark 20 London B S# SNAME STATUS CITY S1 Smith 20 London S2 Jones 10 Paris A MINUS B S# SNAME STATUS CITY S4 Clark 20 London B MINUS A S# SNAME STATUS CITY S2 Jones 10 Paris

Cartesian Product Given two relations a and b without common attribute names, a TIMES b is a relation with a heading that is the (set theoretic) union of the heading of a and b and with the body consisting of the set of all tuples t such that t is a (set theoretic) union of a tuple appearing in a and a tuple appearing in b. Example: A S# B P# A TIMES B S# P# S1 P1 S1 P1 S2 P2 S1 P2 P3 S1 P3 S2 P1 S2 P2 S2 P3

Restriction Given a relation a with attributes X, Y,..., Z and a truth-valued function p whose parameters are some subset of X, Y,..., Z, the restriction of a according to p, a WHERE p, is a relation with the same heading as a and with body consisting of all those tuples in a on which p evaluates to TRUE. Example: S S# SNAME STATUS CITY S1 Smith 20 London S2 Jones 10 Paris S3 Blake 30 Paris S WHERE CITY = London S# SNAME STATUS CITY S1 Smith 20 London

Restriction Given a relation a with attributes X, Y,..., Z and a truth-valued function p whose parameters are some subset of X, Y,..., Z, the restriction of a according to p, a WHERE p, is a relation with the same heading as a and with body consisting of all those tuples in a on which p evaluates to TRUE. Example: P P# PN COLOR WEIGHT CITY P1 Nut Red 12.0 London P2 Bolt Green 17.0 Paris P3 Screw Blue 17.0 Oslo P4 Screw Red 14.0 London P5 Cam Blue 12.0 Paris P WHERE WEIGHT < WEIGHT (14.0) P# PN COLOR WEIGHT CITY P1 Nut Red 12.0 London P5 Cam Blue 12.0 Paris

Restriction Given a relation a with attributes X, Y,..., Z and a truth-valued function p whose parameters are some subset of X, Y,..., Z, the restriction of a according to p, a WHERE p, is a relation with the same heading as a and with body consisting of all those tuples in a on which p evaluates to TRUE. Example: SP S# P# QTY S1 P1 300 S1 P2 200 S2 P1 400 S2 P2 100 SP WHERE S# = S# ( S3 ) or P# = P# ( P4 ) S# P# QTY

Projection Given a relation a with attributes X, Y,..., Z, the projection of a according on X, Y,..., Z, written a{x, Y,..., Z }, is a relation with a heading derived from the heading of a by removing all attributes that are not among X, Y,..., Z ; a body consisting of all tuples {X x, Y y,..., Z z} such that the tuple appears in a with X value x, Y value y,..., and Z value z. Example: S S# SNAME STATUS CITY S1 Smith 20 London S2 Jones 10 Paris S3 Blake 30 Paris S { CITY } CITY London Paris

Projection Given a relation a with attributes X, Y,..., Z, the projection of a according on X, Y,..., Z, written a{x, Y,..., Z }, is a relation with a heading derived from the heading of a by removing all attributes that are not among X, Y,..., Z ; a body consisting of all tuples {X x, Y y,..., Z z} such that the tuple appears in a with X value x, Y value y,..., and Z value z. Example: P P# PN COLOR WEIGHT CITY P1 Nut Red 12.0 London P2 Bolt Green 17.0 Paris P3 Screw Blue 17.0 Oslo P4 Screw Red 14.0 London P {COLOR, CITY } COLOR Red Green Blue CITY London Paris Oslo

Projection Given a relation a with attributes X, Y,..., Z, the projection of a according on X, Y,..., Z, written a{x, Y,..., Z }, is a relation with a heading derived from the heading of a by removing all attributes that are not among X, Y,..., Z ; a body consisting of all tuples {X x, Y y,..., Z z} such that the tuple appears in a with X value x, Y value y,..., and Z value z. Example: S S# SNAME STATUS CITY S1 Smith 20 London S2 Jones 10 Paris S3 Blake 30 Paris (S WHERE CITY = Paris ) { S# } S# S2 S3

Join Let a relation a have attributes X 1,..., X m, Y 1,... Y n, and b have the attributes Y 1,... Y n, Z 1,..., Z p. The (natural) join of a and b, denoted a JOIN b is a relation with heading X 1,..., X m, Y 1,..., Y n, Z 1,..., Z p and body consisting of all tuples X 1 x 1,..., X m x m, Y 1 y 1,..., Y n y n, Z 1 z 1,..., Z p z p such that a tuple appears in a with Xi value x i, and Y j value y j for all 1 i m and 1 j n, and a tuple appears in b with Yj value y j and Z k value z k for all 1 j n and 1 k p.

Join. Example S# SNAME ST CITY S S1 Smith 20 London S2 Jones 10 Paris S3 Blake 30 Paris P# PN COLOR WGT CITY P P1 Nut Red 12.0 London P2 Bolt Green 17.0 Paris P3 Screw Blue 17.0 Oslo P4 Screw Red 14.0 London P5 Cam Blue 12.0 Paris S JOIN P S# SNAME ST CITY P# PN COLOR WGT S1 Smith 20 London P1 Nut Red 12.0 S1 Smith 20 London P4 Screw Red 14.0 S2 Jones 10 Paris P2 Bolt Green 17.0 S2 Jones 10 Paris P5 Cam Blue 12.0 S3 Blake 30 Paris P2 Bolt Green 17.0 S3 Blake 30 Paris P5 Cam Blue 12.0

Examples. Supplier-and-Parts Get supplier names for suppliers who supply part P2. ( ( SP JOIN S ) WHERE P# = P# ( P2 )) { SNAME } SP JOIN S extends each SP tuple with the corresponding supplier information (SNAME, STATUS, CITY values). The result is restricted to just those tuples for part P2. The restriction is projected over SNAME

Examples. Supplier-and-Parts Get supplier names for suppliers who supply at least one red part. ( ( ( P WHERE COLOR = COLOR( Red ) ) JOIN SP ) { S# } JOIN S ){ SNAME }

Examples. Supplier-and-Parts Get all pairs of supplier numbers such that the suppliers are located in the same city. ( ( ( S RENAME S# AS SA ) { SA, CITY } JOIN ( S RENAME S# AS SB ) { SB, CITY } ) WHERE SA < SB { SA, SB }

Examples. Supplier-and-Parts Get supplier names for suppliers who do not supply part P2. ( ( S { S# } MINUS ( SP WHERE P# = P# ( P2 ) ) {S#} ) JOIN S ){ SNAME }