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

Similar documents
MariaDB Crash Course. A Addison-Wesley. Ben Forta. Upper Saddle River, NJ Boston. Indianapolis. Singapore Mexico City. Cape Town Sydney.

CGS 3066: Spring 2017 SQL Reference

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

Relational Database Features

Sql Server Syllabus. Overview

user specifies what is wanted, not how to find it

Homework Assignment 2. Due Date: October 21th, :30pm (noon) CS425 - Database Organization Results

Principles of Data Management

Databases (MariaDB/MySQL) CS401, Fall 2015

Course Outline and Objectives: Database Programming with SQL

Index. Bitmap Heap Scan, 156 Bitmap Index Scan, 156. Rahul Batra 2018 R. Batra, SQL Primer,

Databases. Course October 23, 2018 Carsten Witt

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

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

Course Details Duration: 3 days Starting time: 9.00 am Finishing time: 4.30 pm Lunch and refreshments are provided.

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

1) Introduction to SQL

School of Computing and Information Technology. Examination Paper Autumn 2016

Table of Contents. PDF created with FinePrint pdffactory Pro trial version

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

SQL Queries. for. Mere Mortals. Third Edition. A Hands-On Guide to Data Manipulation in SQL. John L. Viescas Michael J. Hernandez

SQL Commands & Mongo DB New Syllabus

SQL: Data Querying. B0B36DBS, BD6B36DBS: Database Systems. h p:// Lecture 4

CS108 Lecture 18: Databases and SQL

Introduction to Computer Science and Business

SQL Data Querying and Views

Relational Database Management Systems for Epidemiologists: SQL Part II

Operating systems fundamentals - B07

Introduction to relational databases and MySQL

COGS 121 HCI Programming Studio. Week 03 - Tech Lecture

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

E-R diagrams and database schemas. Functional dependencies. Definition (tuple, attribute, value). A tuple has the form

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

OVERVIEW OF RELATIONAL DATABASES: KEYS

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

SQL STORED ROUTINES. CS121: Relational Databases Fall 2017 Lecture 9

Midterm Exam. October 30th, :15-4:30. CS425 - Database Organization Results

HKTA TANG HIN MEMORIAL SECONDARY SCHOOL SECONDARY 3 COMPUTER LITERACY. Name: ( ) Class: Date: Databases and Microsoft Access

CS425 Fall 2017 Boris Glavic Chapter 5: Intermediate SQL

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

SQL Functionality SQL. Creating Relation Schemas. Creating Relation Schemas

SQL: DML and Advanced Constructs Insert, Update, Delete, View, Index, Procedure, Transaction, Trigger

Unit Assessment Guide

Find All Tables Containing Column With Specified Name Oracle

1 INTRODUCTION TO EASIK 2 TABLE OF CONTENTS

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

Relational Database Development

Review. Objec,ves. Example Students Table. Database Overview 3/8/17. PostgreSQL DB Elas,csearch. Databases

1. SQL definition SQL is a declarative query language 2. Components DRL: Data Retrieval Language DML: Data Manipulation Language DDL: Data Definition

Midterm Review. Winter Lecture 13

Outline. Introduction to SQL. What happens when you run an SQL query? There are 6 possible clauses in a select statement. Tara Murphy and James Curran

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

Introduction to SQL on GRAHAM ED ARMSTRONG SHARCNET AUGUST 2018

Introduction to SQL. Tara Murphy and James Curran. 15th April, 2009

Chapter 13 : Informatics Practices. Class XI ( As per CBSE Board) SQL Commands. New Syllabus Visit : python.mykvs.in for regular updates

Oracle Database 11g: SQL and PL/SQL Fundamentals

A1 (Part 2): Injection SQL Injection

Oracle Syllabus Course code-r10605 SQL

Introduction. Sample Database SQL-92. Sample Data. Sample Data. Chapter 6 Introduction to Structured Query Language (SQL)

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

PostgreSQL Database and C++ Interface (and Midterm Topics) ECE 650 Systems Programming & Engineering Duke University, Spring 2018

STOP DROWNING IN DATA. START MAKING SENSE! An Introduction To SQLite Databases. (Data for this tutorial at

In-Class Exercise: SQL #2 Putting Information into a Database

INTRODUCTION TO DATABASES IN PYTHON. Creating Databases and Tables

Workbooks (File) and Worksheet Handling

Chapter 4: Intermediate SQL

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

Ghislain Fourny. Information Systems for Engineers 7. The ecosystem around SQL

This course is aimed at those who need to extract information from a relational database system.

Model Question Paper. Credits: 4 Marks: 140

CS348 INFORMATION SYSTEMS

Oracle Exam 1z0-882 Oracle Certified Professional, MySQL 5.6 Developer Version: 7.0 [ Total Questions: 100 ]

INDEX. 1 Basic SQL Statements. 2 Restricting and Sorting Data. 3 Single Row Functions. 4 Displaying data from multiple tables

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

PostgreSQL Built-in Sharding:

ECE 650 Systems Programming & Engineering. Spring 2018

SQL DDL II. CS121: Relational Databases Fall 2017 Lecture 8

Databases and SQL programming overview

Simple queries Set operations Aggregate operators Null values Joins Query Optimization. John Edgar 2

INTERMEDIATE SQL GOING BEYOND THE SELECT. Created by Brian Duffey

CS2300: File Structures and Introduction to Database Systems

DB2 SQL Tuning Tips for z/os Developers

SQL Introduction. CS 377: Database Systems

Querying Data with Transact SQL

Introduction to MySQL /MariaDB and SQL Basics. Read Chapter 3!

PASSWORDS TREES AND HIERARCHIES. CS121: Relational Databases Fall 2017 Lecture 24

SQL: The Query Language Part 1. Relational Query Languages

Oracle Database: SQL and PL/SQL Fundamentals NEW

SQL OVERVIEW. CS121: Relational Databases Fall 2017 Lecture 4

GridDB Advanced Edition SQL reference

Querying Data with Transact-SQL

MySQL Workshop. Scott D. Anderson

CSCI-UA: Database Design & Web Implementation. Professor Evan Sandhaus

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

CSCB20 Week 4. Introduction to Database and Web Application Programming. Anna Bretscher Winter 2017

CS317 File and Database Systems

5. Single-row function

1 Writing Basic SQL SELECT Statements 2 Restricting and Sorting Data

Oracle 1Z0-882 Exam. Volume: 100 Questions. Question No: 1 Consider the table structure shown by this output: Mysql> desc city:

Oracle Database: SQL and PL/SQL Fundamentals Ed 2

Transcription:

CS50 Beyond

Databases

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

SQL

SQL Databases MySQL PostgreSQL SQLite...

Data Types INTEGER DECIMAL SERIAL VARCHAR TIMESTAMP BOOLEAN ENUM...

CREATE TABLE

CREATE TABLE flights ( id SERIAL PRIMARY KEY, origin VARCHAR NOT NULL, destination VARCHAR NOT NULL, duration INTEGER NOT NULL );

CREATE TABLE flights ( id SERIAL PRIMARY KEY, origin VARCHAR NOT NULL, destination VARCHAR NOT NULL, duration INTEGER NOT NULL );

CREATE TABLE flights ( id SERIAL PRIMARY KEY, origin VARCHAR NOT NULL, destination VARCHAR NOT NULL, duration INTEGER NOT NULL );

CREATE TABLE flights ( id SERIAL PRIMARY KEY, origin VARCHAR NOT NULL, destination VARCHAR NOT NULL, duration INTEGER NOT NULL );

CREATE TABLE flights ( id SERIAL PRIMARY KEY, origin VARCHAR NOT NULL, destination VARCHAR NOT NULL, duration INTEGER NOT NULL );

CREATE TABLE flights ( id SERIAL PRIMARY KEY, origin VARCHAR NOT NULL, destination VARCHAR NOT NULL, duration INTEGER NOT NULL );

CREATE TABLE flights ( id SERIAL PRIMARY KEY, origin VARCHAR NOT NULL, destination VARCHAR NOT NULL, duration INTEGER NOT NULL );

Constraints NOT NULL UNIQUE PRIMARY KEY DEFAULT CHECK...

INSERT

INSERT INTO flights (origin, destination, duration) VALUES ('New York', 'London', 415);

INSERT INTO flights (origin, destination, duration) VALUES ('New York', 'London', 415);

INSERT INTO flights (origin, destination, duration) VALUES ('New York', 'London', 415);

INSERT INTO flights (origin, destination, duration) VALUES ('New York', 'London', 415);

INSERT INTO flights (origin, destination, duration) VALUES ('New York', 'London', 415);

INSERT INTO flights (origin, destination, duration) VALUES ('New York', 'London', 415);

SELECT

SELECT * FROM flights; id origin destination duration 1 New York London 415 2 Shanghai Paris 760 3 Istanbul Tokyo 700 4 New York Paris 435 5 Moscow Paris 245 6 Lima New York 455

SELECT * FROM flights; id origin destination duration 1 New York London 415 2 Shanghai Paris 760 3 Istanbul Tokyo 700 4 New York Paris 435 5 Moscow Paris 245 6 Lima New York 455

SELECT origin, destination FROM flights; id origin destination duration 1 New York London 415 2 Shanghai Paris 760 3 Istanbul Tokyo 700 4 New York Paris 435 5 Moscow Paris 245 6 Lima New York 455

SELECT origin, destination FROM flights; id origin destination duration 1 New York London 415 2 Shanghai Paris 760 3 Istanbul Tokyo 700 4 New York Paris 435 5 Moscow Paris 245 6 Lima New York 455

SELECT * FROM flights WHERE id = 3; id origin destination duration 1 New York London 415 2 Shanghai Paris 760 3 Istanbul Tokyo 700 4 New York Paris 435 5 Moscow Paris 245 6 Lima New York 455

SELECT * FROM flights WHERE id = 3; id origin destination duration 1 New York London 415 2 Shanghai Paris 760 3 Istanbul Tokyo 700 4 New York Paris 435 5 Moscow Paris 245 6 Lima New York 455

SELECT * FROM flights WHERE origin = 'New York'; id origin destination duration 1 New York London 415 2 Shanghai Paris 760 3 Istanbul Tokyo 700 4 New York Paris 435 5 Moscow Paris 245 6 Lima New York 455

SELECT * FROM flights WHERE origin = 'New York'; id origin destination duration 1 New York London 415 2 Shanghai Paris 760 3 Istanbul Tokyo 700 4 New York Paris 435 5 Moscow Paris 245 6 Lima New York 455

SELECT * FROM flights WHERE duration > 500; id origin destination duration 1 New York London 415 2 Shanghai Paris 760 3 Istanbul Tokyo 700 4 New York Paris 435 5 Moscow Paris 245 6 Lima New York 455

SELECT * FROM flights WHERE duration > 500; id origin destination duration 1 New York London 415 2 Shanghai Paris 760 3 Istanbul Tokyo 700 4 New York Paris 435 5 Moscow Paris 245 6 Lima New York 455

SELECT * FROM flights WHERE destination = 'Paris' AND duration > 500; id origin destination duration 1 New York London 415 2 Shanghai Paris 760 3 Istanbul Tokyo 700 4 New York Paris 435 5 Moscow Paris 245 6 Lima New York 455

SELECT * FROM flights WHERE destination = 'Paris' AND duration > 500; id origin destination duration 1 New York London 415 2 Shanghai Paris 760 3 Istanbul Tokyo 700 4 New York Paris 435 5 Moscow Paris 245 6 Lima New York 455

SELECT * FROM flights WHERE destination = 'Paris' OR duration > 500; id origin destination duration 1 New York London 415 2 Shanghai Paris 760 3 Istanbul Tokyo 700 4 New York Paris 435 5 Moscow Paris 245 6 Lima New York 455

SELECT * FROM flights WHERE destination = 'Paris' OR duration > 500; id origin destination duration 1 New York London 415 2 Shanghai Paris 760 3 Istanbul Tokyo 700 4 New York Paris 435 5 Moscow Paris 245 6 Lima New York 455

SELECT * FROM flights WHERE origin IN ('New York', 'Lima'); id origin destination duration 1 New York London 415 2 Shanghai Paris 760 3 Istanbul Tokyo 700 4 New York Paris 435 5 Moscow Paris 245 6 Lima New York 455

SELECT * FROM flights WHERE origin IN ('New York', 'Lima'); id origin destination duration 1 New York London 415 2 Shanghai Paris 760 3 Istanbul Tokyo 700 4 New York Paris 435 5 Moscow Paris 245 6 Lima New York 455

SELECT * FROM flights WHERE origin LIKE '%a%'; id origin destination duration 1 New York London 415 2 Shanghai Paris 760 3 Istanbul Tokyo 700 4 New York Paris 435 5 Moscow Paris 245 6 Lima New York 455

SELECT * FROM flights WHERE origin LIKE '%a%'; id origin destination duration 1 New York London 415 2 Shanghai Paris 760 3 Istanbul Tokyo 700 4 New York Paris 435 5 Moscow Paris 245 6 Lima New York 455

Functions SUM COUNT MIN MAX AVG...

UPDATE

UPDATE flights SET duration = 430 WHERE origin = 'New York' AND destination = 'London';

DELETE

DELETE FROM flights WHERE destination = 'Tokyo';

Other Clauses LIMIT ORDER BY GROUP BY HAVING...

Foreign Keys

flights id origin destination duration 1 New York London 415 2 Shanghai Paris 760 3 Istanbul Tokyo 700 4 New York Paris 435 5 Moscow Paris 245 6 Lima New York 455

flights id origin origin_code destination destination_code duration 1 New York JFK London LHR 415 2 Shanghai PVG Paris CDG 760 3 Istanbul IST Tokyo NRT 700 4 New York JFK Paris CDG 435 5 Moscow SVO Paris CDG 245 6 Lima LIM New York JFK 455

locations id code name 1 JFK New York 2 PVG Shanghai 3 IST Istanbul 4 LHR London 5 SVO Moscow 6 LIM Lima 7 CDG Paris 8 NRT Tokyo

flights id origin_id destination_id duration 1 1 4 415 2 2 7 760 3 3 8 700 4 1 7 435 5 5 7 245 6 6 1 455

passengers id name flight_id 1 Alice 1 2 Bob 1 3 Charlie 2 4 Dave 2 5 Erin 4 6 Frank 6 7 Grace 6

JOIN

Types of JOINs JOIN / INNER JOIN LEFT OUTER JOIN RIGHT OUTER JOIN FULL OUTER JOIN

CREATE INDEX

Nested Queries

flights id origin destination duration 1 New York London 415 2 Shanghai Paris 760 3 Istanbul Tokyo 700 4 New York Paris 435 5 Moscow Paris 245 6 Lima New York 455

passengers id name flight_id 1 Alice 1 2 Bob 1 3 Charlie 2 4 Dave 2 5 Erin 4 6 Frank 6 7 Grace 6

SELECT flight_id FROM passengers GROUP BY flight_id HAVING COUNT(*) > 1;

SELECT flight_id FROM passengers GROUP BY flight_id HAVING COUNT(*) > 1; id name flight_id 1 Alice 1 2 Bob 1 3 Charlie 2 4 Dave 2 5 Erin 4 6 Frank 6 7 Grace 6

SELECT flight_id FROM passengers GROUP BY flight_id HAVING COUNT(*) > 1; id name flight_id 1 Alice 1 2 Bob 1 3 Charlie 2 4 Dave 2 5 Erin 4 flight_id 1 2 6 6 Frank 6 7 Grace 6

SELECT flight_id FROM passengers GROUP BY flight_id HAVING COUNT(*) > 1; flight_id 1 2 6

SELECT * FROM flights WHERE id IN (SELECT flight_id FROM passengers GROUP BY flight_id HAVING COUNT(*) > 1); flight_id 1 2 6

SELECT * FROM flights WHERE id IN (SELECT flight_id FROM passengers GROUP BY flight_id HAVING COUNT(*) > 1); id origin destination duration 1 New York London 415 2 Shanghai Paris 760 3 Istanbul Tokyo 700 4 New York Paris 435 flight_id 1 2 6 5 Moscow Paris 245 6 Lima New York 455

SELECT * FROM flights WHERE id IN (SELECT flight_id FROM passengers GROUP BY flight_id HAVING COUNT(*) > 1); id origin destination duration 1 New York London 415 2 Shanghai Paris 760 3 Istanbul Tokyo 700 4 New York Paris 435 flight_id 1 2 6 5 Moscow Paris 245 6 Lima New York 455

SQL Injection

Username: Password:

SELECT * FROM users WHERE (username = username) AND (password = password);

Username: alice Password: 12345

SELECT * FROM users WHERE (username = username) AND (password = password);

SELECT * FROM users WHERE (username = 'alice') AND (password = '12345');

Username: hacker Password: 1' OR '1' = '1

SELECT * FROM users WHERE (username = username) AND (password = password);

SELECT * FROM users WHERE (username = 'hacker') AND (password = '1' OR '1' = '1');

Race Conditions

Race Conditions Bank Account $100

Race Conditions SELECT balance FROM bank Bank Account WHERE user_id = 1; $100

Race Conditions SELECT balance FROM bank Bank Account WHERE user_id = 1; 100 $100

Race Conditions SELECT balance FROM bank Bank Account WHERE user_id = 1; 100 UPDATE bank $100 SET balance = balance - 100 WHERE user_id = 1; $100

Race Conditions SELECT balance FROM bank Bank Account WHERE user_id = 1; 100 UPDATE bank $0 SET balance = balance - 100 WHERE user_id = 1; $100

Race Conditions Bank Account $100

Race Conditions SELECT balance FROM bank Bank Account WHERE user_id = 1; $100

Race Conditions SELECT balance FROM bank Bank Account WHERE user_id = 1; 100 $100

Race Conditions SELECT balance FROM bank Bank Account WHERE user_id = 1; SELECT balance FROM bank 100 WHERE user_id = 1; $100

Race Conditions SELECT balance FROM bank Bank Account WHERE user_id = 1; SELECT balance FROM bank 100 WHERE user_id = 1; $100 100

Race Conditions SELECT balance FROM bank Bank Account WHERE user_id = 1; SELECT balance FROM bank 100 WHERE user_id = 1; UPDATE bank SET balance = balance - 100 WHERE user_id = 1; $100 100 $100

Race Conditions SELECT balance FROM bank Bank Account WHERE user_id = 1; SELECT balance FROM bank 100 WHERE user_id = 1; UPDATE bank SET balance = balance - 100 WHERE user_id = 1; $0 100 $100

Race Conditions SELECT balance FROM bank Bank Account WHERE user_id = 1; SELECT balance FROM bank 100 WHERE user_id = 1; UPDATE bank SET balance = balance - 100 WHERE user_id = 1; $100 $0 100 UPDATE bank SET balance = balance - 100 WHERE user_id = 1; $100

Race Conditions SELECT balance FROM bank Bank Account WHERE user_id = 1; SELECT balance FROM bank 100 WHERE user_id = 1; UPDATE bank SET balance = balance - 100 WHERE user_id = 1; $100 -$100 100 UPDATE bank SET balance = balance - 100 WHERE user_id = 1; $100

SQL Transactions BEGIN COMMIT

SQLAlchemy

Morning Project Create a database on Heroku. Decide on a database schema and create tables: one for authors and one for books. Write a Python program to import data into the database. Write a Python program that queries the database for a book by its (partial) title or author.

CS50 Beyond