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

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

Introduction to Data Management CSE 414

Lecture 3 Additional Slides. CSE 344, Winter 2014 Sudeepa Roy

Introduction to Database Systems CSE 444

Announcements. Outline UNIQUE. (Inner) joins. (Inner) Joins. Database Systems CSE 414. WQ1 is posted to gradebook double check scores

Introduction to Database Systems CSE 414. Lecture 3: SQL Basics

Introduction to Database Systems CSE 414. Lecture 3: SQL Basics

Database Systems CSE 414

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

Introduction to Data Management CSE 344

Introduction to Database Systems CSE 414

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

CSE 344 JANUARY 5 TH INTRO TO THE RELATIONAL DATABASE

Lecture 2: Introduction to SQL

CSE 344 JANUARY 8 TH SQLITE AND JOINS

Database Systems CSE 414

Introduction to Databases CSE 414. Lecture 2: Data Models

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

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

Announcements. Subqueries. Lecture Goals. 1. Subqueries in SELECT. Database Systems CSE 414. HW1 is due today 11pm. WQ1 is due tomorrow 11pm

SELECT Product.name, Purchase.store FROM Product JOIN Purchase ON Product.name = Purchase.prodName

Introduction to Data Management CSE 344. Lecture 2: Data Models

Introduction to Data Management CSE 344

Announcements. Joins in SQL. Joins in SQL. (Inner) joins. Joins in SQL. Introduction to Database Systems CSE 414

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

Introduction to Database Systems CSE 414

CSE 344 JANUARY 19 TH SUBQUERIES 2 AND RELATIONAL ALGEBRA

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

Lecture 3: SQL Part II

Introduction to Database Systems CSE 414

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

Introduction to Data Management CSE 344

MTAT Introduction to Databases

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

CSE 544 Principles of Database Management Systems

CSE 544 Principles of Database Management Systems

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

Please pick up your name card

COMP 430 Intro. to Database Systems

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

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

CSE 344 Introduction to Data Management. Section 2: More SQL

Announcements. Using Electronics in Class. Review. Staff Instructor: Alvin Cheung Office hour on Wednesdays, 1-2pm. Class Overview

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

Before we talk about Big Data. Lets talk about not-so-big data

Introduction to Data Management CSE 344

L07: SQL: Advanced & Practice. CS3200 Database design (sp18 s2) 1/11/2018

Announcements. Agenda. Database/Relation/Tuple. Schema. Discussion. CSE 444: Database Internals

SQL. Assit.Prof Dr. Anantakul Intarapadung

COMP 430 Intro. to Database Systems

Where Are We? Next Few Lectures. Integrity Constraints Motivation. Constraints in E/R Diagrams. Keys in E/R Diagrams

CSC 261/461 Database Systems Lecture 5. Fall 2017

Basic SQL. Basic SQL. Basic SQL

Database Systems CSE 414

Database Systems CSE 303. Lecture 02

Database Systems CSE 303. Lecture 02

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

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

Principles of Database Systems CSE 544. Lecture #1 Introduction and SQL

Database Systems CSE 414

Chapter 3. Introduction to relational databases and MySQL. 2010, Mike Murach & Associates, Inc. Murach's PHP and MySQL, C3

CS 564 Midterm Review

Subqueries. 1. Subqueries in SELECT. 1. Subqueries in SELECT. Including Empty Groups. Simple Aggregations. Introduction to Database Systems CSE 414

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

Database Systems CSE 414

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

HW1 is due tonight HW2 groups are assigned. Outline today: - nested queries and witnesses - We start with a detailed example! - outer joins, nulls?

Lesson 2. Data Manipulation Language

Database Systems CSE 414

618 Index. BIT data type, 108, 109 BIT_LENGTH, 595f BIT VARYING data type, 108 BLOB data type, 108 Boolean data type, 109

Database Systems CSE 414

Lectures 2&3: Introduction to SQL

Querying Data with Transact SQL

THE UNIVERSITY OF AUCKLAND

Introduction to relational databases and MySQL

Introduction to Database Systems CSE 414. Lecture 8: SQL Wrap-up

CSC 261/461 Database Systems Lecture 7

SQL Functionality SQL. Creating Relation Schemas. Creating Relation Schemas

Introduction to Database Systems CSE 344

Introduction to Database Systems CSE 344

Relational Algebra and SQL

SQL (Structured Query Language)

Database Design Process Entity / Relationship Diagrams

CS 327E Lecture 2. Shirley Cohen. January 27, 2016

Implementing Table Operations Using Structured Query Language (SQL) Using Multiple Operations. SQL: Structured Query Language

Intermediate SQL ( )

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

GridDB Advanced Edition SQL reference

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

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

SQL DATA DEFINITION: KEY CONSTRAINTS. CS121: Relational Databases Fall 2017 Lecture 7

Introduction to Database Systems

SQL Structured Query Language Introduction

CS 464/564 Introduction to Database Management System Instructor: Abdullah Mueen

Chapter # 7 Introduction to Structured Query Language (SQL) Part II

Assignment 6: SQL III Solution

Why Relational Databases? Relational databases allow for the storage and analysis of large amounts of data.

Principles of Data Management

SQLite, Firefox, and our small IMDB movie database. CS3200 Database design (sp18 s2) Version 1/17/2018

Announcements. JSon Data Structures. JSon Syntax. JSon Semantics: a Tree! JSon Primitive Datatypes. Introduction to Database Systems CSE 414

Transcription:

Introduction to Data Management CSE 414 Announcements Reminder: first web quiz due Sunday Lecture 3: More SQL (including most of Ch. 6.1-6.2) CSE 414 - Spring 2017 1 CSE 414 - Spring 2017 2 Multi-column Keys This makes name a key: name VARCHAR(20) PRIMARY KEY, for_profit BOOLEAN); How can we make a key on name & country? Multi-column Keys Syntax change if a primary key has multiple columns: name VARCHAR(20) PRIMARY KEY, for_profit BOOLEAN, PRIMARY KEY (name, country)); goes away CSE 414 - Spring 2017 3 CSE 414 - Spring 2017 4 Multi-column Keys (2) Multi-column Keys (3) Likewise for secondary keys: name VARCHAR(20) UNIQUE, for_profit BOOLEAN, UNIQUE (name, country)); goes away This makes manufacturer a foreign key: CREATE TABLE Product( name VARCHAR(20), good idea to include price DECIMAL(10,2), target column name manufacturer VARCHAR(20) REFERENCES Company(name)); CSE 414 - Spring 2017 5 CSE 414 - Spring 2017 6 1

Multi-column Keys (3) Similar syntax for foreign keys: CREATE TABLE Product( name VARCHAR(20), now need both price DECIMAL(10,2), name & country manu_name VARCHAR(20), manu_co VARCHAR(20), FOREIGN KEY (manu_name, manu_co) REFERENCES Company(name, country)); One Way to Input Data Write a program that outputs SQL statements: for (int a = 1; a <= 50; a++) for (int b = 1; b <= 50; b++) INSERT INTO T VALUES (%d,%d);\n, a, b); Feed those into SQLite: sqlite3 foo.db < inputs.sql CSE 414 - Spring 2017 7 CSE 414 - Spring 2017 8 Warning Be very careful when doing this with strings: Demo: MyTriples.java INSERT INTO T2 VALUES (%d, %s );, 3, O Shaughnessy ); Becomes: INSERT INTO T2 VALUES (3, O Shaughnessy ); which is a syntax error in this case CSE 414 - Spring 2017 9 CSE 414 - Spring 2017 10 Warning (cont) Be very careful when doing this with strings: INSERT INTO T VALUES (%d, %s );, 3, O Shaughnessy ); https://xkcd.com/327/ CSE 414 - Spring 2017 11 This allows a SQL injection attack! Must check for quotes and escape (or disallow) them. We ll see safer ways to do this using JDBC DBMSs usually have faster ways to input data SQLite has.import (try with.mode csv) CSE 414 - Spring 2017 12 2

SQLite is just a library SQLite Uses Can be used as part of any C/C++/Java program ex: could be used in an iphone app Can be used in Chrome & Safari no support in Firefox or IE Demo: websql.html (Note: this HTML/JS code is out of class scope) Also selection & projection examples (see lec03-sql-basics.sql) CSE 414 - Spring 2017 13 CSE 414 - Spring 2017 14 Physical Data Independence SQL doesn t specify how data is stored on disk No need to think about encodings of data types ex: DECIMAL(10,2) ex: VARCHAR(255) does this need to use 255 bytes to store hello? No need to think about how tuples are arranged ex: could be row- or column-major ordered (Most DBMSs are row-ordered but BigQuery is column.) Allows NULL keys SQLite Gotchas Does not support boolean or date/time columns Doesn t always enforce domain constraints! will let you insert a string where an INT is expected Doesn t enforce foreign key constraints by default Etc CSE 414 - Spring 2017 15 CSE 414 - Spring 2017 16 DISTINCT and ORDER BY Query results do not have to be relations i.e., they can have duplicate rows remove them using DISTINCT Result order is normally unspecified choose an order using ORDER BY e.g., ORDER BY country, cname e.g., ORDER BY price ASC, pname DESC Examples in lec03-sql-basics.sql Joins Can use data from multiple tables: SELECT pname, price FROM Product, Company WHERE manufacturer = cname AND country = AND price < 150; This is a selection and projection of the join of the Product and Company relations. CSE 414 - Spring 2017 17 CSE 414 - Spring 2017 18 3

SingleTouch 149.99 SingleTouch 149.99 PowerGizmo 29.99 PowerGizmo 29.99 (,, SingleTouch, 149.99, ) (,, Gizmo, 19.99, ) CSE 414 - Spring 2017 19 CSE 414 - Spring 2017 20 SingleTouch 149.99 SingleTouch 149.99 PowerGizmo 29.99 PowerGizmo 29.99 (,, PowerGizmo, 29.99, ) (,, SingleTouch, 149.99, ) CSE 414 - Spring 2017 21 CSE 414 - Spring 2017 22 SingleTouch 149.99 SingleTouch 149.99 PowerGizmo 29.99 PowerGizmo 29.99 (,, Gizmo, 19.99, ) (,, PowerGizmo, 29.99, ) CSE 414 - Spring 2017 23 CSE 414 - Spring 2017 24 4

JOIN SingleTouch 149.99 PowerGizmo 29.99 This join produces 6 different rows in general, # rows in join is (# rows in A) * (# rows in B) number of rows often much smaller after selection DBMS will do everything in it s power to not compute A JOIN B CSE 414 - Spring 2017 25 (2) Can think of a join in terms of code: for every row C in Company { for every row P in Product { if (P.manufacturer = C.cname and C.country = and P.price < 150.00) output (C.cname, C.country, P.pname, P.price, P.category, P.manufacturer); } CSE 414 - Spring 2017 26 } Types of Joins Join Examples We usually think of the selection as part of the join e.g., manufacturer = cname and country = and called the join predicate See lec03-sql-basics.sql Join without a predicate is cross product / cross join Special names depending on predicate natural join if = between pairs of columns with same name with well chosen col names, many joins become natural These are inner joins. We will discuss outer later CSE 414 - Spring 2017 27 CSE 414 - Spring 2017 28 5