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

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

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

CSCI-UA: Database Design & Web Implementation. Professor Evan Sandhaus Lecture #17: MySQL Gets Done

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

MySQL: Querying and Using Form Data

CSCI-UA: Database Design & Web Implementation. Professor Evan Sandhaus Lecture #23: SQLite

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

Web Application Development (WAD) V th Sem BBAITM (Unit 4) By: Binit Patel

CHAPTER 10. Connecting to Databases within PHP

Database Connectivity using PHP Some Points to Remember:

Networks and Web for Health Informatics (HINF 6220) Tutorial 13 : PHP 29 Oct 2015

Chapter 7 PHP Files & MySQL Databases

NULLs & Outer Joins. Objectives of the Lecture :

Hello everyone! Page 1. Your folder should look like this. To start with Run your XAMPP app and start your Apache and MySQL.

Web Application Development (WAD) V th Sem BBAITM(Unit-1) By: Binit Patel

Unit Assessment Guide

Principles of Data Management

ITS331 IT Laboratory I: (Laboratory #11) Session Handling

PHP Querying. Lecture 21. Robb T. Koether. Hampden-Sydney College. Fri, Mar 2, 2018

CSC Web Programming. Introduction to SQL

Sql Server Syllabus. Overview

Mount Saint Mary College, Newburgh, NY Internet Programming III - CIT310

Daniel Pittman October 17, 2011

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

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

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #16 Loops: Matrix Using Nested for Loop

If Only. More SQL and PHP

Web Programming. Dr Walid M. Aly. Lecture 10 PHP. lec10. Web Programming CS433/CS614 22:32. Dr Walid M. Aly

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

B. V. Patel Institute of BMC & IT 2014

Relational Database Management Systems for Epidemiologists: SQL Part II

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

1) Introduction to SQL

MongoDB Step By Step. By B.A.Khivsara Assistant Professor Department of Computer Engineering SNJB s COE,Chandwad

Development Technologies. Agenda: phpmyadmin 2/20/2016. phpmyadmin MySQLi. Before you can put your data into a table, that table should exist.

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

2017 Politecnico di Torino 1

2017 Politecnico di Torino 1

Web Security. Attacks on Servers 11/6/2017 1

COMP284 Scripting Languages Lecture 10: PHP (Part 2) Handouts

Princess Nourah bint Abdulrahman University. Computer Sciences Department

Lecture 12. PHP. cp476 PHP

COM1004 Web and Internet Technology

5. Single-row function

CMPS 401 Survey of Programming Languages

WEBD 236 Web Information Systems Programming

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

CGS 3066: Spring 2017 SQL Reference

The M in LAMP: MySQL CSCI 470: Web Science Keith Vertanen Copyright 2014

PHP Reference. To access MySQL manually, run the following command on the machine, called Sources, where MySQL and PhP have been installed:

PHP Development - Introduction

Let's Look Back. We talked about how to create a form in HTML. Forms are one way to interact with users

Querying Data with Transact-SQL

PHP 5 if...else...elseif Statements

CS 377 Database Systems. Li Xiong Department of Mathematics and Computer Science Emory University

Overview of MySQL Structure and Syntax [2]

PHP: Cookies, Sessions, Databases. CS174. Chris Pollett. Sep 24, 2008.

Introduction to database design

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

Querying Data with Transact SQL

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

Introduction of PHP Created By: Umar Farooque Khan

Relational Database Management Systems for Epidemiologists: SQL Part I

COMP284 Scripting Languages Lecture 11: PHP (Part 3) Handouts

Course Outline and Objectives: Database Programming with SQL

SQL Server Administration Class 4 of 4. Activant Prophet 21. Basic Data Manipulation

INTERMEDIATE SQL GOING BEYOND THE SELECT. Created by Brian Duffey

The PHP language. Teaching you everything about PHP? Not exactly Goal: teach you how to interact with a database via web

Announcements (September 14) SQL: Part I SQL. Creating and dropping tables. Basic queries: SFW statement. Example: reading a table

CPSC 481: CREATIVE INQUIRY TO WSBF

Querying Data with Transact-SQL

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

COMS 469: Interactive Media II

Activant Solutions Inc. SQL 2005: Basic Data Manipulation

HTML Tables and Forms. Outline. Review. Review. Example Demo/ Walkthrough. CS 418/518 Web Programming Spring Tables to Display Data"

Secure Web-Based Systems Fall Test 1

Shell programming. Introduction to Operating Systems

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

Comp 519: Web Programming Autumn 2015

HPCC JDBC Driver. Boca Raton Documentation Team

Fundamentals of Web Programming

Chapter 9: MySQL for Server-Side Data Storage

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

Language. f SQL. Larry Rockoff COURSE TECHNOLOGY. Kingdom United States. Course Technology PTR. A part ofcenqaqe Learninq

Retrieving Query Results

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

CERTIFICATE IN WEB PROGRAMMING

COMP519: Web Programming Autumn 2015

STIDistrict Query (Basic)

SQL Part 2. Kathleen Durant PhD Northeastern University CS3200 Lesson 6

Networks and Web for Health Informatics (HINF 6220)

CSCE 548 Building Secure Software SQL Injection Attack

Previously everyone in the class used the mysql account: Username: csci340user Password: csci340pass

Monetra. POST Protocol Specification

COMP519 Web Programming Lecture 27: PHP (Part 3) Handouts

End o' semester clean up. A little bit of everything

CSCU9Q5 Introduction to MySQL. Data Definition & Manipulation (Over ~two Lectures)

Structured Query Language Continued. Rose-Hulman Institute of Technology Curt Clifton

Web Security IV: Cross-Site Attacks

Programming in C++ PART 2

Transcription:

CSCI-UA:0060-02 Database Design & Web Implementation Professor Evan Sandhaus sandhaus@cs.nyu.edu evan@nytimes.com Lecture #28: This is the end - the only end my friends. Database Design and Web Implementation

Administrivia Homework HW 9 Is Out Do Some Reading Check Out Python and MongoDB Book Database Design and Web Implementation 2

On The Menu Review MySQL Syntax PHP PHP + MySQL SQLite MongoDB Database Design and Web Implementation 3

Create Table Syntax CREATE TABLE [table_name] ( [column_name] [column_type] [column_details], KEY([column_name(s)], UNIQUE KEY([column_name(s)], PRIMARY KEY([column_name(s)], FOREIGN KEY ([column_name]) REFERENCES [table_name]([column_name]) ) ENGINE=[engine] DEFAULT CHARSET=[charset] COLLATE=[collation]; Database Design and Web Implementation 4

INSERT Syntax INSERT INTO [table_name] (column_1, column_2,...) VALUES (value_1, value_2,...); Database Design and Web Implementation 5

INSERT Syntax INSERT INTO [table_name] (column_1, column_2,...) VALUES (value_1, value_2,...), (value_1, value_2,...), (value_1, value_2,...), (value_1, value_2,...), (value_1, value_2,...); Database Design and Web Implementation 6

UPDATE Syntax UPDATE [table_name] SET [column_1] = [value_1] [column_2] = [value_2] WHERE [condition_1] [(AND OR) condition_2] [(AND OR) condition_3]... [(AND OR) condition_n] Database Design and Web Implementation 7

DELETE Syntax DELETE FROM [table_name] WHERE [condition_1] [(AND OR) condition_2] [(AND OR) condition_3]... [(AND OR) condition_n] Database Design and Web Implementation 8

Simple Select Statement SELECT * FROM table LIMIT x OFFSET y Database Design and Web Implementation 9

Simple Select Statement SELECT row_1, row_2,... FROM table_1, table_2,... WHERE condition_1 (AND OR) condition_2 (AND OR)... Database Design and Web Implementation 10

Simple Select Statement with order by SELECT row_1, row_2,... FROM table_1, table_2,... WHERE condition_1 (AND OR) condition_2 (AND OR) ORDER BY column_1, column_2 Database Design and Web Implementation 11

Aggregate Queries SELECT SUM(numeric_column_1), MIN(numeric_column_2), MAX(numeric_column_3), AVG(numeric_column_4), COUNT(any_column_5), COUNT(DISTINCT any_column_1), FROM table_1... Database Design and Web Implementation 12

GROUP BY A SELECT against a table generates a virtual table containing the matching rows. Adding a GROUP BY [col_name] statement collapses this virtual table so that there is just one row for each individual value of [col_name]. GROUP BY statements are often combined with aggregate functions (SUM, MIN, MAX, AVG). Database Design and Web Implementation 13

Aggregate Queries With Group By SELECT SUM(numeric_column_1), other_column_1, FROM table_1 GROUP BY other_column_1 Database Design and Web Implementation 14

JOIN A SELECT against a table generates a virtual table containing the matching rows. E.G. SELECT * FROM t1; A SELECT against multiple tables generates a virtual table containing combined rows from both tables. E.G. SELECT * FROM t1 JOIN t2; JOINS allow us to specify how we want rows from different to be combined in a SELECT over multiple tables. Database Design and Web Implementation 15

JOIN SELECT * FROM professor; SELECT * FROM class; SELECT * FROM professor JOIN class; Returns ALL POSSIBLE combinations of rows in the professor and class tables. If we only want to see the combination of rows that reflect which professor is teaching which class, we must specify some type of join. Database Design and Web Implementation 16

INNER JOIN When SELECTING an inner join between tables t1 and t2 on key k the only rows that are returned are those rows in which t1.k = t2.k. SELECT * FROM professor JOIN CLASS ON professor.professor_id = class.professor_id; Database Design and Web Implementation 17

INNER JOIN - Visualized Database Design and Web Implementation 18

INNER JOIN Lots of different ways of expressing - the following are equivalent in MySQL SELECT * FROM t1 INNER JOIN t2 ON t1.k = t2.k; SELECT * FROM t1 JOIN t2 ON t1.k = t2.k; SELECT * FROM t1, t2 WHERE t1.k = t2.k; Database Design and Web Implementation 19

OUTER JOIN Outer joins come in three flavors. LEFT RIGHT FULL Database Design and Web Implementation 20

LEFT OUTER JOIN When SELECTING a LEFT OUTER JOIN from tables t1 and t2 on key k the result contains: All rows from t1 All rows from t2 that match t1 on k or NULL if no match exists. SELECT * FROM t1 LEFT OUTER JOIN t2 ON t1.k=t2.k; Database Design and Web Implementation 21

LEFT OUTER JOIN Database Design and Web Implementation 22

RIGHT OUTER JOIN When SELECTING a RIGHT OUTER JOIN from tables t1 and t2 on key k the result contains: All rows from t2 All rows from t1 that match t2 on k or NULL if no match exists. SELECT * FROM t1 RIGHT OUTER JOIN t2 ON t1.k=t2.k; Database Design and Web Implementation 23

RIGHT OUTER JOIN Database Design and Web Implementation 24

FULL OUTER JOIN When SELECTING a FULL OUTER JOIN from tables t1 and t2 on key k the result contains: All rows from t2 All rows from t1 NULL values for t1 and t2 rows where there exists no match on key k. No straightforward way of typing this in MySQL USE Union instead Database Design and Web Implementation 25

FULL OUTER JOIN Database Design and Web Implementation 26

PHP PHP = The PHP Hypertext Preprocessor Invented in 1995 Server Side Scripting Language (deets on next slide) Running on 244 Million Websites Evan Sandhaus is not a huge fan Database Design and Web Implementation 27

PHP - Server Side Scripting Language Database Design and Web Implementation 28

PHP - Hello World <?php?> echo "Hello World!"; OR <?php?> print "Hello World!"; Database Design and Web Implementation 29

PHP - Variables // numbers $current_year = 2013; $pi = 3.14159; $distance_to_moon = 3.844e6; Database Design and Web Implementation 30

PHP - Variables // booleans $just_true = true; $just_false = false; $or_true = $just_true $just_false; // true $and_true = $just_true && $just_true; //true Database Design and Web Implementation 31

PHP - Variables // Strings $single_quotes = 'Single Lady'; $double_quotes = "By Beyonce"; // String Concatenation $concat = $single_quotes." ".$double_quotes; // "Single Lady By Beyonce" // String Formatting $formatted_string = "Welcome to $current_year!"; // "Welcome to 2013" $format_fail = 'Welcome to $current_year!'; // "Welcome to $current_year" $format_comma = 'Welcome to '.number_format($current_year); // "Weclome to 2,013" Database Design and Web Implementation 32

PHP - Arrays // Arrays // creates an empty array $simple_array = array(); // set position 0 to 1 $simple_array[0] = 1; // create an array with the specified values $simple_array = array( 1,2,3,5,7,11,13 ); // append 17 to the end of the array $simple_array[] = 17; // assign value 5 to $a_prime $a_prime = $simple_array[3]; // assign length of $simple_array (8) to $a_length $a_length = sizeof($simple_array); Database Design and Web Implementation 33

PHP - Dictionaries // Dictionaries - also arrays // but with keys that aren't sequential integers // create an empty array $simple_dictionary = array(); // assign value 'Engel' to key 'Deena' $simple_dictionary['deena'] = 'Engel'; // Inline dictionary declaration $simple_dictionary = array( 'Evan' => 'Sandhaus', 'Tara' => 'Bobiak' ); // Assigns 'Sandhaus' to $lname $lname = $simple_dictionary['evan']; // Assign "Mr. Sandhaus plans to mary Ms. Bobiak" // to $array_concat $array_concat = "Mr. {$simple_dictionary['evan']} plans to mary Ms. {$simple_dictionary['tara']}"; Database Design and Web Implementation 34

Foreach Loops $simple_dictionary = array( 'Evan' => 'Sandhaus', 'Tara' => 'Bobiak' ); // Simple foreach loop // Prints out // Evan Sandhaus // Tara Bobiak foreach ($simple_dictionary as $key => $value) { echo "$key $value<br>"; } echo "<br>"; Database Design and Web Implementation 35

PHP Built-In Fuctions is_array(x), is_boolean(x), is_string(x) : True if x is of the specified type. implode(glue, array) create a string containing each of the elements from array separated by the 'glue' string. isset(array[key]): Returns true if key exists in array, false otherwise. sizeof(array), count(array): Return the size of an array. http://php.net/quickref.php Database Design and Web Implementation 36

What Is Truth When converting to boolean, the following values are considered FALSE: the boolean FALSE itself the integer 0 (zero) the float 0.0 (zero) the empty string, and the string "0" an array with zero elements an object with zero member variables (PHP 4 only) the special type NULL (including unset variables) Database Design and Web Implementation 37

PHP Selective Blocks <html>! <head>!! <title>insert Demo</title>! </head>! <body>!! <?php if ($condition) :?>!!! <!-- Printed iff $condition is true -->!! <?php elseif($condition_2):?>!!! <!-- Printed iff $condition is false and $condition_2 is true -->!! <?php else:?>!!! <!-- Printed if both $condition and $condition_2 are false. -->!! <?php endif;?>! </body> </html> Database Design and Web Implementation 38

Form <form action="cms.php" method="post" > Database Design and Web Implementation 39

Inputs <input type="text" name='headline'/> <select name='issue'> <option value='winter'>winter</option> <option value='spring'>spring</option> <option value='summer'>summer</option> <option value='fall'>fall</option> </select> Database Design and Web Implementation 40

Inputs Continued <input type='radio' name='content_type' value='news'>news</input> <input type='radio' name='content_type' value='opinion'>opinion</input> <textarea rows="10" cols="60" name='article_body'></textarea> <input type="checkbox" name="tag[]" value="us">united States<br> <input type="checkbox" name="tag[]" value="world">world<br> <input type="checkbox" name="tag[]" value="sports">sports<br> <input type="checkbox" name="tag[]" value="entertainment">entertainment<br> Database Design and Web Implementation 41

Hidden Inputs In Forms <input type='hidden' value='hidden_value' name='some_name'> Database Design and Web Implementation 42

Submit <input type="submit" value="submit"> Database Design and Web Implementation 43

PHP & MySQL connect $database_connection = mysqli_connect( $host, $username, $password, $database_name ); Database Design and Web Implementation 44

PHP Mysql Query $results = mysqli_query( $database_connection, $sql ); Database Design and Web Implementation 45

PHP Mysql Handle Results while ($row = $result->fetch_assoc()) {... } Iterates over all rows returned by the database as an associative array between column names and values. Database Design and Web Implementation 46

MYSQLI - More Goodies $results->num_rows; Return the number of rows in the result $results->fetch_row() Return the results as a numeric array (not associative). $results->fetch_array (MYSQLI_ASSOC MYSQLI_NUM MYSQLI_BOTH) Return the results as a the specified type of array Database Design and Web Implementation 47

A bit more about mysqli_query() $results = mysqli_query($database_connection, $sql); What is results set to? A mysqli_result for SELECT, SHOW, DESCRIBE or EXPLAIN queries. TRUE for all other successful queries FALSE if the query fails. Database Design and Web Implementation 48

PHP Query Security PHP Will often take user inputs and place those inputs into a query. Malicious actors (bad hackers) can embed MySQL commands into their inputs that can affect the underlying database and inadvertently expose your private data. Database Design and Web Implementation 49

Protecting Against SQL Injection Use the mysqli_real_escape_string on ALL database inputs. E.G. $sql = "SELECT * FROM x WHERE y ='". mysqli_real_escape_string($z)."'";; Database Design and Web Implementation 50

MySQL Architecture (Simplified) Appplication Network Connection MySQL Server Database Design and Web Implementation 51

SQLite Architecture Simplified Appplication Local I/O File System SQLite Library Database Design and Web Implementation 52

Mongo The Basics By Analogy MySQL database table row column primary key Mongo database collection document field primary key Database Design and Web Implementation 53

What is a Document? A document is a collection of key-value pairs that looks an awful lot like JSON. JSON? - Let's review. Database Design and Web Implementation 54

JSON - Remember Me? { "numeric_key" : 9, "string_key" : "Hello", "bool_true_key" : true, "bool_false_key" : false, "null_key" "array_key" : null, : [true,"2","three"], "dictionary_key" : { } "a" : "there" } Database Design and Web Implementation 55

_id Every document inserted into a collection is assigned a unique _id field if no such field is specified. _id is generated based on a number of factors (time, previous inserts, current machine). Database Design and Web Implementation 56

MongoDB - Quick Review Use a database use database_name Insert record db.collection.insert({<document>}); Show all databases show dbs Show all collections show collections Database Design and Web Implementation 57

Finding Basic db.collection.find() Exact Match db.collection.find({"k1" : "v1"}) Comparison: db.collection.find({"k1" : {$lt : "v1"}}) $lt, $lte, $gt, $gte, $ne Negation db.collection.find({"k1 : {$not : {expr}}}) Membership db.collection.find({"k1" : {$in : ["v1","v2",...,"vn"]} Existence Testing db.collection.find("k1" : {$exists : true}) Database Design and Web Implementation 58

More Finding With Limit db.collection.find().limit(n) With Sorting db.collection.find().sort({"key_1" : 1-1, "key_2" : 1-1}) Count db.collection.find().count() Database Design and Web Implementation 59

Being Distinct Get all unique values for specific key in collection. db.collection.distinct("k1") Get number of distinct values db.collection.distinct("k1").length This is different syntax than count Database Design and Web Implementation 60

Projecting Return all fields db.collection.find({},{}); Return only specified field (and _id) db.collection.find({},{"k1" : 1}); Return all but specific field db.collection.find({},{"k1" : 0}) Return only specific fields (without _id) db.collection.find({},{"k1" : 1, "_id" : 0}); Database Design and Web Implementation 61

Finally - The Final Areas we will cover Relational Design MySQL PHP PHP + MySQL SQLite MongoDB Database Design and Web Implementation 62