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

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

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

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

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

Relational Database Management Systems for Epidemiologists: SQL Part II

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

Database Management Systems,

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

Data Manipulation Language (DML)

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

DB tutorial using Database.NET dbforge Studio Express and SQLiteSpy

Querying Data with Transact SQL

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

ASSIGNMENT NO Computer System with Open Source Operating System. 2. Mysql

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

1) Introduction to SQL

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

CMP-3440 Database Systems

MySQL Workshop. Scott D. Anderson

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

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

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

CSC Web Programming. Introduction to SQL

Lecture 6 - More SQL

Unit Assessment Guide

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

What is SQL? Toolkit for this guide. Learning SQL Using phpmyadmin

Introduction to database design

NULLs & Outer Joins. Objectives of the Lecture :

Introduction to Database Systems CSE 414

Sql Server Syllabus. Overview

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

This lab will introduce you to MySQL. Begin by logging into the class web server via SSH Secure Shell Client

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

Querying Data with Transact-SQL

CGS 3066: Spring 2017 SQL Reference

Course Outline. MySQL Database Administration & Design. Course Description: Pre-requisites: Course Content:

Provider: MySQLAB Web page:

Midterm Review. Winter Lecture 13

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

Database design process

WHAT IS SQL. Database query language, which can also: Define structure of data Modify data Specify security constraints

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

Activant Solutions Inc. SQL 2005: Basic Data Manipulation

Institute of Aga. Microsoft SQL Server LECTURER NIYAZ M. SALIH

Databases (MariaDB/MySQL) CS401, Fall 2015


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

MySQL for Beginners Ed 3

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

Advanced SQL Tribal Data Workshop Joe Nowinski

Carnegie Mellon Univ. Dept. of Computer Science /615 - DB Applications. Administrivia. Administrivia. Faloutsos/Pavlo CMU /615

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

This lecture. Basic Syntax

Database Technology. Topic 3: SQL. Olaf Hartig.

If Only. More SQL and PHP

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

Institute of Aga. Network Database LECTURER NIYAZ M. SALIH

CHAPTER 16. More on SQL- Grouping Records and Table Joins

UNIT-IV (Relational Database Language, PL/SQL)

This lecture. Databases - SQL II. Counting students. Summary Functions

INTERMEDIATE SQL GOING BEYOND THE SELECT. Created by Brian Duffey

CMP-3440 Database Systems

Databases - SQL II. (GF Royle, N Spadaccini ) Structured Query Language II 1 / 22

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

Joins, NULL, and Aggregation

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

SQL Data Query Language

DATABASE MANAGEMENT SYSTEMS

Advance Database Systems. Joining Concepts in Advanced SQL Lecture# 4

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

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

1. Which of the following will give the same answer irrespective of the NULL values in the specified. a. MIN() b. MAX()

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

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

Exact Numeric Data Types

SIT772 Database and Information Retrieval WEEK 6. RELATIONAL ALGEBRAS. The foundation of good database design

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

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

Introduction to Data Management CSE 344

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

Table Joins and Indexes in SQL

Types. Inner join ( Equi Joins ) Outer(left, right, full) Cross. Prepared By : - Chintan Shah & Pankti Dharwa 2

SQL Commands & Mongo DB New Syllabus

Lab # 3 Hands-On. DML Basic SQL Statements Institute of Computer Science, University of Tartu, Estonia

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

SQL Aggregate Functions

SQL QUERIES. CS121: Relational Databases Fall 2017 Lecture 5

MANAGING DATA(BASES) USING SQL (NON-PROCEDURAL SQL, X401.9)

Olive Backup Exporter

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

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

CSE 344 Midterm. Wednesday, February 19, 2014, 14:30-15:20. Question Points Score Total: 100

Follow these steps to get started: o Launch MS Access from your start menu. The MS Access startup panel is displayed:

Ken s SQL Syntax Refresher

MySQL 5.0 Certification Study Guide

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

KORA. RDBMS Concepts II

Transcription:

CSCI-UA:0060-02 Database Design & Web Implementation Professor Evan Sandhaus sandhaus@cs.nyu.edu evan@nytimes.com Lecture #17: MySQL Gets Done Database Design and Web Implementation

Database Design and Web Implementation 2

Administrivia Midterm - End of Class Homework Homework 6 due 4/4/2013 Database Design and Web Implementation 3

On The Menu GROUP BY Aggregate Functions Join Reviews Reflexive Joins Update Syntax Delete Syntax Complex News Queries Database Design and Web Implementation 4

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 5

Por Exemplo Database Design and Web Implementation 6

CREATE TABLE CREATE TABLE IF NOT EXISTS `track` ( `track_id` INT NOT NULL AUTO_INCREMENT, `artist_name` VARCHAR(255) NOT NULL, `album_name` VARCHAR(255) NOT NULL, `release_year` INT NOT NULL, `track_name` VARCHAR(255) NULL, `disc_number` INT NULL, `track_number` INT NULL, `track_length` FLOAT NULL, `track_popularity` FLOAT NULL, PRIMARY KEY (`track_id`) ) ENGINE = InnoDB; Database Design and Web Implementation 7

LOAD DATA LOAD DATA LOCAL INFILE "/Users/evansandhaus/Documents/NYU_DB/class_17/ green_day.txt" INTO TABLE track FIELDS TERMINATED BY '\t' ENCLOSED BY '"' LINES TERMINATED BY '\n' ( ); artist_name, album_name, release_year, track_name, disc_number, track_number, track_length, track_popularity Database Design and Web Implementation 8

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

Examples Longest Track Shortest Track Number of uniquely named tracks. Average Popularity Per Album. Number of tracks released per year. Number of tracks longer than average track. Most popular track per album. Database Design and Web Implementation 10

World s Simplest Database Database Design and Web Implementation 11

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 12

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 13

INNER JOIN - Visualized Database Design and Web Implementation 14

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 15

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

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 17

LEFT OUTER JOIN Database Design and Web Implementation 18

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 19

RIGHT OUTER JOIN Database Design and Web Implementation 20

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 will cover in next lecture Database Design and Web Implementation 21

FULL OUTER JOIN Database Design and Web Implementation 22

OUTER JOIN - Syntax The following are equivalent SELECT * FROM t1 LEFT OUTER JOIN t2 ON t1.k = t2.k SELECT * FROM t1 LEFT JOIN t2 ON t1.k = t2.k Database Design and Web Implementation 23

UNION UNION allows you to combine the results of two selects. Syntax: (SELECT col_x,col_y FROM table_1) UNION (SELECT col_z,col_q FROM table_2) UNION FACTS!: UNION gives the resulting table col names from t1. UNION eliminates duplicate rows by default. UNION will fail if the joined columns have incompatible types. Database Design and Web Implementation 24

UNION & FULL OUTER JOIN (SELECT * FROM t1 RIGHT OUTER JOIN ON t1.k = t2.k) UNION (SELECT * FROM t1 LEFT OUTER JOIN ON t1.k = t2.k) Database Design and Web Implementation 25

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 26

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 27

Database Design and Web Implementation 28

Query Me This How many tags are there of each type? How many authors are there of each type? Find the article with the most images? Find the article with the most tags? Find the most commonly applied tag? How many articles are authored by more than one author? How many articles are authored by organizations services? Database Design and Web Implementation 29