NULL. The special value NULL could mean: Unknown Unavailable Not Applicable

Similar documents
The SELECT-FROM-WHERE Structure

The SELECT-FROM-WHERE Structure

COMP 244 DATABASE CONCEPTS & APPLICATIONS

CS 4400 Exam 2 ANSWER KEY

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

EECS 647: Introduction to Database Systems

Announcements (September 18) SQL: Part II. Solution 1. Incomplete information. Solution 3? Solution 2. Homework #1 due today (11:59pm)

CS 2340 Objects and Design

Introduction to Data Management. Lecture #17 (SQL, the Never Ending Story )

CMPT 354: Database System I. Lecture 3. SQL Basics

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

Introduction to Data Management. Lecture 16 (SQL: There s STILL More...) It s time for another installment of...

user specifies what is wanted, not how to find it

CS W Introduction to Databases Spring Computer Science Department Columbia University

Querying Data with Transact SQL

SQL Functionality SQL. Creating Relation Schemas. Creating Relation Schemas

Relational Algebra 1 / 38

T-SQL Training: T-SQL for SQL Server for Developers

Lecture 2 SQL. Instructor: Sudeepa Roy. CompSci 516: Data Intensive Computing Systems

SQL - Data Query language

Lecture 3 SQL - 2. Today s topic. Recap: Lecture 2. Basic SQL Query. Conceptual Evaluation Strategy 9/3/17. Instructor: Sudeepa Roy

EECS 647: Introduction to Database Systems

SQL. Chapter 5 FROM WHERE

CMPT 354: Database System I. Lecture 4. SQL Advanced

SQL: Part III. Announcements. Constraints. CPS 216 Advanced Database Systems

Chapter 7 Constraints and Triggers. Spring 2011 Instructor: Hassan Khosravi

Announcements (September 21) SQL: Part III. Triggers. Active data. Trigger options. Trigger example

EGCI 321: Database Systems. Dr. Tanasanee Phienthrakul

Lecture 3 More SQL. Instructor: Sudeepa Roy. CompSci 516: Database Systems

CS143: Relational Model

The Relational Model. Suan Lee

Slides by: Ms. Shree Jaswal

SQL-Nested Queries & Aggregate functions. Lecture By Binu Jasim 02-Aug-2016

SQL: Queries, Constraints, Triggers

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

Databases 1. Defining Tables, Constraints

A7-R3: INTRODUCTION TO DATABASE MANAGEMENT SYSTEMS

1) Introduction to SQL

Integrity Constraints (Reminder)

SQL: Queries, Programming, Triggers

CS 2316 Exam 3 ANSWER KEY

Comp 5311 Database Management Systems. 4b. Structured Query Language 3

origin destination duration New York London 415 Shanghai Paris 760 Istanbul Tokyo 700 New York Paris 435 Moscow Paris 245 Lima New York 455

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

Querying Data with Transact-SQL

Database Management

The Relational Model of Data (ii)

Introduction to Data Management. Lecture #4 (E-R Relational Translation)

01/01/2017. Chapter 5: The Relational Data Model and Relational Database Constraints: Outline. Chapter 5: Relational Database Constraints

An Introduction to Structured Query Language

Announcements (September 14) SQL: Part I SQL. Creating and dropping tables. Basic queries: SFW statement. Example: reading a table

Lecture 2: Introduction to SQL

COGS 121 HCI Programming Studio. Week 03 - Tech Lecture

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

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

ITCS 3160 DATA BASE DESIGN AND IMPLEMENTATION

4. SQL - the Relational Database Language Standard 4.3 Data Manipulation Language (DML)

Querying Data with Transact-SQL

Relational data model

SQL: Data Definition Language. csc343, Introduction to Databases Diane Horton Fall 2017

Carnegie Mellon Univ. Dept. of Computer Science /615 - DB Applications. Today's Party. Example Database. Faloutsos/Pavlo CMU /615

ACS-3902 Fall Ron McFadyen 3D21 Slides are based on chapter 5 (7 th edition) (chapter 3 in 6 th edition)

Relational Model and Relational Algebra

An Introduction to Structured Query Language

Lecture 2 SQL. Announcements. Recap: Lecture 1. Today s topic. Semi-structured Data and XML. XML: an overview 8/30/17. Instructor: Sudeepa Roy

Database Management Systems. Chapter 5

Introduction to SQL Part 1 By Michael Hahsler based on slides for CS145 Introduction to Databases (Stanford)

CIS 330: Applied Database Systems

The Relational Model. Chapter 3

An Introduction to Structured Query Language

SQL functions fit into two broad categories: Data definition language Data manipulation language

An Introduction to Structured Query Language

Slicing and Dicing Data in CF and SQL: Part 1

For the Dorm Energy Monitoring DB design, some constraints are given and some are absent. You are asked to fill in some of these absent constraints.

The Relational Model. Relational Data Model Relational Query Language (DDL + DML) Integrity Constraints (IC)

Review. The Relational Model. Glossary. Review. Data Models. Why Study the Relational Model? Why use a DBMS? OS provides RAM and disk

CSC 742 Database Management Systems

The SQL Query Language. Creating Relations in SQL. Referential Integrity in SQL

An Introduction to Structured Query Language

Multisets and Duplicates. SQL: Duplicate Semantics and NULL Values. How does this impact Queries?

More SQL: Complex Queries, Triggers, Views, and Schema Modification

Goals for Today. CS 133: Databases. Relational Model. Multi-Relation Queries. Reason about the conceptual evaluation of an SQL query

CSEN 501 CSEN501 - Databases I

CompSci 516 Database Systems. Lecture 2 SQL. Instructor: Sudeepa Roy

SQL: Queries, Programming, Triggers. Basic SQL Query. Conceptual Evaluation Strategy. Example of Conceptual Evaluation. A Note on Range Variables

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

Administrivia. The Relational Model. Review. Review. Review. Some useful terms

Querying Data with Transact-SQL

Textbook: Chapter 4. Chapter 5: Intermediate SQL. CS425 Fall 2016 Boris Glavic. Chapter 5: Intermediate SQL. View Definition.

CS 2316 Exam 3. Practice. Failure to properly fill in the information on this page will result in a deduction of up to 5 points from your exam score.

Sql Server Syllabus. Overview

Database Applications (15-415)

Querying Data with Transact-SQL (20761)

MANAGING DATA(BASES) USING SQL (NON-PROCEDURAL SQL, X401.9)

The Relational Model. Week 2

Ryan Stephens. Ron Plew Arie D. Jones. Sams Teach Yourself FIFTH EDITION. 800 East 96th Street, Indianapolis, Indiana, 46240

Querying Microsoft SQL Server 2014

CS425 Fall 2017 Boris Glavic Chapter 5: Intermediate SQL

Database Management Systems. Chapter 3 Part 1

Basic SQL. Basic SQL. Basic SQL

Transcription:

Advanced SQL 1 / 23

NULL The special value NULL could mean: Unknown Unavailable Not Applicable 2 / 23

Three-Valued Logic - AND AND TRUE FALSE UNKNOWN TRUE TRUE FALSE UNKNOWN FALSE FALSE FALSE FALSE UNKNOWN UNKNOWN FALSE UNKNOWN 3 / 23

Three-Valued Logic - OR OR TRUE FALSE UNKNOWN TRUE TRUE TRUE TRUE FALSE TRUE FALSE UNKKNOWN UNKNOWN TRUE UNKNOWN UNKNOWN 4 / 23

Three-Valued Logic - NOT NOT TRUE FALSE UNKNOWN TRUE FALSE TRUE UNKNOWN 5 / 23

Comparisons with NULL Values Each NULL is distinct, so comparisons with <, >, and = don t make sense. To compare with null, use SQL operator IS, e.g., "Which books don t have editors?": SELECT * FROM book WHERE editor IS NULL ; Inner joins include only tuples for which the join condition evaluates to TRUE. 6 / 23

The IN Operator mysql > select * from book where month in ( April, July ); + -- -------+------------+-------+------+--------+ book_id book_title month year editor + -- -------+------------+-------+------+--------+ 1 CACM April 1960 8 2 CACM July 1974 8 3 BST July 1948 2 7 AAAI July 2012 9 8 NIPS July 2012 9 + -- -------+------------+-------+------+--------+ 5 rows in set (0.00 sec ) 7 / 23

Nested Queries, a.k.a., Sub-Selects List all the books published in the same month in which an issue of CACM was published. mysql > select book_title, month -> from book -> where month in ( select month -> from book where book_title = CACM ); + -- ----------+-------+ book_title month + -- ----------+-------+ CACM April CACM July BST July AAAI July NIPS July + -- ----------+-------+ 5 rows in set (0.00 sec ) 8 / 23

Extended Example 1: Which dorms have fewer occupants than Caldwell? Step 1: how many occupants in Caldwell? mysql > select count (*) as caldwell_occupancy -> from dorm join student using ( dorm_id ) -> where dorm. name = caldwell ; + -- ------------------+ caldwell_occupancy + -- ------------------+ 4 + -- ------------------+ 1 row in set (0.00 sec ) 9 / 23

Occupancy Less than Caldwell Now we use the previous "caldwell_occupancy" query as a subquery. mysql > select dorm. name as dorm_name, count (*) as occupancy -> from dorm join student using ( dorm_id ) -> group by dorm_name -> having occupancy < ( select count (*) as caldwell_occupancy -> from dorm join student using ( dorm_id ) -> where dorm. name = caldwell ); + -- ---------+-----------+ dorm_name occupancy + -- ---------+-----------+ Armstrong 3 Brown 3 + -- ---------+-----------+ 2 rows in set (0.00 sec ) Notice that we couldn t use a where clause here because occupancy is computed from a group, which isn t available at the WHERE stage of the SQL SELECT pipeline. 10 / 23

Extended Example 2: Which dorm has the highest average GPA? Step 1: Group students and their GPAs by dorm. Step 2: Get the average GPAs of each dorm. Step 3: Get the max avg GPA from step 2. 11 / 23

Step 1: Group students and their GPAs by dorm mysql > select dorm. name as dorm_name, student. name as student_name, gpa -> from dorm join student using ( dorm_id ) -> group by dorm_name, student_name, gpa ; + -- ---------+--------------+------+ dorm_name student_name gpa + -- ---------+--------------+------+ Armstrong Alice 3.6 Armstrong Bob 2.7 Armstrong Cheng 3.9 Brown Dhruv 3.4 Brown Ellie 4 Brown Fong 2.3 Caldwell Gerd 4 Caldwell Hal 2.2 Caldwell Isaac 2 Caldwell Jacque 5 + -- ---------+--------------+------+ 10 rows in set (0.00 sec ) 12 / 23

Step 2: Get the average GPAs of each dorm. mysql > select dorm. name as dorm_name, avg ( gpa ) as average_gpa -> from dorm join student using ( dorm_id ) -> group by dorm_name ; + -- ---------+--------------------+ dorm_name average_gpa + -- ---------+--------------------+ Armstrong 3.400000015894572 Brown 3.2333333492279053 Caldwell 3.300000011920929 + -- ---------+--------------------+ 3 rows in set (0.00 sec ) 13 / 23

Step 2.1 Formatting Numeric Values mysql > select dorm. name as dorm_name, format ( avg ( gpa ), 2) as average_gpa -> from dorm join student using ( dorm_id ) -> group by dorm_name ; + -- ---------+-------------+ dorm_name average_gpa + -- ---------+-------------+ Armstrong 3.40 Brown 3.23 Caldwell 3.30 + -- ---------+-------------+ 3 rows in set (0.01 sec ) 14 / 23

FORMAT(x,d[,locale]) Formats the number x to d decimals using a format like nn,nnn.nnn and returns the result as a string. If d is 0, the result has no decimal point or fractional part. locale defaults to the value of the lc_time_names system variable. mysql > select @@lc_time_names ; + -- ---------------+ @@lc_time_names + -- ---------------+ en_us + -- ---------------+ 1 row in set (0.00 sec ) 15 / 23

Step 3: Get max average gpa from average gpa results. Using a nested query: mysql > select dorm_name, max ( average_gpa ) as max_average_gpa -> from ( select dorm. name as dorm_name, format ( avg ( gpa ), 2) as average_gpa -> from dorm join student using ( dorm_id ) -> group by dorm_name ) as avg_gpas ; + -- ---------+-----------------+ dorm_name max_average_gpa + -- ---------+-----------------+ Armstrong 3.40 + -- ---------+-----------------+ 1 row in set (0.00 sec ) 16 / 23

Semantic Constraints The relational model can only encode: Domain constraints Key constraints Foreign key constraints We call constraints on arbitrary values in tuples within and between relations semantic constraints. While the relational model has no concept of semantic constraints, SQL can handle semantic constraints with assertions and triggers. 17 / 23

Assertions CREATE ASSERTION <assertion - name > CHECK (< condition >) <condition> can be any SQL staetment that evaluates to TRUE, FALSE or UNKNOWN. Any databases INSERT or UPDATE that couses the <condition> to be FALSE is rejected by the database engine. While CREATE ASSERTION is part of the SQL standard, no major DBMS today (including MySQL) implements assertions. 18 / 23

Triggers A trigger is a piece of SQL code associated with a table that executes when an event occurs on the table. A trigger can t directly prevent an INSERT or UPDATE from occuring due to a semantic oncstraint violation, but a trigger can modify values being inserted or pudated to avoid the violation or log the violation in a message table or execute a stored procedure or external program. 19 / 23

The Three-Schema Architecture Remember the three-schema architecture? 20 / 23

Views mysql > create view cacm_issues as -> select * from book -> where book_title = CACM ; Query OK, 0 rows affected (0.00 sec ) The CREATE VIEW statement is the mapping between the internal schema (base tables) and the external schema(s) (derived tables). They re all part of the database: mysql > show tables ; + -- --------------+ Tables_in_pubs + -- --------------+ author author_pub book cacm_issues pub + -- --------------+ 5 rows in set (0.00 sec ) 21 / 23

A View is Like a Table You can get data from the table: mysql > select * from cacm_issues ; + -- -------+------------+-------+------+--------+ book_id book_title month year editor + -- -------+------------+-------+------+--------+ 1 CACM April 1960 8 2 CACM July 1974 8 + -- -------+------------+-------+------+--------+ 2 rows in set (0.00 sec ) And you can update data in the view, which modifies the underlying base tables. 22 / 23

Users and Permissions The value of external schemas is that they can give specific users customized vies of the database. We do this with permissions: 23 / 23