93
Web Programming with Python and JavaScript

Web Programming with Python and JavaScriptcdn.cs50.net/web/2018/spring/lectures/3/lecture3.pdf · with Python and JavaScript. Databases. origin destination dura,on New York London

  • Upload
    others

  • View
    35

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Web Programming with Python and JavaScriptcdn.cs50.net/web/2018/spring/lectures/3/lecture3.pdf · with Python and JavaScript. Databases. origin destination dura,on New York London

Web Programming with Python and JavaScript

Page 2: Web Programming with Python and JavaScriptcdn.cs50.net/web/2018/spring/lectures/3/lecture3.pdf · with Python and JavaScript. Databases. origin destination dura,on New York London

Databases

Page 3: Web Programming with Python and JavaScriptcdn.cs50.net/web/2018/spring/lectures/3/lecture3.pdf · with Python and JavaScript. Databases. origin destination dura,on New York London

origin destination dura,on

New York London 415

Shanghai Paris 760

Istanbul Tokyo 700

New York Paris 435

Moscow Paris 245

Lima New York 455

Page 4: Web Programming with Python and JavaScriptcdn.cs50.net/web/2018/spring/lectures/3/lecture3.pdf · with Python and JavaScript. Databases. origin destination dura,on New York London

SQL

Page 5: Web Programming with Python and JavaScriptcdn.cs50.net/web/2018/spring/lectures/3/lecture3.pdf · with Python and JavaScript. Databases. origin destination dura,on New York London

PostgreSQL

Page 6: Web Programming with Python and JavaScriptcdn.cs50.net/web/2018/spring/lectures/3/lecture3.pdf · with Python and JavaScript. Databases. origin destination dura,on New York London

Data Types

• INTEGER • DECIMAL • SERIAL • VARCHAR • TIMESTAMP • BOOLEAN • ENUM • ...

Page 7: Web Programming with Python and JavaScriptcdn.cs50.net/web/2018/spring/lectures/3/lecture3.pdf · with Python and JavaScript. Databases. origin destination dura,on New York London

CREATE TABLE

Page 8: Web Programming with Python and JavaScriptcdn.cs50.net/web/2018/spring/lectures/3/lecture3.pdf · with Python and JavaScript. Databases. origin destination dura,on New York London

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

Page 9: Web Programming with Python and JavaScriptcdn.cs50.net/web/2018/spring/lectures/3/lecture3.pdf · with Python and JavaScript. Databases. origin destination dura,on New York London

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

Page 10: Web Programming with Python and JavaScriptcdn.cs50.net/web/2018/spring/lectures/3/lecture3.pdf · with Python and JavaScript. Databases. origin destination dura,on New York London

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

Page 11: Web Programming with Python and JavaScriptcdn.cs50.net/web/2018/spring/lectures/3/lecture3.pdf · with Python and JavaScript. Databases. origin destination dura,on New York London

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

Page 12: Web Programming with Python and JavaScriptcdn.cs50.net/web/2018/spring/lectures/3/lecture3.pdf · with Python and JavaScript. Databases. origin destination dura,on New York London

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

Page 13: Web Programming with Python and JavaScriptcdn.cs50.net/web/2018/spring/lectures/3/lecture3.pdf · with Python and JavaScript. Databases. origin destination dura,on New York London

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

Page 14: Web Programming with Python and JavaScriptcdn.cs50.net/web/2018/spring/lectures/3/lecture3.pdf · with Python and JavaScript. Databases. origin destination dura,on New York London

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

Page 15: Web Programming with Python and JavaScriptcdn.cs50.net/web/2018/spring/lectures/3/lecture3.pdf · with Python and JavaScript. Databases. origin destination dura,on New York London

Constraints

• NOT NULL

• UNIQUE

• PRIMARY KEY

• DEFAULT

• CHECK

• ...

Page 16: Web Programming with Python and JavaScriptcdn.cs50.net/web/2018/spring/lectures/3/lecture3.pdf · with Python and JavaScript. Databases. origin destination dura,on New York London

INSERT

Page 17: Web Programming with Python and JavaScriptcdn.cs50.net/web/2018/spring/lectures/3/lecture3.pdf · with Python and JavaScript. Databases. origin destination dura,on New York London

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

Page 18: Web Programming with Python and JavaScriptcdn.cs50.net/web/2018/spring/lectures/3/lecture3.pdf · with Python and JavaScript. Databases. origin destination dura,on New York London

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

Page 19: Web Programming with Python and JavaScriptcdn.cs50.net/web/2018/spring/lectures/3/lecture3.pdf · with Python and JavaScript. Databases. origin destination dura,on New York London

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

Page 20: Web Programming with Python and JavaScriptcdn.cs50.net/web/2018/spring/lectures/3/lecture3.pdf · with Python and JavaScript. Databases. origin destination dura,on New York London

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

Page 21: Web Programming with Python and JavaScriptcdn.cs50.net/web/2018/spring/lectures/3/lecture3.pdf · with Python and JavaScript. Databases. origin destination dura,on New York London

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

Page 22: Web Programming with Python and JavaScriptcdn.cs50.net/web/2018/spring/lectures/3/lecture3.pdf · with Python and JavaScript. Databases. origin destination dura,on New York London

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

Page 23: Web Programming with Python and JavaScriptcdn.cs50.net/web/2018/spring/lectures/3/lecture3.pdf · with Python and JavaScript. Databases. origin destination dura,on New York London

SELECT

Page 24: Web Programming with Python and JavaScriptcdn.cs50.net/web/2018/spring/lectures/3/lecture3.pdf · with Python and JavaScript. Databases. origin destination dura,on New York London

id origin des,na,on dura,on

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;

Page 25: Web Programming with Python and JavaScriptcdn.cs50.net/web/2018/spring/lectures/3/lecture3.pdf · with Python and JavaScript. Databases. origin destination dura,on New York London

id origin des,na,on dura,on

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;

Page 26: Web Programming with Python and JavaScriptcdn.cs50.net/web/2018/spring/lectures/3/lecture3.pdf · with Python and JavaScript. Databases. origin destination dura,on New York London

id origin des,na,on dura,on

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;

Page 27: Web Programming with Python and JavaScriptcdn.cs50.net/web/2018/spring/lectures/3/lecture3.pdf · with Python and JavaScript. Databases. origin destination dura,on New York London

id origin des,na,on dura,on

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;

Page 28: Web Programming with Python and JavaScriptcdn.cs50.net/web/2018/spring/lectures/3/lecture3.pdf · with Python and JavaScript. Databases. origin destination dura,on New York London

id origin des,na,on dura,on

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;

Page 29: Web Programming with Python and JavaScriptcdn.cs50.net/web/2018/spring/lectures/3/lecture3.pdf · with Python and JavaScript. Databases. origin destination dura,on New York London

id origin des,na,on dura,on

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;

Page 30: Web Programming with Python and JavaScriptcdn.cs50.net/web/2018/spring/lectures/3/lecture3.pdf · with Python and JavaScript. Databases. origin destination dura,on New York London

id origin des,na,on dura,on

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';

Page 31: Web Programming with Python and JavaScriptcdn.cs50.net/web/2018/spring/lectures/3/lecture3.pdf · with Python and JavaScript. Databases. origin destination dura,on New York London

id origin des,na,on dura,on

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';

Page 32: Web Programming with Python and JavaScriptcdn.cs50.net/web/2018/spring/lectures/3/lecture3.pdf · with Python and JavaScript. Databases. origin destination dura,on New York London

id origin des,na,on dura,on

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;

Page 33: Web Programming with Python and JavaScriptcdn.cs50.net/web/2018/spring/lectures/3/lecture3.pdf · with Python and JavaScript. Databases. origin destination dura,on New York London

id origin des,na,on dura,on

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;

Page 34: Web Programming with Python and JavaScriptcdn.cs50.net/web/2018/spring/lectures/3/lecture3.pdf · with Python and JavaScript. Databases. origin destination dura,on New York London

id origin des,na,on dura,on

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;

Page 35: Web Programming with Python and JavaScriptcdn.cs50.net/web/2018/spring/lectures/3/lecture3.pdf · with Python and JavaScript. Databases. origin destination dura,on New York London

id origin des,na,on dura,on

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;

Page 36: Web Programming with Python and JavaScriptcdn.cs50.net/web/2018/spring/lectures/3/lecture3.pdf · with Python and JavaScript. Databases. origin destination dura,on New York London

id origin des,na,on dura,on

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;

Page 37: Web Programming with Python and JavaScriptcdn.cs50.net/web/2018/spring/lectures/3/lecture3.pdf · with Python and JavaScript. Databases. origin destination dura,on New York London

id origin des,na,on dura,on

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;

Page 38: Web Programming with Python and JavaScriptcdn.cs50.net/web/2018/spring/lectures/3/lecture3.pdf · with Python and JavaScript. Databases. origin destination dura,on New York London

id origin des,na,on dura,on

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');

Page 39: Web Programming with Python and JavaScriptcdn.cs50.net/web/2018/spring/lectures/3/lecture3.pdf · with Python and JavaScript. Databases. origin destination dura,on New York London

id origin des,na,on dura,on

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');

Page 40: Web Programming with Python and JavaScriptcdn.cs50.net/web/2018/spring/lectures/3/lecture3.pdf · with Python and JavaScript. Databases. origin destination dura,on New York London

id origin des,na,on dura,on

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%';

Page 41: Web Programming with Python and JavaScriptcdn.cs50.net/web/2018/spring/lectures/3/lecture3.pdf · with Python and JavaScript. Databases. origin destination dura,on New York London

id origin des,na,on dura,on

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%';

Page 42: Web Programming with Python and JavaScriptcdn.cs50.net/web/2018/spring/lectures/3/lecture3.pdf · with Python and JavaScript. Databases. origin destination dura,on New York London

FuncMons

• SUM

• COUNT

• MIN

• MAX

• AVG

• ...

Page 43: Web Programming with Python and JavaScriptcdn.cs50.net/web/2018/spring/lectures/3/lecture3.pdf · with Python and JavaScript. Databases. origin destination dura,on New York London

UPDATE

Page 44: Web Programming with Python and JavaScriptcdn.cs50.net/web/2018/spring/lectures/3/lecture3.pdf · with Python and JavaScript. Databases. origin destination dura,on New York London

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

Page 45: Web Programming with Python and JavaScriptcdn.cs50.net/web/2018/spring/lectures/3/lecture3.pdf · with Python and JavaScript. Databases. origin destination dura,on New York London

DELETE

Page 46: Web Programming with Python and JavaScriptcdn.cs50.net/web/2018/spring/lectures/3/lecture3.pdf · with Python and JavaScript. Databases. origin destination dura,on New York London

DELETE FROM countries WHERE destination = 'Tokyo';

Page 47: Web Programming with Python and JavaScriptcdn.cs50.net/web/2018/spring/lectures/3/lecture3.pdf · with Python and JavaScript. Databases. origin destination dura,on New York London

Other Clauses

• LIMIT

• ORDER BY

• GROUP BY

• HAVING

• ...

Page 48: Web Programming with Python and JavaScriptcdn.cs50.net/web/2018/spring/lectures/3/lecture3.pdf · with Python and JavaScript. Databases. origin destination dura,on New York London

Foreign Keys

Page 49: Web Programming with Python and JavaScriptcdn.cs50.net/web/2018/spring/lectures/3/lecture3.pdf · with Python and JavaScript. Databases. origin destination dura,on New York London

id origin des,na,on 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

Page 50: Web Programming with Python and JavaScriptcdn.cs50.net/web/2018/spring/lectures/3/lecture3.pdf · with Python and JavaScript. Databases. origin destination dura,on New York London

id origin origin_code des,na,on des,na,on_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

flights

Page 51: Web Programming with Python and JavaScriptcdn.cs50.net/web/2018/spring/lectures/3/lecture3.pdf · with Python and JavaScript. Databases. origin destination dura,on New York London

id code name1 JFK New York2 PVG Shanghai3 IST Istanbul4 LHR London5 SVO Moscow6 LIM Lima7 CDG Paris8 NRT Tokyo

locations

Page 52: Web Programming with Python and JavaScriptcdn.cs50.net/web/2018/spring/lectures/3/lecture3.pdf · with Python and JavaScript. Databases. origin destination dura,on New York London

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

flights

Page 53: Web Programming with Python and JavaScriptcdn.cs50.net/web/2018/spring/lectures/3/lecture3.pdf · with Python and JavaScript. Databases. origin destination dura,on New York London

id name flight_id1 Alice 12 Bob 13 Charlie 24 Dave 25 Erin 46 Frank 67 Grace 6

passengers

Page 54: Web Programming with Python and JavaScriptcdn.cs50.net/web/2018/spring/lectures/3/lecture3.pdf · with Python and JavaScript. Databases. origin destination dura,on New York London

JOIN

Page 55: Web Programming with Python and JavaScriptcdn.cs50.net/web/2018/spring/lectures/3/lecture3.pdf · with Python and JavaScript. Databases. origin destination dura,on New York London

Types of JOINs

• JOIN / INNER JOIN

• LEFT OUTER JOIN

• RIGHT OUTER JOIN

• FULL OUTER JOIN

Page 56: Web Programming with Python and JavaScriptcdn.cs50.net/web/2018/spring/lectures/3/lecture3.pdf · with Python and JavaScript. Databases. origin destination dura,on New York London

CREATE INDEX

Page 57: Web Programming with Python and JavaScriptcdn.cs50.net/web/2018/spring/lectures/3/lecture3.pdf · with Python and JavaScript. Databases. origin destination dura,on New York London

Nested Queries

Page 58: Web Programming with Python and JavaScriptcdn.cs50.net/web/2018/spring/lectures/3/lecture3.pdf · with Python and JavaScript. Databases. origin destination dura,on New York London

id origin des,na,on dura,on

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

Page 59: Web Programming with Python and JavaScriptcdn.cs50.net/web/2018/spring/lectures/3/lecture3.pdf · with Python and JavaScript. Databases. origin destination dura,on New York London

id name flight_id1 Alice 12 Bob 13 Charlie 24 Dave 25 Erin 46 Frank 67 Grace 6

passengers

Page 60: Web Programming with Python and JavaScriptcdn.cs50.net/web/2018/spring/lectures/3/lecture3.pdf · with Python and JavaScript. Databases. origin destination dura,on New York London

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

Page 61: Web Programming with Python and JavaScriptcdn.cs50.net/web/2018/spring/lectures/3/lecture3.pdf · with Python and JavaScript. Databases. origin destination dura,on New York London

id name flight_id1 Alice 12 Bob 13 Charlie 24 Dave 25 Erin 46 Frank 67 Grace 6

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

Page 62: Web Programming with Python and JavaScriptcdn.cs50.net/web/2018/spring/lectures/3/lecture3.pdf · with Python and JavaScript. Databases. origin destination dura,on New York London

id name flight_id1 Alice 12 Bob 13 Charlie 24 Dave 25 Erin 46 Frank 67 Grace 6

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

flight_id126

Page 63: Web Programming with Python and JavaScriptcdn.cs50.net/web/2018/spring/lectures/3/lecture3.pdf · with Python and JavaScript. Databases. origin destination dura,on New York London

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

flight_id126

Page 64: Web Programming with Python and JavaScriptcdn.cs50.net/web/2018/spring/lectures/3/lecture3.pdf · with Python and JavaScript. Databases. origin destination dura,on New York London

SELECT * FROM flights WHERE id IN (SELECT flight_id FROM passengers

GROUP BY flight_id HAVING COUNT(*) > 1);

flight_id126

Page 65: Web Programming with Python and JavaScriptcdn.cs50.net/web/2018/spring/lectures/3/lecture3.pdf · with Python and JavaScript. Databases. origin destination dura,on New York London

SELECT * FROM flights WHERE id IN (SELECT flight_id FROM passengers

GROUP BY flight_id HAVING COUNT(*) > 1);

flight_id126

id origin des,na,on dura,on

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

Page 66: Web Programming with Python and JavaScriptcdn.cs50.net/web/2018/spring/lectures/3/lecture3.pdf · with Python and JavaScript. Databases. origin destination dura,on New York London

SELECT * FROM flights WHERE id IN (SELECT flight_id FROM passengers

GROUP BY flight_id HAVING COUNT(*) > 1);

flight_id126

id origin des,na,on dura,on

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

Page 67: Web Programming with Python and JavaScriptcdn.cs50.net/web/2018/spring/lectures/3/lecture3.pdf · with Python and JavaScript. Databases. origin destination dura,on New York London

SQL InjecMon

Page 68: Web Programming with Python and JavaScriptcdn.cs50.net/web/2018/spring/lectures/3/lecture3.pdf · with Python and JavaScript. Databases. origin destination dura,on New York London

Username:

Password:

Page 69: Web Programming with Python and JavaScriptcdn.cs50.net/web/2018/spring/lectures/3/lecture3.pdf · with Python and JavaScript. Databases. origin destination dura,on New York London

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

Page 70: Web Programming with Python and JavaScriptcdn.cs50.net/web/2018/spring/lectures/3/lecture3.pdf · with Python and JavaScript. Databases. origin destination dura,on New York London

Username:

Password:

alice

12345

Page 71: Web Programming with Python and JavaScriptcdn.cs50.net/web/2018/spring/lectures/3/lecture3.pdf · with Python and JavaScript. Databases. origin destination dura,on New York London

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

Page 72: Web Programming with Python and JavaScriptcdn.cs50.net/web/2018/spring/lectures/3/lecture3.pdf · with Python and JavaScript. Databases. origin destination dura,on New York London

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

Page 73: Web Programming with Python and JavaScriptcdn.cs50.net/web/2018/spring/lectures/3/lecture3.pdf · with Python and JavaScript. Databases. origin destination dura,on New York London

Username:

Password:

hacker

1' OR '1' = '1

Page 74: Web Programming with Python and JavaScriptcdn.cs50.net/web/2018/spring/lectures/3/lecture3.pdf · with Python and JavaScript. Databases. origin destination dura,on New York London

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

Page 75: Web Programming with Python and JavaScriptcdn.cs50.net/web/2018/spring/lectures/3/lecture3.pdf · with Python and JavaScript. Databases. origin destination dura,on New York London

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

Page 76: Web Programming with Python and JavaScriptcdn.cs50.net/web/2018/spring/lectures/3/lecture3.pdf · with Python and JavaScript. Databases. origin destination dura,on New York London

Race Conditions

Page 77: Web Programming with Python and JavaScriptcdn.cs50.net/web/2018/spring/lectures/3/lecture3.pdf · with Python and JavaScript. Databases. origin destination dura,on New York London

Race Conditions

$100

Bank Account

Page 78: Web Programming with Python and JavaScriptcdn.cs50.net/web/2018/spring/lectures/3/lecture3.pdf · with Python and JavaScript. Databases. origin destination dura,on New York London

Race Conditions

$100

Bank AccountSELECT balance FROM bank WHERE user_id = 1;

Page 79: Web Programming with Python and JavaScriptcdn.cs50.net/web/2018/spring/lectures/3/lecture3.pdf · with Python and JavaScript. Databases. origin destination dura,on New York London

Race Conditions

$100

Bank AccountSELECT balance FROM bank WHERE user_id = 1;

100

Page 80: Web Programming with Python and JavaScriptcdn.cs50.net/web/2018/spring/lectures/3/lecture3.pdf · with Python and JavaScript. Databases. origin destination dura,on New York London

Race Conditions

$100

Bank AccountSELECT balance FROM bank WHERE user_id = 1;

100

UPDATE bank SET balance = balance - 100 WHERE user_id = 1;

$100

Page 81: Web Programming with Python and JavaScriptcdn.cs50.net/web/2018/spring/lectures/3/lecture3.pdf · with Python and JavaScript. Databases. origin destination dura,on New York London

Race Conditions

$0

Bank AccountSELECT balance FROM bank WHERE user_id = 1;

$100

100

UPDATE bank SET balance = balance - 100 WHERE user_id = 1;

Page 82: Web Programming with Python and JavaScriptcdn.cs50.net/web/2018/spring/lectures/3/lecture3.pdf · with Python and JavaScript. Databases. origin destination dura,on New York London

Race Conditions

$100

Bank Account

Page 83: Web Programming with Python and JavaScriptcdn.cs50.net/web/2018/spring/lectures/3/lecture3.pdf · with Python and JavaScript. Databases. origin destination dura,on New York London

Race Conditions

$100

Bank AccountSELECT balance FROM bank WHERE user_id = 1;

Page 84: Web Programming with Python and JavaScriptcdn.cs50.net/web/2018/spring/lectures/3/lecture3.pdf · with Python and JavaScript. Databases. origin destination dura,on New York London

Race Conditions

$100

Bank AccountSELECT balance FROM bank WHERE user_id = 1;

100

Page 85: Web Programming with Python and JavaScriptcdn.cs50.net/web/2018/spring/lectures/3/lecture3.pdf · with Python and JavaScript. Databases. origin destination dura,on New York London

Race Conditions

$100

Bank AccountSELECT balance FROM bank WHERE user_id = 1;

100SELECT balance FROM bank WHERE user_id = 1;

Page 86: Web Programming with Python and JavaScriptcdn.cs50.net/web/2018/spring/lectures/3/lecture3.pdf · with Python and JavaScript. Databases. origin destination dura,on New York London

Race Conditions

$100

Bank AccountSELECT balance FROM bank WHERE user_id = 1;

100SELECT balance FROM bank WHERE user_id = 1;

100

Page 87: Web Programming with Python and JavaScriptcdn.cs50.net/web/2018/spring/lectures/3/lecture3.pdf · with Python and JavaScript. Databases. origin destination dura,on New York London

Race Conditions

$100

Bank AccountSELECT balance FROM bank WHERE user_id = 1;

$100

100SELECT balance FROM bank WHERE user_id = 1;

100UPDATE bank SET balance = balance - 100 WHERE user_id = 1;

Page 88: Web Programming with Python and JavaScriptcdn.cs50.net/web/2018/spring/lectures/3/lecture3.pdf · with Python and JavaScript. Databases. origin destination dura,on New York London

Race Conditions

$0

Bank AccountSELECT balance FROM bank WHERE user_id = 1;

$100

100SELECT balance FROM bank WHERE user_id = 1;

100UPDATE bank SET balance = balance - 100 WHERE user_id = 1;

Page 89: Web Programming with Python and JavaScriptcdn.cs50.net/web/2018/spring/lectures/3/lecture3.pdf · with Python and JavaScript. Databases. origin destination dura,on New York London

Race Conditions

$0

Bank AccountSELECT balance FROM bank WHERE user_id = 1;

$100

100SELECT balance FROM bank WHERE user_id = 1;

100UPDATE bank SET balance = balance - 100 WHERE user_id = 1; UPDATE bank

SET balance = balance - 100 WHERE user_id = 1;

$100

Page 90: Web Programming with Python and JavaScriptcdn.cs50.net/web/2018/spring/lectures/3/lecture3.pdf · with Python and JavaScript. Databases. origin destination dura,on New York London

Race Conditions

-$100

Bank AccountSELECT balance FROM bank WHERE user_id = 1;

$100

100SELECT balance FROM bank WHERE user_id = 1;

100UPDATE bank SET balance = balance - 100 WHERE user_id = 1; UPDATE bank

SET balance = balance - 100 WHERE user_id = 1;

$100

Page 91: Web Programming with Python and JavaScriptcdn.cs50.net/web/2018/spring/lectures/3/lecture3.pdf · with Python and JavaScript. Databases. origin destination dura,on New York London

SQL Transactions

• BEGIN

• COMMIT

Page 92: Web Programming with Python and JavaScriptcdn.cs50.net/web/2018/spring/lectures/3/lecture3.pdf · with Python and JavaScript. Databases. origin destination dura,on New York London

SQLAlchemy

Page 93: Web Programming with Python and JavaScriptcdn.cs50.net/web/2018/spring/lectures/3/lecture3.pdf · with Python and JavaScript. Databases. origin destination dura,on New York London

Web Programming with Python and JavaScript