Databases-1 Lecture-01. Introduction, Relational Algebra

Similar documents
Relational Algebra and SQL. Basic Operations Algebra of Bags

Relational Algebra. Algebra of Bags

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

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

Relational Query Languages

Chapter 2 The relational Model of data. Relational algebra

1 Relational Data Model

Chapter 2 The relational Model of data. Relational model introduction

CS145 Introduction. About CS145 Relational Model, Schemas, SQL Semistructured Model, XML

Introduction to SQL. Select-From-Where Statements Multirelation Queries Subqueries. Slides are reused by the approval of Jeffrey Ullman s

Databases 1. Defining Tables, Constraints

Introduction to SQL SELECT-FROM-WHERE STATEMENTS SUBQUERIES DATABASE SYSTEMS AND CONCEPTS, CSCI 3030U, UOIT, COURSE INSTRUCTOR: JAREK SZLICHTA

CSCI3030U Database Models

CS411 Database Systems. 04: Relational Algebra Ch 2.4, 5.1

SQL DATA DEFINITION LANGUAGE

SQL DATA DEFINITION LANGUAGE

Chapter 6 The database Language SQL as a tutorial

SQL DATA DEFINITION LANGUAGE

Introduction to SQL. Multirelation Queries Subqueries. Slides are reused by the approval of Jeffrey Ullman s

SQL: Data Definition Language

Introduction to SQL. Select-From-Where Statements Multirelation Queries Subqueries

Relational Query Languages: Relational Algebra. Juliana Freire

EXTENDED RELATIONAL ALGEBRA OUTERJOINS, GROUPING/AGGREGATION INSERT/DELETE/UPDATE

Chapter 6 The database Language SQL as a tutorial

SQL Continued! Outerjoins, Aggregations, Grouping, Data Modification

They like one or more beers and frequent one. Bars have unique names and addresses. They. serve one or more beers and are frequented

CS54100: Database Systems

Constraints. Local and Global Constraints Triggers

More SQL. Extended Relational Algebra Outerjoins, Grouping/Aggregation Insert/Delete/Update

SQL: Data Manipulation Language

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

CS W Introduction to Databases Spring Computer Science Department Columbia University

A tuple is dangling if it doesn't join with any

Why SQL? SQL is a very-high-level language. Database management system figures out best way to execute query

Beersèname, manfè. Likesèdrinker, beerè. Sellsèbar, beer, priceè. Frequentsèdrinker, barè

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

Constraints and Triggers

Subqueries. Must use a tuple-variable to name tuples of the result

CS3DB3/SE4DB3/SE6DB3 TUTORIAL

Entity-Relationship Model. Purpose of E/R Model

Database Design and Programming

2.2.2.Relational Database concept

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

Relational Model and Relational Algebra

SQL. The Basics Advanced Manipulation Constraints Authorization 1. 1

Views, Indexes, Authorization. Views. Views 8/6/18. Virtual and Materialized Views Speeding Accesses to Data Grant/Revoke Priviledges

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

SQL Functionality SQL. Creating Relation Schemas. Creating Relation Schemas

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

types, but key declarations and constraints Similar CREATE X commands for other schema ëdrop X name" deletes the created element of beer VARCHARè20è,

Announcements. Relational Model & Algebra. Example. Relational data model. Example. Schema versus instance. Lecture notes

Relational Algebra. Spring 2012 Instructor: Hassan Khosravi

Relational Model, Relational Algebra, and SQL

CS54100: Database Systems

CSCD43: Database Systems Technology. Lecture 4

Relational Model & Algebra. Announcements (Tue. Sep. 3) Relational data model. CompSci 316 Introduction to Database Systems

I Relational Database Modeling how to define

CSC 343 Winter SQL: Aggregation, Joins, and Triggers MICHAEL LIUT

Relational Model & Algebra. Announcements (Thu. Aug. 27) Relational data model. CPS 116 Introduction to Database Systems

SQL: Concepts. Todd Bacastow IST 210: Organization of Data 2/17/ IST 210

CS2300: File Structures and Introduction to Database Systems

What is an Algebra. Core Relational Algebra. What is Relational Algebra? Operação de Seleção. Álgebra Relacional: Resumo

Contains slides made by Naci Akkøk, Pål Halvorsen, Arthur M. Keller, Vera Goebel

I Relational Database Modeling how to define

Chapter 2: Intro to Relational Model

Transactions, Views, Indexes. Controlling Concurrent Behavior Virtual and Materialized Views Speeding Accesses to Data

Mahathma Gandhi University

Relational Algebra. Procedural language Six basic operators

Relational Databases

From E/R Diagrams to Relations

Chapter 6 Formal Relational Query Languages

Today s topics. Null Values. Nulls and Views in SQL. Standard Boolean 2-valued logic 9/5/17. 2-valued logic does not work for nulls

CMP-3440 Database Systems

CSC 261/461 Database Systems Lecture 13. Fall 2017

The Relational Model

Relational Algebra. Lecture 4A Kathleen Durant Northeastern University

CS 377 Database Systems

Basant Group of Institution

From Relational Algebra to the Structured Query Language. Rose-Hulman Institute of Technology Curt Clifton

ECE 650 Systems Programming & Engineering. Spring 2018

Ian Kenny. November 28, 2017

e e Conceptual design begins with the collection of requirements and results needed from the database (ER Diag.)

Programming and Database Fundamentals for Data Scientists

Relational Model: History

Basic operators: selection, projection, cross product, union, difference,

SQL Overview. CSCE 315, Fall 2017 Project 1, Part 3. Slides adapted from those used by Jeffrey Ullman, via Jennifer Welch

Relational Databases. Relational Databases. Extended Functional view of Information Manager. e.g. Schema & Example instance of student Relation

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

Relational Algebra. Study Chapter Comp 521 Files and Databases Fall

user specifies what is wanted, not how to find it

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

Relational Algebra 1

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

The SQL data-definition language (DDL) allows defining :

XQUERY: an XML Query Language

Real SQL Programming Persistent Stored Modules (PSM)

Recap and Schedule. Till Now: Today: Ø Query Languages: Relational Algebra; SQL. Ø Datalog A logical query language. Ø SQL Recursion.

Grouping Operator. Applying γ L (R) Recall: Outerjoin. Example: Grouping/Aggregation. γ A,B,AVG(C)->X (R) =??

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

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

Transcription:

Databases-1 Lecture-01 Introduction, Relational Algebra

Information, 2018 Spring About me: Hajas Csilla, Mathematician, PhD, Senior lecturer, Dept. of Information Systems, Eötvös Loránd University of Budapest Databases-1 Lecture: Friday 10:15-11:45 ELTE South Building, 0-220 Karteszi Room Website of the course: http://sila.hajas.elte.hu/edu18feb/db1l.html 2 DB1Lect_01_RelAlgebra (Hajas, ELTE) --- based on Ullman s book and slides

Textbook A First Course in Database Systems (3rd ed.) by Jeff Ullman and Jennifer Widom same material and sections as Database Systems: The Complete Book (2nd ed) by Garcia-Molina, Jeff Ullman and Jennifer Widom 3 DB1Lect_01_RelAlgebra (Hajas, ELTE) --- based on Ullman s book and slides

Topics of the semester Relational Data Model Core and Extended Relational Algebra SQL Query and Modification Constraints, Triggers and Views PSM, Oracle PL/SQL Datalog, Recursion Entity-Relationship Model Design of Relational Databases 4 DB1Lect_01_RelAlgebra (Hajas, ELTE) --- based on Ullman s book and slides

What is a Data Model? 1. Mathematical representation of data 2. Operation on data 3. Constraints 5 DB1Lect_01_RelAlgebra (Hajas, ELTE) --- based on Ullman s book and slides

Relational Data Model A relation is a table Attributes (column headers) Tuples (rows) Beer Winterbrew Bud Lite Manufacturer Pete s Anheuser-Busch 6 DB1Lect_01_RelAlgebra (Hajas, ELTE) --- based on Ullman s book and slides

Types and schemas Relation schema = relation name + attributes, in order (+ types of attributes). Example: Beers(name, manf) or Beers(name: string, manf: string) Database = collection of relations. Database schema = set of all relation schemas in the database. 7 DB1Lect_01_RelAlgebra (Hajas, ELTE) --- based on Ullman s book and slides

Why relations? Very simple model. Often matches how we think about data. Abstract model that underlies SQL, the most important database language today. 8 DB1Lect_01_RelAlgebra (Hajas, ELTE) --- based on Ullman s book and slides

Relational model Logical level: The relations are considered as tables. The tables has unique names The colums address the attributes The rows represent the records Rows can be interchanged, the order of rows is irrelevant Physical level: The relations are stored in a file structure 9 DB1Lect_01_RelAlgebra (Hajas, ELTE) --- based on Ullman s book and slides

Examples Example 1 Example 2 A B C B C A a b c b c a d a a a a d c b d d d c Example 3 Example 4 A B C A B C c b d c b d d a a c b d a b c a b c In ex. 1 and ex. 2 the columns are interchanged but the same relation In ex. 1 and ex. 3 the same tuples are represented in different orders but these are the same relations too. Ex. 4 is not a relation 10 DB1Lect_01_RelAlgebra (Hajas, ELTE) --- based on Ullman s book and slides

Defining a Database Schema A database schema comprises declarations for the relations ( tables ) of the database. Many other kinds of elements may also appear in the database schema, including views, constraints, triggers, indexes, etc. 11 DB1Lect_01_RelAlgebra (Hajas, ELTE) --- based on Ullman s book and slides

Declaring a Relation Simplest form is: CREATE TABLE <name> (<list of elements>); CREATE TABLE Sells ( bar CHAR(20), beer VARCHAR(20), price REAL ); 12 DB1Lect_01_RelAlgebra (Hajas, ELTE) --- based on Ullman s book and slides

Elements of Table Declarations The principal element is a pair consisting of an attribute and a type. The most common types are: INT or INTEGER (synonyms). REAL or FLOAT (synonyms). CHAR(n ) = fixed-length string of n characters. VARCHAR(n ) = variable-length string of up to n characters. DATE is a type, and the form of a date value is: Example: yyyy-mm-dd DATE 2002-09-30 13 DB1Lect_01_RelAlgebra (Hajas, ELTE) --- based on Ullman s book and slides

Example: Create Table CREATE TABLE Sells ( bar CHAR(20), beer VARCHAR(20), price REAL ); 14 DB1Lect_01_RelAlgebra (Hajas, ELTE) --- based on Ullman s book and slides

Other Declarations for Attributes Declaration for an attributes is a pair consisting of an attribute and a type. Other declarations we can make for an attribute are: 1. NOT NULL means that the value for this attribute may never be NULL. 2. DEFAULT <value> says that if there is no specific value known for this attribute s component in some tuple, use the stated <value>. 15 DB1Lect_01_RelAlgebra (Hajas, ELTE) --- based on Ullman s book and slides

Example: Default Values CREATE TABLE Drinkers ( name CHAR(30)NOT NULL, addr CHAR(50)DEFAULT 3 Sesame St., phone CHAR(16) ); 16 DB1Lect_01_RelAlgebra (Hajas, ELTE) --- based on Ullman s book and slides

Effect of Defaults -- 1 Suppose we insert the fact that Sally is a drinker, but we know neither her address nor her phone. An INSERT with a partial list of attributes makes the insertion possible: INSERT INTO Drinkers(name) VALUES( Sally ); 17 DB1Lect_01_RelAlgebra (Hajas, ELTE) --- based on Ullman s book and slides

Effect of Defaults -- 2 But what tuple appears in Drinkers? name addr phone Sally 123 Sesame St NULL If we had declared phone NOT NULL, this insertion would have been rejected. 18 DB1Lect_01_RelAlgebra (Hajas, ELTE) --- based on Ullman s book and slides

Remove a relation from schema Remove a relation from the database schema by: DROP TABLE <name>; Example: DROP TABLE Sells; 19 DB1Lect_01_RelAlgebra (Hajas, ELTE) --- based on Ullman s book and slides

Query Languages: Relational Algebra What is an Algebra? Mathematical system consisting of: Operands --- variables or values from which new values can be constructed. Operators --- symbols denoting procedures that construct new values from given values. 20 DB1Lect_01_RelAlgebra (Hajas, ELTE) --- based on Ullman s book and slides

Core Relational Algebra Union, intersection, and difference. Usual set operations, but require both operands have the same relation schema. Selection: picking certain rows. Projection: picking certain columns. Products and joins: compositions of relations. Renaming of relations and attributes. 21 DB1Lect_01_RelAlgebra (Hajas, ELTE) --- based on Ullman s book and slides

Union, intersection, difference To apply these operators the relations must have the same attributes. Union (R1 R2): all tuples from R1 or R2 Intersection (R1 R2): common tuples from R1 and R2 Difference (R1\R2): tuples occuring in R1 but not in R2 22 DB1Lect_01_RelAlgebra (Hajas, ELTE) --- based on Ullman s book and slides

Example Relation Sells1: Bar Beer Price Joe s Bud 2.50 Joe s Miller 2.75 Sue s Bud 2.50 Relation Sells2: Bar Beer Price Joe s Bud 2.50 Jack s Bud 2.75 Sells1 Sells2: Bar Beer Price Joe s Bud 2.50 Joe s Miller 2.75 Sue s Bud 2.50 Jack s Bud 2.75 Sells1 Sells2: Bar Beer Price Joe s Bud 2.50 Sells2 \ Sells1: Bar Beer Price Jack s Bud 2.75 23 DB1Lect_01_RelAlgebra (Hajas, ELTE) --- based on Ullman s book and slides

Selection R1 := σ C (R2) C is a condition (as in if statements) that refers to attributes of R2. R1 is all those tuples of R2 that satisfy C. 24 DB1Lect_01_RelAlgebra (Hajas, ELTE) --- based on Ullman s book and slides

Example Relation Sells: Bar Beer Price Joe s Bud 2.50 Joe s Miller 2.75 Sue s Bud 2.50 Sue s Miller 3.00 JoeMenu := σ bar= Joe s (Sells): Bar Beer Price Joe s Bud 2.50 Joe s Miller 2.75 25 DB1Lect_01_RelAlgebra (Hajas, ELTE) --- based on Ullman s book and slides

Projection R1 := π L (R2) L is a list of attributes from the schema of R2. R1 is constructed by looking at each tuple of R2, extracting the attributes on list L, in the order specified, and creating from those components a tuple for R1. Eliminate duplicate tuples, if any. 26 DB1Lect_01_RelAlgebra (Hajas, ELTE) --- based on Ullman s book and slides

Example Relation Sells: Bar Beer Price Joe s Bud 2.50 Joe s Miller 2.75 Sue s Bud 2.50 Sue s Miller 3.00 Prices := π beer,price (Sells): Beer Price Bud 2.50 Miller 2.75 Miller 3.00 27 DB1Lect_01_RelAlgebra (Hajas, ELTE) --- based on Ullman s book and slides

Product R3 := R1 x R2 Pair each tuple t1 of R1 with each tuple t2 of R2. Concatenation t1t2 is a tuple of R3. Schema of R3 is the attributes of R1 and R2, in order. But beware attribute A of the same name in R1 and R2: use R1.A and R2.A. 28 DB1Lect_01_RelAlgebra (Hajas, ELTE) --- based on Ullman s book and slides

Example: R3=R1 x R2 R1 R2 A B 1 2 3 4 B C 5 6 7 8 9 10 R3=R1 x R2 A R1.B R2.B C 1 2 5 6 1 2 7 8 1 2 9 10 3 4 5 6 3 4 7 8 3 4 9 10 29 DB1Lect_01_RelAlgebra (Hajas, ELTE) --- based on Ullman s book and slides

Theta-Join R3 := R1 C R2 Take the product R1 * R2. Then apply σ C to the result. As for σ, C can be any boolean-valued condition. Historic versions of this operator allowed only A theta B, where theta was =, <, etc.; hence the name theta-join. 30 DB1Lect_01_RelAlgebra (Hajas, ELTE) --- based on Ullman s book and slides

Example Sells: Bar Beer Price Joe s Bud 2.50 Joe s Miller 2.75 Sue s Bud 2.50 Sue s Miller 3.00 Bars: Name Joe s Sue s Address Maple st. River rd. Barinfo= Sells Sells.bar = Bars.name Bars Bar Beer Price Name Address Joe s Bud 2.50 Joe s Maple st. Joe s Miller 2.75 Joe s Maple st. Sue s Bud 2.50 Sue s River rd. Sue s Miller 3.00 Sue s River rd. 31 DB1Lect_01_RelAlgebra (Hajas, ELTE) --- based on Ullman s book and slides

Natural Join A frequent type of join connects two relations by: Equating attributes of the same name, and Projecting out one copy of each pair of equated attributes. Called natural join. Denoted R3 := R1 R2. 32 DB1Lect_01_RelAlgebra (Hajas, ELTE) --- based on Ullman s book and slides

Example Sells: Bar Beer Price Joe s Bud 2.50 Joe s Miller 2.75 Sue s Bud 2.50 Sue s Miller 3.00 Bars: Bar Joe s Sue s Address Maple st. River rd. Barinfo= Sells Bars Bar Beer Price Address Joe s Bud 2.50 Maple st. Joe s Miller 2.75 Maple st. Sue s Bud 2.50 River rd. Sue s Miller 3.00 River rd. 33 DB1Lect_01_RelAlgebra (Hajas, ELTE) --- based on Ullman s book and slides

Renaming The RENAME operator gives a new schema to a relation. R1 := ρ 1(A1,,An) (R2) makes R1 be a relation with attributes A1,,An and the same tuples as R2. Simplified notation: R1(A1,,An) := R2. 34 DB1Lect_01_RelAlgebra (Hajas, ELTE) --- based on Ullman s book and slides

Example Bars: Name Joe s Sue s Address Maple st. River rd. R(Bar, Address) := Bars Bar Joe s Sue s Address Maple st. River rd. 35 DB1Lect_01_RelAlgebra (Hajas, ELTE) --- based on Ullman s book and slides

Building Complex Expressions Algebras allow us to express sequences of operations in a natural way Example: in arithmetic --- (x + 4)*(y - 3). Relational algebra allows the same. Three notations, just as in arithmetic: 1. Sequences of assignment statements. 2. Expressions with several operators. 3. Expression trees. 36 DB1Lect_01_RelAlgebra (Hajas, ELTE) --- based on Ullman s book and slides

Sequences of Assignments Create temporary relation names. Renaming can be implied by giving relations a list of attributes. Example: R3 := R1 C R2 can be written: R4 := R1 x R2 R3 := σ C (R4) 37 DB1Lect_01_RelAlgebra (Hajas, ELTE) --- based on Ullman s book and slides

Expressions in a Single Assignment Example: the theta-join R3 := R1 C R2 can be written: R3 := σ C (R1 x R2) Precedence of relational operators: 1. Unary operators --- select, project, rename --- have highest precedence, bind first. 2. Then come products and joins. 3. Then intersection. 4. Finally, union and set difference bind last. But you can always insert parentheses to force the order you desire. 38 DB1Lect_01_RelAlgebra (Hajas, ELTE) --- based on Ullman s book and slides

Expression Trees Leaves are operands --- either variables standing for relations or particular, constant relations. Interior nodes are operators, applied to their child or children. 39 DB1Lect_01_RelAlgebra (Hajas, ELTE) --- based on Ullman s book and slides

Example Using the relations Bars(name, address) and Sells(bar, beer, price), find the names of all the bars that are either on Maple St. or sell Bud for less than $3. 40 DB1Lect_01_RelAlgebra (Hajas, ELTE) --- based on Ullman s book and slides

As a Tree: U ρ R(name) π name π bar σ addr = Maple St. σ price<3 AND beer= Bud 41 Bars Sells DB1Lect_01_RelAlgebra (Hajas, ELTE) --- based on Ullman s book and slides

Schema-Defining Rules For union, intersection, and difference, the schemas of the two operands must be the same, so use that schema for the result. Selection: schema of the result is the same as the schema of the operand. Projection: list of attributes tells us the schema. Product, Theta-join: the schema is the attributes of both relations. Use R.A, etc., to distinguish two attributes named A. Natural join: use attributes of both relations. Shared attribute names are merged. Renaming: the operator tells the schema. 42 DB1Lect_01_RelAlgebra (Hajas, ELTE) --- based on Ullman s book and slides

Relational algebra: Monotonity Monotone non-decreasing expression: applied on more tuples, the result contains more tuples Formally if Ri Si for every i=1,,n, then E(R1,,Rn) E(S1,,Sn). Difference is the only core expression which is not monotone: A B 1 0 2 1 A B A B - 1 0 1 0-2 1 A B 1 0 2 1 43 DB1Lect_01_RelAlgebra (Hajas, ELTE) --- based on Ullman s book and slides