COMP 430 Intro. to Database Systems

Similar documents
DS Introduction to SQL Part 1 Single-Table Queries. By Michael Hahsler based on slides for CS145 Introduction to Databases (Stanford)

Lecture 2: Introduction to SQL

Database Systems CSE 303. Lecture 02

Database Systems CSE 303. Lecture 02

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

COMP 430 Intro. to Database Systems

Big Data Processing Technologies. Chentao Wu Associate Professor Dept. of Computer Science and Engineering

MTAT Introduction to Databases

Lecture 3: SQL Part II

SQL Functionality SQL. Creating Relation Schemas. Creating Relation Schemas

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

Chapter 3: Introduction to SQL

Chapter 3: Introduction to SQL

SQL: Data Definition Language

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

Information Systems Engineering. SQL Structured Query Language DDL Data Definition (sub)language

CSE 344 JANUARY 5 TH INTRO TO THE RELATIONAL DATABASE

QQ Group

Introduction to Database Systems CSE 444

SQL DATA DEFINITION LANGUAGE

Operating systems fundamentals - B07

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

INF 212 ANALYSIS OF PROG. LANGS SQL AND SPREADSHEETS. Instructors: Crista Lopes Copyright Instructors.

SQL DATA DEFINITION LANGUAGE

user specifies what is wanted, not how to find it

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

SQL Structured Query Language Introduction

SQL OVERVIEW. CS121: Relational Databases Fall 2017 Lecture 4

Chapter 4. Basic SQL. SQL Data Definition and Data Types. Basic SQL. SQL language SQL. Terminology: CREATE statement

SQL DATA DEFINITION LANGUAGE

CS143: Relational Model

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

CMPT 354: Database System I. Lecture 2. Relational Model

Basic SQL. Basic SQL. Basic SQL

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

The Relational Model of Data (ii)

Keys, SQL, and Views CMPSCI 645

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

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

Introduction to Data Management CSE 414

CSE 344 JANUARY 8 TH SQLITE AND JOINS

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

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

Introduction to Databases CSE 414. Lecture 2: Data Models

Outline. Textbook Chapter 6. Note 1. CSIE30600/CSIEB0290 Database Systems Basic SQL 2

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

SQL. Assit.Prof Dr. Anantakul Intarapadung

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

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

Principles of Data Management

CSIE30600 Database Systems Basic SQL 2. Outline

Database Systems SQL SL03

HOW TO CREATE AND MAINTAIN DATABASES AND TABLES. By S. Sabraz Nawaz Senior Lecturer in MIT FMC, SEUSL

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

DATABASE TECHNOLOGY - 1MB025

UFCEKG 20 2 : Data, Schemas and Applications

Part I: Structured Data

Principles of Database Systems CSE 544. Lecture #2 SQL The Complete Story

Database Systems SQL SL03

DATABASTEKNIK - 1DL116

DS Introduction to SQL Part 2 Multi-table Queries. By Michael Hahsler based on slides for CS145 Introduction to Databases (Stanford)

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

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

Slides by: Ms. Shree Jaswal

Chapter 4: SQL. Basic Structure

SQL Data Definition Language: Create and Change the Database Ray Lockwood

COGS 121 HCI Programming Studio. Week 03 - Tech Lecture

EGCI 321: Database Systems. Dr. Tanasanee Phienthrakul

Relational Algebra and SQL

Explain in words what this relational algebra expression returns:

CS 582 Database Management Systems II

Announcements. Multi-column Keys. Multi-column Keys (3) Multi-column Keys. Multi-column Keys (2) Introduction to Data Management CSE 414

1) Introduction to SQL

SQL: The Query Language Part 1. Relational Query Languages

The SQL database language Parts of the SQL language

Daffodil DB. Design Document (Beta) Version 4.0

SQL: Data Manipulation Language. csc343, Introduction to Databases Diane Horton Winter 2017

SQL: csc343, Introduction to Databases Renée J. Miller and Fatemeh Nargesian and Sina Sina Meraji. Winter 2018

Introduction to SQL. ECE 650 Systems Programming & Engineering Duke University, Spring 2018

IT 3203 Introduction to Web Development

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

Announcements. Multi-column Keys. Multi-column Keys. Multi-column Keys (3) Multi-column Keys (2) Introduction to Data Management CSE 414

SQL Data Definition and Data Manipulation Languages (DDL and DML)

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

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

An Introduction to Structured Query Language

Review: Where have we been?

Introduction to Data Management CSE 344

Polls on Piazza. Open for 2 days Outline today: Next time: "witnesses" (traditionally students find this topic the most difficult)

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

An Introduction to Structured Query Language

CS425 Fall 2017 Boris Glavic Chapter 4: Introduction to SQL

Lab # 4. Data Definition Language (DDL)

An Introduction to Structured Query Language

D B M G. SQL language: basics. Managing tables. Creating a table Modifying table structure Deleting a table The data dictionary Data integrity

ColdFusion Summit 2016

Introduction to SQL Part 2 by Michael Hahsler Based on slides for CS145 Introduction to Databases (Stanford)

DATABASE TECHNOLOGY - 1MB025

Chapter 1: Introduction

2.9 Table Creation. CREATE TABLE TableName ( AttrName AttrType, AttrName AttrType,... )

Transcription:

SELECT name FROM sqlite_master WHERE type='table' COMP 430 Intro. to Database Systems Single-table SQL Get clickers today! Slides use ideas from Chris Ré and Chris Jermaine.

Clicker test Have you used clickers before? A. True B. False 0% 0%

What is Structured Query Language (SQL)? Standard language for databases Many related standards: ANSI SQL, SQL92 (= SQL2), SQL99 (= SQL3) Vendors support various subsets and extensions Very high-level programming language Highly optimized, parallelized Huge language We ll cover only a core subset. Typically called from main application language (Python, Java, ) SQLexecute( SQL CODE )

Two languages in one Data Definition Language (DDL) Define relational schemas Create/modify/delete tables and their attributes declarative imperative Data Manipulation Language (DML) Query DB for desired data Add/modify/delete data in tables Define/use functions, procedures, triggers declarative imperative imperative

Basic concepts

Table=Relation Heading: Names of each column. Product p_name price manufacturer Gizmo $19.99 GizmoWorks Powergizmo $39.99 GizmoWorks Widget $19.99 WidgetsRUs HyperWidget $203.99 Hyper Row=tuple=record: A single entry in the table. #rows = cardinality Column=attribute=field: A typed data entry in each row. #columns = arity=degree

Table=Relation Product p_name price manufacturer Gizmo $19.99 GizmoWorks Powergizmo $39.99 GizmoWorks Widget $19.99 WidgetsRUs HyperWidget $203.99 Hyper Table is a multiset of rows. Not ordered. Duplicate rows allowed.

Schema defines table attributes Product p_name price manufacturer Gizmo $19.99 GizmoWorks Powergizmo $39.99 GizmoWorks Widget $19.99 WidgetsRUs HyperWidget $203.99 Hyper Product (p_name: string, price: float, manufacturer: string)

Creating a table Product (p_name: string, price: float, manufacturer: string) CREATE TABLE Product ( p_name VARCHAR(50), price NUMERIC(6,2), manufacturer VARCHAR(50) ); Some SQL variants have a CURRENCY or MONEY type.

SQL attribute types All types are atomic! Examples: CHAR(n), VARCHAR(n), NCHAR(n), NVARCHAR(n) INT, BIGINT, SMALLINT, FLOAT, DECIMAL(m,n) BOOLEAN, MONEY, DATETIME Traditionally. However, sets and arrays are allowed in some SQL versions.

NULL data Any value of any type can be NULL. Signals missing value. Value doesn t exist. Value exists but unknown. Value not applicable to this record.

Keys Key = minimal set of attributes acting as a unique tuple identifier. Consider the universe of all potential relation data, not just what the table currently contains. Product p_name price manufacturer Gizmo 19.99 GizmoWorks Powergizmo 39.99 GizmoWorks Widget 19.99 WidgetsRUs HyperWidget 203.99 Hyper

What is key? Response Counter 25% 25% 25% 25% A. dept B. dept, number C. dept, number, section D. dept, number, section, instructor Course dept number section instructor MATH 101 1 Jones MATH 101 2 Smith MATH 102 1 Williams PHYS 101 1 Baker PHYS 102 2 Baker dept dept, number t, number, section section, instructor

What is key? Response Counter 25% 25% 25% 25% A. s_name B. abbrev C. The pair s_name, abbrev D. Each of s_name and abbrev State s_name abbrev year_admitted Alabama AL 1819 Alaska AK 1959 Arizona AZ 1912 Arkansas AR 1836 California CA 1850 s_name abbrev name, abbrev e and abbrev

Primary keys Every table should have one primary key. Each tuple must have distinct non-null values of primary key attributes. Guarantees table is a mathematical relation. Product p_name price manufacturer Gizmo 19.99 GizmoWorks Powergizmo 39.99 GizmoWorks Widget 19.99 WidgetsRUs HyperWidget 203.99 Hyper Product (p_name: string, price: float, manufacturer: string)

Primary keys are a DB-checked constraint Adding a new record with duplicate or NULL primary key will fail. Failure reported as exception or erroneous return value. Product p_name price manufacturer Product (p_name: string, price: float, manufacturer: string) Gizmo 19.99 GizmoWorks Powergizmo 39.99 GizmoWorks Widget 19.99 WidgetsRUs HyperWidget 203.99 Hyper Widget 15.99 GizmoWorks We will see other kinds of DB-checked constraints.

Creating a table with primary key Product (p_name: string, price: float, manufacturer: string) CREATE TABLE Product ( p_name VARCHAR(50), price DECIMAL(6,2), manufacturer VARCHAR(50), PRIMARY KEY (p_name) );

NOT NULL constraint Course (c_id: integer, c_name: string, instructor: string) CREATE TABLE Course ( c_id INTEGER, c_name VARCHAR(50) NOT NULL, instructor VARCHAR(50), PRIMARY KEY (c_id) );

Adding a single record to a table INSERT INTO Product VALUES ( MiniWidget, 21.99, WidgetsRUs ); INSERT INTO Product (p_name, manufacturer) VALUES ( NanoWidget, WidgetsRUs ); Product p_name price manufacturer Gizmo 19.99 GizmoWorks Powergizmo 39.99 GizmoWorks Widget 19.99 WidgetsRUs HyperWidget 203.99 Hyper Product p_name price manufacturer Gizmo 19.99 GizmoWorks Powergizmo 39.99 GizmoWorks Widget 19.99 WidgetsRUs HyperWidget 203.99 Hyper MiniWidget 21.99 WidgetsRUs NanoWidget NULL WidgetsRUs

Activity: Creating a table 02a-table.ipynb Course dept number section instructor MATH 101 1 Jones MATH 101 2 Smith MATH 102 1 Williams PHYS 101 1 Baker PHYS 102 2 Baker CREATE TABLE Course ( ); INSERT INTO Course ; Course (dept, number, section, instructor)

Activity partial solution Course (dept, number, section, instructor) CREATE TABLE Course ( dept CHAR(4), number CHAR(3), section INT DEFAULT 1, instructor VARCHAR(50), PRIMARY KEY (dept, number, section) ); Convenient for this example.

Simple queries

SELECT-FROM-WHERE SELECT attributes FROM tables WHERE conditions Results in a new table, which can be returned or stored.

Selecting some records SELECT * FROM Product WHERE Price > 20; Product p_name price manufacturer Gizmo 19.99 GizmoWorks Powergizmo 39.99 GizmoWorks p_name price manufacturer Powergizmo 39.99 GizmoWorks HyperWidget 203.99 Hyper Widget 19.99 WidgetsRUs HyperWidget 203.99 Hyper

More selection examples SELECT * FROM Product WHERE price IS NOT NULL; SELECT * FROM Product WHERE price BETWEEN 20 AND 40; SELECT * FROM Product WHERE price > 20 AND manufacturer = GizmoWorks ; SELECT * FROM Product WHERE manufacturer IN ( GizmoWorks, WidgetsRUs );

Selection with pattern matching SELECT * FROM Product WHERE p_name LIKE %Gizmo% ; % = Match any sequence of 0-or-more characters _ = Match any single character [abc] = Match any one character listed [a-c] = Match any one character in range

Projecting some fields SELECT p_name, manufacturer FROM Product; Product p_name price manufacturer Gizmo 19.99 GizmoWorks Powergizmo 39.99 GizmoWorks Widget 19.99 WidgetsRUs HyperWidget 203.99 Hyper p_name Gizmo Powergizmo Widget HyperWidget manufacturer GizmoWorks GizmoWorks WidgetsRUs Hyper Answer (p_name, manufacturer)

Combining selection & projection SELECT p_name, manufacturer FROM Product WHERE price > 20; Product p_name price manufacturer Gizmo 19.99 GizmoWorks Powergizmo 39.99 GizmoWorks Widget 19.99 WidgetsRUs HyperWidget 203.99 Hyper p_name Powergizmo HyperWidget manufacturer GizmoWorks Hyper

Results not necessarily distinct SELECT manufacturer FROM Product; Product p_name price manufacturer Gizmo 19.99 GizmoWorks Powergizmo 39.99 GizmoWorks Widget 19.99 WidgetsRUs HyperWidget 203.99 Hyper manufacturer GizmoWorks GizmoWorks WidgetsRUs Hyper Tables are multisets! Answer (manufacturer)

Making results distinct SELECT DISTINCT manufacturer FROM Product; Product p_name price manufacturer Gizmo 19.99 GizmoWorks Powergizmo 39.99 GizmoWorks Widget 19.99 WidgetsRUs HyperWidget 203.99 Hyper manufacturer GizmoWorks WidgetsRUs Hyper Ensures results are a set.

Query semantics set notation SELECT [DISTINCT] a 1, a 2,, a m FROM T WHERE Conditions(a 1, a 2,, a p ); {(a 1, a 2,, a m ) Conditions(a 1, a 2,, a p )} Multisets by default. Sets with DISTINCT.

Query semantics sequence of ops SELECT [DISTINCT] a 1, a 2,, a m FROM T WHERE Conditions(a 1, a 2,, a p ); Answer = {} Multiset union by default. for row in T do Set union with DISTINCT. if Conditions(row.a 1, row.a 2,, row.a p ) then Answer = Answer {(row.a 1, row.a 2,, row.a m )} return Answer

Subset of results SELECT p_name, manufacturer FROM Product LIMIT 2; Which 2 is implementationdependent. Product p_name price manufacturer Gizmo 19.99 GizmoWorks Powergizmo 39.99 GizmoWorks Widget 19.99 WidgetsRUs HyperWidget 203.99 Hyper Gizmo p_name Powergizmo manufacturer GizmoWorks GizmoWorks

Computation in SELECT clause Product SELECT p_name, IsNull(price, 0) AS price FROM Product; p_name price manufacturer p_name price Gizmo 19.99 GizmoWorks Powergizmo 39.99 GizmoWorks Widget 19.99 WidgetsRUs HyperWidget 203.99 Hyper MiniWidget 21.99 WidgetsRUs NanoWidget NULL WidgetsRUs Gizmo 19.99 Powergizmo 39.99 Widget 19.99 HyperWidget 203.99 MiniWidget 21.99 NanoWidget 0

More computation in SELECT clauses SELECT location, time, celsius * 1.8 + 32 AS fahrenheit FROM SensorReading; SELECT player_id, Floor(height) AS feet, (height Floor(height)) * 12 AS inches FROM Player; Use AS. Without it, SQL will create a default field name.

Sorting SELECT p_name, manufacturer FROM Product ORDER BY price DESC, manufacturer; Product p_name price manufacturer Gizmo 19.99 GizmoWorks Powergizmo 39.99 GizmoWorks Widget 19.99 WidgetsRUs HyperWidget 203.99 Hyper p_name HyperWidget Powergizmo Gizmo Widget manufacturer Hyper GizmoWorks GizmoWorks WidgetsRUs

Sorting vs. Multiset semantics Table rows are unordered, except when they re ordered. More accurately, unless you use ORDER BY: Can t assume anything about ordering. Ordering depends on implementation, which can vary. Queries don t necessarily maintain order of original table.

Activity: Writing queries 02b-queries.ipynb

Some details

NULL semantics NULL is not a value. It is the lack of a value. In numeric operations: f(null) NULL In Boolean operations, we use 3-value logic (FALSE, UNKNOWN, TRUE): NULL = Houston UNKNOWN NULL = NULL UNKNOWN A F NOT A T A AND B B F U T A OR B B F U T U U F F F F F F U T T F A U F U U A U U U T T F U T T T T T

Syntax details Strings use single quotes, not double Houston Equality test uses single =, not double x = 5 Amount of whitespace doesn t matter

Syntax details case sensitivity Case insensitive Keywords: SELECT, NULL Table names: Product Function names: IsNull() Depends on SQL version Attribute names: product_id Case sensitive String literals: HOUSTON, Houston, houston