Midterm Review. Winter Lecture 13

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

Relational Model, Relational Algebra, and SQL

Querying Data with Transact SQL

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

SQL OVERVIEW. CS121: Relational Databases Fall 2017 Lecture 4

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

Relational Algebra and SQL

RELATIONAL ALGEBRA. CS121: Relational Databases Fall 2017 Lecture 2

Chapter 4: SQL. Basic Structure

Chapter 2: Relational Model

Chapter 2: Intro to Relational Model

II. Structured Query Language (SQL)

II. Structured Query Language (SQL)

Relational Databases

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

DATABASE TECHNOLOGY - 1MB025

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

Querying Data with Transact-SQL

Chapter 2: Intro to Relational Model

CMP-3440 Database Systems

MTA Database Administrator Fundamentals Course

2.2.2.Relational Database concept

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

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

SQL QUERIES. CS121: Relational Databases Fall 2017 Lecture 5

Chapter 3: Introduction to SQL

Querying Data with Transact SQL Microsoft Official Curriculum (MOC 20761)

DATABASE DESIGN I - 1DL300

Database System Concepts

Oracle Database 11g: SQL and PL/SQL Fundamentals

Lecture Notes for 3 rd August Lecture topic : Introduction to Relational Model. Rishi Barua Shubham Tripathi

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

Chapter 4. Basic SQL. Copyright 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

The SQL database language Parts of the SQL language

SQL (Structured Query Language)

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

Database Systems SQL SL03

Mahathma Gandhi University

Chapter 3: SQL. Chapter 3: SQL

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

WHAT IS SQL. Database query language, which can also: Define structure of data Modify data Specify security constraints

Chapter 7. Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel

Chapter 3: Introduction to SQL

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

Chapter 4. The Relational Model

Database Systems SQL SL03

Database Technology Introduction. Heiko Paulheim

Course Modules for MCSA: SQL Server 2016 Database Development Training & Certification Course:

Oracle Database 10g: Introduction to SQL

DATABASE TECHNOLOGY - 1MB025

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

DATABASTEKNIK - 1DL116

Querying Data with Transact-SQL (761)

Querying Data with Transact-SQL

Database Management Systems Paper Solution

20461: Querying Microsoft SQL Server 2014 Databases

Slides by: Ms. Shree Jaswal

CSEN 501 CSEN501 - Databases I

Ian Kenny. November 28, 2017

CS 582 Database Management Systems II

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

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

Sql Server Syllabus. Overview

SQL Fundamentals. Chapter 3. Class 03: SQL Fundamentals 1

Introduction to Computer Science and Business

DB2 SQL Class Outline

SQL: Data De ni on. B0B36DBS, BD6B36DBS: Database Systems. h p:// Lecture 3

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

CS122 Lecture 4 Winter Term,

CS122 Lecture 5 Winter Term,

Database Usage (and Construction)

COSC344 Database Theory and Applications. Lecture 5 SQL - Data Definition Language. COSC344 Lecture 5 1

Oracle Database: SQL and PL/SQL Fundamentals NEW

Database Management

Relational Algebra. Procedural language Six basic operators

Basic SQL. Dr Fawaz Alarfaj. ACKNOWLEDGEMENT Slides are adopted from: Elmasri & Navathe, Fundamentals of Database Systems MySQL Documentation

SQL - Subqueries and. Schema. Chapter 3.4 V4.0. Napier University

Querying Data with Transact-SQL (20761)

Oracle Database: SQL and PL/SQL Fundamentals Ed 2

Relational Data Model ( 관계형데이터모델 )

Basant Group of Institution

Teradata SQL Features Overview Version

An Introduction to Structured Query Language

Exam code: Exam name: Database Fundamentals. Version 16.0

SQL: Part II. Announcements (September 18) Incomplete information. CPS 116 Introduction to Database Systems. Homework #1 due today (11:59pm)

Microsoft Querying Data with Transact-SQL - Performance Course

BASIC SQL CHAPTER 4 (6/E) CHAPTER 8 (5/E)

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

CS2300: File Structures and Introduction to Database Systems

Oracle Syllabus Course code-r10605 SQL

SQL Interview Questions

COURSE OUTLINE MOC 20461: QUERYING MICROSOFT SQL SERVER 2014

RELATIONAL DATA MODEL: Relational Algebra

8) A top-to-bottom relationship among the items in a database is established by a

Querying Data with Transact-SQL

After completing this course, participants will be able to:

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

Querying Microsoft SQL Server

Explain in words what this relational algebra expression returns:

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

Transcription:

Midterm Review Winter 2006-2007 Lecture 13

Midterm Overview 3 hours, single sitting Topics: Relational model relations, keys, relational algebra expressions SQL DDL commands CREATE TABLE, CREATE VIEW Specifying integrity constraints SQL DML commands SELECT, INSERT, UPDATE, DELETE Grouping and aggregation, subqueries, etc.

Relational Model Relations are sets of tuples unlike SQL tables, which are multisets Relations have a schema Relation schemas are sets of attributes, with associated domains Each tuple specifies a value for each attribute or null if value is unknown or missing Every attribute domain includes null

Keys Need a way to uniquely identify tuples within a relation Superkey Any set of attributes that uniquely identifies a tuple If a set of attributes K is a superkey, then so is any superset of K Candidate key A minimalsuperkey If any attribute is removed, no longer a superkey Primary key A particular candidate key, chosen as the primary means of referring to tuples

Keys and Constraints Keys constrain the set of tuples that can appear in a relation In a relation r with a candidate key K, no two tuples can have the same values for K Can also have foreign keys A relation contains the key attributes of another relation Referencing relation has a foreign key Referenced relation has a primary (or candidate) key Referencing relation can only contain values of foreign key that also appear in referenced relation Called referential integrity

Example Auto insurance database car(license, vin, make, model, year) Primary key: license Candidate key: vin (Vehicle ID Number) customer(name, driverlicense, street, city) Every customer must have a different name owner(name, license) claim(name, license, date, description, amount) Can have multiple claims on one car, but have to occur on different dates

A Note about the Example The auto insurance schema specifies a the customer s name as the primary key I decided to leave the examples the same, since it would take too much time to change Plus, that particular auto insurance company happens to have only customers with different names It s part of the company policy

Relational Algebra Operations Six fundamental operations: σ select operation Π project operation ( set-union operation set-difference operation Cartesian product operation ρ rename operation Operations take one or two relations as input Each produces another relation as output

Additional Relational Operations Several additional operations, defined in terms of fundamental operations: ' set-intersection natural join division assignment Extended relational operations: Π G generalized project operation grouping and aggregation left outer join, right outer join, full outer join

Examples Schema: car(license, vin, make, model, year) customer(name, driverlicense, street, city) owner(name, license) claim(name, license, date, description, amount) Find names of all customers living in Los Angeles or New York. Π name (σ city= Los Angeles city= New York (customer)) Select predicate can refer to attributes, constants, or arithmetic expressions using these Conditions combined with and

Examples (2) Schema: car(license, vin, make, model, year) customer(name, driverlicense, street, city) owner(name, license) claim(name, license, date, description, amount) Find customer name, street, and city of all Toyota owners Need to join customer, owner, car relations Could use Cartesian product, select, etc. Or, use natural join operation: Π name,street,city (σ make= Toyota (customer owner car)

Examples (3) Schema: car(license, vin, make, model, year) customer(name, driverlicense, street, city) owner(name, license) claim(name, license, date, description, amount) Find how many claims each customer has Don t include customers with no claims Simple grouping and aggregation operation nameg count(license) as num_claims (claim) Aggregate operations work on multisets by default

Examples (4) Now, include customers with no claims They should have 0 in their values Requires outer join between customer, claim Outer part of join symbol is towards relation whose rows should be null-padded Want all customers, and claim records if they are there, so outer part is towards customer name G count(license) as num_claims (customer claim) Aggregate functions ignore null values

Examples (5) Grouping/aggregation operation produces a relation Can t use aggregate functions in select predicates Find customer with the most claims Use grouping/aggregation to find maximum claim count Use Cartesian product to compare this with each customer s number of claims Common subquery: computation of how many claims each customer has

Examples (6) Use assignment to store temporary result claim_counts name G count(license) as num_claims (claim) max_count G max(num_claims) as max_claims (claim_counts) σ num_claims=max_claims (claim_counts max_count) In relational algebra, need to use Cartesian product to compare contents of two relations Result of grouping/aggregation is always contained within a relation

Rename Operation Mainly used when joining a relation to itself Need to rename one copy of the relation to avoid ambiguities Remember that Π and G both allow names to be specified Can rename attributes Can assign a name to results Often shorter than including extra ρ operation

Modifying Relations Can add rows to a relation r r ( { ( ), ( ) } Can delete rows from a relation r r σ P (r) Can modify rows in a relation r Π(r) Uses generalized project operation Remember to include unmodified rows, too! r Π(σ P (r)) ( σ ŸP (r)

Structured Query Language SQL is a query language based on relational algebra Largely declarative in nature Tables are like relations, but are multisets Each column has a type (its domain), and possibly other constraints (e.g. NOT NULL) Can issue queries against tables using SELECT statement

SELECT Statements Basic SELECT syntax: SELECT A 1, A 2,... FROM r 1, r 2,... WHERE P; Equivalent to: Π (σ P (r 1 r 2 )) A 1,A 2, SELECT is a generalized project operation Can include expressions, etc. SELECT generates a multiset by default Can specify SELECT DISTINCT to eliminate duplicates

FROM Clause FROM clause supports JOIN syntax too Examples: FROM t1 JOIN t2 ON t1.a1 = t2.a1 FROM t1 JOIN t2 USING (a1, a2) FROM t1 NATURAL JOIN t2 FROM t1 LEFT JOIN t2 USING (a1, a2) FROM t1 NATURAL LEFT JOIN t2 Some of these remove duplicate columns, and some don t! USING and NATURAL joins coalesce columns

WHERE Clause WHERE clause specifies selection predicate Can use AND, OR, NOT to combine conditions NULL values affect comparisons! Can t use = NULL or <> NULL Must use IS NULL or IS NOT NULL Can use BETWEEN to simplify range checks a >= v1 AND a <= v2 a BETWEEN v1 AND v2

Subqueries Can include subqueries in FROM clause Called a derived relation Nested SELECT statement in FROM clause, given a name and a set of attribute names Can also use subqueries in WHERE clause Can compare an attribute to a scalar subquery Can also use set-comparison operations to test against a subquery IN, NOT IN set membership tests SOME, ALL comparison against a set EXISTS, NOT EXISTS empty-set tests

Grouping and Aggregation SQL supports grouping and aggregation GROUP BY specifies attributes to group on Can apply aggregate functions to other columns, in SELECT clause Can filter results of grouping operation using HAVING clause HAVING clause can use aggregate values too Difference between WHERE and HAVING? WHERE applied before grouping; HAVING after

Aggregated Values Schema: car(license, vin, make, model, year) customer(name, driverlicense, street, city) owner(name, license) claim(name, license, date, description, amount) Find customers with more claims than average number of claims An aggregate of another aggregate

Aggregate Values (2) Two steps to find average number of claims: Must compute count for each customer SELECT name, COUNT(license) AS num_claims FROM claim GROUP BY name Then, compute average (MySQL syntax): SELECT AVG(num_claims) FROM (SELECT COUNT(license) AS num_claims FROM claim GROUP BY name) AS counts Now, compare this result to each count Need to issue group/count operation again! This query gets complicated fast

Aggregated Values (3) Easy solution (for databases without WITH): Define a view, then query against the view CREATE VIEW claim_counts AS SELECT name, COUNT(*) AS num_claims FROM claim GROUP BY name; SELECT name FROM claim_counts WHERE num_claims > (SELECT AVG(num_claims) FROM claim_counts); Can also use WITH syntax for this

SQL Data Definition Can specify table schemas using CREATE TABLE syntax Specify each column s name and domain Can specify domain constraint: NOT NULL Can specify key constraints PRIMARY KEY UNIQUE (candidate keys) REFERENCES table (column) Key constraints can go in column declaration Can also specify keys after all column decls. UNIQUE and REFERENCES don t imply NOT NULL!

DDL Example Relation schema: car(license, vin, make, model, year) CREATE TABLE statement: CREATE TABLE car ( license CHAR(10) PRIMARY KEY, vin CHAR(30) NOT NULL UNIQUE, make VARCHAR(20) NOT NULL, model VARCHAR(20) NOT NULL, year INTEGER NOT NULL );

DDL Example (2) Relation schema: claim(name, license, date, description, amount) CREATE TABLE statement: CREATE TABLE claim ( name VARCHAR(30), license CHAR(10), date TIMESTAMP, description VARCHAR(1000) NOT NULL, amount NUMERIC(8,2), PRIMARY KEY (name, license, date), FOREIGN KEY name REFERENCES customer, FOREIGN KEY license REFERENCES car );

Midterm Details Midterm posted online shortly Due next Friday, February 9 No homework to do next week Good luck!