0% found this document useful (0 votes)
50 views

Computer Science S-75: Building Dynamic Websites

This document contains lecture notes on SQL and databases. It discusses MySQL data types, functions, JOIN operations, transactions, locks, and CSV files. PDO is also introduced for connecting to and querying databases in PHP. Examples are provided for different SQL queries and operations.

Uploaded by

samyakmehta1234
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views

Computer Science S-75: Building Dynamic Websites

This document contains lecture notes on SQL and databases. It discusses MySQL data types, functions, JOIN operations, transactions, locks, and CSV files. PDO is also introduced for connecting to and querying databases in PHP. Examples are provided for different SQL queries and operations.

Uploaded by

samyakmehta1234
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Computer Science S-75

Building Dynamic Websites


Harvard Summer School
https://www.cs75.net/

Lecture 5: SQL, Continued


David J. Malan [email protected]
0

MySQL Types

CHAR VARCHAR TEXT TINYTEXT MEDIUMTEXT LONGTEXT DATE DATETIME YEAR TIME TIMESTAMP TINYINT SMALLINT MEDIUMINT INT BIGINT

FLOAT DOUBLE DECIMAL TINYBLOB BLOB MEDIUMBLOB LONGBLOB BINARY VARBINARY ENUM SET

MySQL Functions
http://dev.mysql.com/doc/refman/5.5/en/functions.html

DATE_FORMAT(date,format)
http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_date-format

TIME_FORMAT(time,format)
http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_time-format

...

PDO
$dsn = 'mysql:dbname=lecture;host=127.0.0.1'; $user = 'jharvard'; $password = 'crimson'; try { $dbh = new PDO($dsn, $user, $password); } catch (PDOException $e) { echo 'Connection failed: ' . $e->getMessage(); }

http://www.phpro.org/tutorials/Introduction-to-PHP-PDO.html

JOIN
SELECT Employees.Name, Orders.Product FROM Employees, Orders WHERE Employees.Employee_ID=Orders.Employee_ID

Excerpted from http://www.w3schools.com/sql/sql_join.asp.

JOIN
SELECT Employees.Name, Orders.Product FROM Employees, Orders WHERE Employees.Employee_ID=Orders.Employee_ID

Excerpted from http://www.w3schools.com/sql/sql_join.asp.

JOIN
SELECT Employees.Name, Orders.Product FROM Employees JOIN Orders ON Employees.Employee_ID=Orders.Employee_ID

Adapted from http://www.w3schools.com/sql/sql_join.asp.

Race Conditions

INSERT ... ON DUPLICATE KEY UPDATE

Image from dev.mysql.com.

Transactions (InnoDB)
START TRANSACTION; UPDATE account SET balance = balance - 1000 WHERE number = 2; UPDATE account SET balance = balance + 1000 WHERE number = 1; COMMIT;

Excerpted from http://dev.mysql.com/books/mysqlpress/mysql-tutorial/ch10.html.

Transactions (InnoDB)
START TRANSACTION; UPDATE account SET balance = balance - 1000 WHERE number = 2; UPDATE account SET balance = balance + 1000 WHERE number = 1; SELECT balance FROM account WHERE number = 2; # suppose account #2 has a negative balance! ROLLBACK;

Excerpted from http://dev.mysql.com/books/mysqlpress/mysql-tutorial/ch10.html.

10

Locks (MyISAM)
LOCK TABLES account WRITE; SELECT balance FROM account WHERE number = 2; UPDATE account SET balance = 1500 WHERE number = 2; UNLOCK TABLES;

Excerpted from http://dev.mysql.com/books/mysqlpress/mysql-tutorial/ch10.html.

11

1) 2) 3) 4) 5) 6)

Log In Register Get Quotes Sell Stocks Buy Stocks ...

12

CSV

Image from finance.yahoo.com.

13

Computer Science S-75


Building Dynamic Websites
Harvard Summer School
https://www.cs75.net/

Lecture 5: SQL, Continued


David J. Malan [email protected]
14

You might also like