Data Structure: Relational Model

Similar documents
CS 232A: Database System Principles. Introduction. Introduction. Applications View of a Relational Database Management (RDBMS) System

CSE 135. Applications View of a Relational Database Management System (RDBMS) SQL. Persistent data structure. High-level API for access &modification

SQL:Union, Intersection, Difference. SQL: Natural Join

Relational Model, Relational Algebra, and SQL

Chapter 2: Intro to Relational Model

CMP-3440 Database Systems

Relational Algebra and SQL

Relational Algebra. Procedural language Six basic operators

Database Management Systems Paper Solution

II B.Sc(IT) [ BATCH] IV SEMESTER CORE: RELATIONAL DATABASE MANAGEMENT SYSTEM - 412A Multiple Choice Questions.

INF1383 -Bancos de Dados

Database Technology Introduction. Heiko Paulheim

Administration Naive DBMS CMPT 454 Topics. John Edgar 2

CS 377 Database Systems

RELATIONAL DATA MODEL

Relational Databases

Transactions and Concurrency Control

Relational db: the origins. Relational Calculus (aka FO)

Techno India Batanagar Computer Science and Engineering. Model Questions. Subject Name: Database Management System Subject Code: CS 601

Database Systems External Sorting and Query Optimization. A.R. Hurson 323 CS Building

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

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

Introduction to Transaction Management

Chapter 6: Formal Relational Query Languages

Relational Model: History

EECS 647: Introduction to Database Systems

COSC344 Database Theory and Applications. Lecture 21 Transactions

DATABASE MANAGEMENT SYSTEMS

3. Relational Data Model 3.5 The Tuple Relational Calculus

Agenda. Discussion. Database/Relation/Tuple. Schema. Instance. CSE 444: Database Internals. Review Relational Model

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

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

Database Systems SQL SL03

2.2.2.Relational Database concept

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

Announcements. Agenda. Database/Relation/Tuple. Discussion. Schema. CSE 444: Database Internals. Room change: Lab 1 part 1 is due on Monday

Lab IV. Transaction Management. Database Laboratory

Database Systems SQL SL03

DATABASE DESIGN II - 1DL400

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

Relational Model 2: Relational Algebra

A REVIEW OF BASIC KNOWLEDGE OF DATABASE SYSTEM

Page 1. Goals for Today" What is a Database " Key Concept: Structured Data" CS162 Operating Systems and Systems Programming Lecture 13.

Advanced Databases. Transactions. Nikolaus Augsten. FB Computerwissenschaften Universität Salzburg

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

Final Review. May 9, 2017

Final Review. May 9, 2018 May 11, 2018

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

Chapter 3: Relational Model

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

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

Data about data is database Select correct option: True False Partially True None of the Above

Chapter 5 Relational Algebra. Nguyen Thi Ai Thao

Database Systems. Announcement

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

Chapter 2: Relational Model

Databases - 5. Problems with the relational model Functions and sub-queries

CMPT 354: Database System I. Lecture 5. Relational Algebra

Chapter 3: Introduction to SQL

Chapter 3: Introduction to SQL

Mahathma Gandhi University

Chapter 6 - Part II The Relational Algebra and Calculus

DATABASE SYSTEMS. Database Systems 1 L. Libkin

Basant Group of Institution

SQL. The Basics Advanced Manipulation Constraints Authorization 1. 1

Transactions. Concurrency. Consistency ACID. Modeling transactions. Transactions and Concurrency Control

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

CS 582 Database Management Systems II

Fundamentals of Database Systems

Chapter 8: The Relational Algebra and The Relational Calculus

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

QUIZ 1 REVIEW SESSION DATABASE MANAGEMENT SYSTEMS

Outline. Database Management and Tuning. Index Tuning Examples. Exercise 1 Query for Student by Name. Concurrency Tuning. Johann Gamper.

Relational Algebra. Relational Query Languages

Chapter 2: Intro to Relational Model

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

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

Intro to Transaction Management

CMPT 354: Database System I. Lecture 11. Transaction Management

DATABASE SYSTEMS. Database Systems 1 L. Libkin

Basic Structure Set Operations Aggregate Functions Null Values Nested Subqueries Derived Relations Views Modification of the Database Data Definition

Transactions. Kathleen Durant PhD Northeastern University CS3200 Lesson 9

CSC 261/461 Database Systems Lecture 5. Fall 2017

Transactions. The Setting. Example: Bad Interaction. Serializability Isolation Levels Atomicity

Fundamentals of Database Systems

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

CSCD43: Database Systems Technology. Lecture 4

Transactions. Juliana Freire. Some slides adapted from L. Delcambre, R. Ramakrishnan, G. Lindstrom, J. Ullman and Silberschatz, Korth and Sudarshan

Information Systems Engineering. SQL Structured Query Language DML Data Manipulation (sub)language

Transactions. Chapter 15. New Chapter. CS 2550 / Spring 2006 Principles of Database Systems. Roadmap. Concept of Transaction.

Introduction to Relational Databases

CSE 544 Principles of Database Management Systems

SQL Overview cont d. SQL Overview. ICOM 5016 Database Systems. SQL Data Types. A language higher-level than general purpose languages

Database Languages. A DBMS provides two types of languages: Language for accessing & manipulating the data. Language for defining a database schema

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

Part I: Structured Data

SQL (Structured Query Language)

Chapter 9: Transactions

Chapter 3. Algorithms for Query Processing and Optimization

Relational Query Languages. Relational Algebra and SQL. What is an Algebra? Select Operator. The Role of Relational Algebra in a DBMS

Transcription:

Applications View of a Relational Database Management (RDBMS) System Why applications use DBs? Persistent data structure Independent from processes using the data Also, large volume of data High-level programming interface for access & modification Automatically optimized Transaction management (ACID) Atomicity: all or none happens, despite failures & errors Concurrency Isolation: appearance of one at a time Durability: recovery from failures and other errors Application Server Java-based Web Application JDBC/ODBC SQL commands RDBMS Client Relations, cursors, other RDBMS Server Relational Database 1 Data Structure: Relational Model Relational databases: Schema + Data Schema (also called scheme): collection of tables (also called relations) each table has a set of attributes no repeating relation names, no repeating attributes in one table Data (also called instance): Bag (set in 132A) of tuples tuples have one value for each attribute of the table they belong Ti t l e Di rector St udi o Wild Lynch MGM Sky Berto MGM Reds Beatty Disney Tango Berto Disney Schedule The a t e r Ti t l e Odeon Wild Forum Reds Forum Sky Slide from CSE132A by Victor Vianu Relational Model: Primary and Foreign Keys Title is key of means every title value is unique in column.title Primary key.id typically autogenerated Title of Schedule references.title means every Title value of Schedule also appears as.title If attribute R.A references primary key S.B then we say that R.A is a foreign key that references S.B Most common reference case 1 Wild Lynch MGM 3 Reds Beatty Disney Schedule ID The a t e r MID 1 Odeon 1 2 Forum 3 3 Forum 2 3

Programming Interface: JDBC/ODBC How client opens connection with server How access & modification commands are issued 4 Access (Query) & Modification Language: SQL SQL used by the database user declarative: we only describe what we want to retrieve based on tuple relational calculus The result of a query is always a table (regardless of the query language used) Internal Equivalent of SQL: Relational Algebra used internally by the database system procedural (operational): we describe how we retrieve often useful in explaining the semantics of SQL in an indirect way Basic Relational Algebra Operators Selection (σ ) σ c R selects tuples of the argument relation R that satisfy the condition c. The condition c consists of atomic predicates of the form attr = value (attr is attribute of R) attr1 = attr2 other operators possible (e.g., >, <,!=, LIKE) Bigger conditions constructed by conjunctions (AND) and disjunctions (OR) of atomic predicates Find movies where director= Berto σdirector= Berto

Basic Relational Algebra Operators Projection (π) π attr1,, attrnr returns a table that has only the attributes attr1,, attrn of R Cartesian Product (x) the schema of the result has all attributes of both R and S for every pair of tuples r from R and s from S there is a result tuple that consists of r and s if both R and S have an attribute A then rename to R.A and S.A π Title,Director Ti t l e Di rector Wild Lynch Sky Berto Reds Beatty Tango Berto Project the title and director of R A B 0 1 2 4 S A a c R x S R. A B S. A C 0 1 a b 0 1 c d 2 4 a b 2 4 c d C b d SQL Queries: The Basic From Basic form SELECT a1,, an FROM R1,, RM WHERE condition Equivalent relational algebra expression π a1,, anσ condition(r1x xrm) WHERE clause is optional When more than one relations of the FROM have an attribute named A we refer to a specific A attribute as <RelationName>.A Find all theaters SELECT Theater FROM Schedule Find the titles of all movies by Berto SELECT Title FROM WHERE Director= Berto Find the titles, directors and theaters of all currently playing movies SELECT Title, Director, Theater FROM, Schedule WHERE.ID=Schedule.MID SQL Queries: Aggregation and Grouping There is no relational algebra equivalent for aggregation and grouping Aggregate functions: AVG, COUNT, MIN, MAX, SUM, and recently user defined functions as well Group-by Employee Name De pt Sal ar y Joe Toys 45 Nick PCs 50 Jim Toys 35 Jack PCs 40 Find the average salary of all employees SELECT AvgSal=Avg(Salary) FROM Employee AvgSal 42.5 Find the average salary for each department SELECT Dept, AvgSal=Avg(Salary) De pt AvgSal FROM Employee Toys 40 PCs 45 GROUP-BY Dept

SQL Grouping: Conditions that Apply on Groups HAVING clause Find the average salary of for each department that has more than 1 employee SELECT Dept, AvgSal=(Avg(Salary)) FROM Employee GROUP-BY Dept HAVING COUNT(Name)>1 and a Few Tricky Points: Duplicates and their Elimination Duplicate elimination must be explicitly requested SELECT DISTINCT FROM WHERE Find the titles and the directors of all currently playing movies SELECT Title, Director FROM, Schedule WHERE.ID=Schedule.MID SELECT DISTINCT Title, Director FROM, Schedule WHERE.ID=Schedule.MID 1 Wild Lynch MGM 3 Reds Beatty Disney Schedule ID The a t e r MID 1 Odeon 1 2 Forum 1 3 Forum 2 Ti t l e Wild Wild Sky Ti t l e Wild Sky Di rector Lynch Lynch Berto Di rector Lynch Berto and a Few Tricky Points: Null Values Null values all comparisons involving NULL are false by definition all aggregation operations, except count, ignore NULL values 1 Wild Lynch MGM 3 Reds Beatty NULL Find MGM movies SELECT * FROM WHERE Studio= MGM Find movies of unknown studios SELECT * FROM WHERE ISNULL(Studio)

SQL: More Bells and Whistles... Select all attributes using * Pattern matching conditions <attr> LIKE <pattern> Retrieve all movie attributes of currently playing movies SELECT.* FROM, Schedule WHERE.ID=Schedule.MID Retrieve all movies where the title starts with Ta SELECT * FROM WHERE Title LIKE Ta% SQL as a Data Manipulation Language: Insertions inserting tuples INSERT INTO R VALUES (v1,,vk); some values may be left NULL use results of queries for insertion INSERT INTO R SELECT FROM WHERE INSERT INTO VALUES ( Brave, Gibson, Gibson ); INSERT INTO (Title,Director) VALUES ( Brave, Gibson ); INSERT INTO Euro SELECT * FROM WHERE Director = Berto SQL as a Data Manipulation Language: Updates and Deletions Deletion basic form: delete every tuple that satisfies <cond> DELETE FROM R WHERE <cond> Update basic form: update every tuple that satisfies <cond> in the way specified by the SET clause UPDATE R SET A1=<exp1>,, Ak=<expk> WHERE <cond> Delete the movies that are not currently playing DELETE FROM WHERE Title NOT IN SELECT Title FROM Schedule Change all Berto entries to Bertoluci UPDATE SET Director= Bertoluci WHERE Director= Berto Increase all salaries in the Toys dept by 10% UPDATE Employee SET Salary = 1.1 * Salary WHERE Dept = Toys The rich get richer exercise: Increase by 10% the salary of the employee with the highest salary

Transaction Management Transaction: Collection of actions that maintain the consistency of the database if ran to completion & isolated Goal: Guarantee integrity and consistency of data despite Concurrency Failures Concurrency Control Recovery 16 Example Concurrency & Failure Problems Consider the John & Mary checking & savings account C: checking account balance S: savings account balance Check-to-Savings transfer transaction moves $X from C to S If it runs in the system alone and to completion the total sum of C and S stays the same C2S(X=100) C:=C-100 S:=S+100 17 Example Failure Problem & Recovery Module s Goal C2S(X=100) C:=C-100 CPU HALTS S:=S+100 Database is in inconsistent state after machine restarts It is not the developer s problem to account for crashes Recovery module guarantees that all or none of transaction happens and its effects become durable 18

Example Concurrency Problem & Concurrency Control Module s Goals Serial Schedule C:=C+100 S:=S-100 Read(C) C:=C+50 S:=S-50 If multiple transactions run in sequence the resulting database is consistent Serial schedules De facto correct 19 Example Concurrency Problem & Concurrency Control Module s Goals Good Schedule w/ Concurrency C:=C+100 Read(C) C:=C+50 S:=S-100 S:=S-50 Databases allow transactions to run in parallel 20 Example Concurrency Problem & Concurrency Control Module s Goals Bad Schedule w/ Concurrency C:=C+100 Read(C) C:=C+50 S:=S-50 S:=S-100 Bad interleaved schedules may leave database in inconsistent state Developer should not have to account for parallelism Concurrency control module guarantees serializability only schedules equivalent to serial ones happen 21