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

TINYINT[(M)] [UNSIGNED] [ZEROFILL] A very small integer. The signed range is -128 to 127. The unsigned range is 0 to 255.

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

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

General References on SQL (structured query language) SQL tutorial.

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

Chapter 8 How to work with data types

Chapter 3 Introduction to relational databases and MySQL

Introduction to SQL on GRAHAM ED ARMSTRONG SHARCNET AUGUST 2018

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

Data Types in MySQL CSCU9Q5. MySQL. Data Types. Consequences of Data Types. Common Data Types. Storage size Character String Date and Time.

Overview of MySQL Structure and Syntax [2]

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

Data and Tables. Bok, Jong Soon

More MySQL ELEVEN Walkthrough examples Walkthrough 1: Bulk loading SESSION

Basis Data Terapan. Yoannita

Data Definition Language with mysql. By Kautsar

Careerarm.com. 1. What is MySQL? MySQL is an open source DBMS which is built, supported and distributed by MySQL AB (now acquired by Oracle)

Tool/Web URL Features phpmyadmin. More on phpmyadmin under User Intefaces. MySQL Query Browser

Advanced SQL. Nov 21, CS445 Pacific University 1

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

*this is a guide that was created from lecture videos and is used to help you gain an understanding of what is mysql used for.

Create Basic Databases and Integrate with a Website Lesson 1

Oracle SQL Developer. Supplementary Information for MySQL Migrations Release 2.1 E

Acknowledgments About the Authors

MySQL Introduction. By Prof. B.A.Khivsara

Standard Query Language: Current standard Logical Schema Design: Schema Definition with SQL (DDL)

DB Programming. Database Systems

Selections. Lecture 4 Sections Robb T. Koether. Hampden-Sydney College. Wed, Jan 22, 2014

Vidya Pratishthan s College Of Engineering, Baramati. Laboratory Manual Database Management System Laboratory

MySQL Schema Best Practices

MySQL: an application

EE 495M - Lecture 3. Overview

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

MySQL by Examples for Beginners

MySQL Schema Review 101

Basic SQL. Basic SQL. Basic SQL

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

Percona XtraDB: Compressed Columns with Dictionaries an alternative to InnoDB table compression. Yura Sorokin, Senior Software Engineer at Percona

Mysql Information Schema Update Time Null >>>CLICK HERE<<< doctrine:schema:update --dump-sql ALTER TABLE categorie

SQL: Concepts. Todd Bacastow IST 210: Organization of Data 2/17/ IST 210

Exact Numeric Data Types

Percona XtraDB: Compressed Columns with Dictionaries an alternative to InnoDB table compression. Yura Sorokin, Senior Software Engineer at Percona

Programming and Database Fundamentals for Data Scientists

Basic SQL. Dr Fawaz Alarfaj. ACKNOWLEDGEMENT Slides are adopted from: Elmasri & Navathe, Fundamentals of Database Systems MySQL Documentation

DATABASE MANAGEMENT SYSTEMS

Advanced MySQL Query Tuning

Writing High Performance SQL Statements. Tim Sharp July 14, 2014

Lecture 1. Monday, August 25, 2014

Simple Quesries in SQL & Table Creation and Data Manipulation

Chapter 9: MySQL for Server-Side Data Storage

ALTER TABLE Improvements in MARIADB Server. Marko Mäkelä Lead Developer InnoDB MariaDB Corporation

MTAT Introduction to Databases

CIS 363 MySQL. Chapter 4 MySQL Connectors Chapter 5 Data Types Chapter 6 Identifiers

High Performance MySQL Practical Tuesday, April 01, :45

The Top 20 Design Tips

CS 327E Lecture 11. Shirley Cohen. March 2, 2016

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

MySQL 101. Designing effective schema for InnoDB. Yves Trudeau April 2015

SQL. Often times, in order for us to build the most functional website we can, we depend on a database to store information.

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

Oral Questions and Answers (DBMS LAB) Questions & Answers- DBMS

SQL Functionality SQL. Creating Relation Schemas. Creating Relation Schemas

How to Use JSON in MySQL Wrong

3.2.4 Enforcing constraints

Deepak Bhinde PGT Comp. Sc.

tablename ORDER BY column ASC tablename ORDER BY column DESC sortingorder, } The WHERE and ORDER BY clauses can be combined in one

SQL Data Definition and Data Manipulation Languages (DDL and DML)

What is MySQL? [Document provides the fundamental operations of PHP-MySQL connectivity]

MySQL Workshop. Scott D. Anderson

Mysql Insert Manual Datetime Format Dd Mm Yyyy

Data Storage and Query Answering. Data Storage and Disk Structure (4)

Server 2 - MySQL #1 Lab

Lab # 4. Data Definition Language (DDL)

MySQL and MariaDB. March, Introduction 3

GridDB Advanced Edition SQL reference

Installing and Configuring Oracle GoldenGate for MySQL 12c (12.1.2)

Database Migration with MySQL Workbench. Alfredo Kojima, Team Lead MySQL Workbench Mike Frank, Product Manager

MTAT Introduction to Databases

SAS/ACCESS Supplement for MySQL. SAS/ACCESS for Relational Databases

SQL: Data Definition Language

Advanced MySQL Query Tuning

SQL Commands & Mongo DB New Syllabus

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

SQL OVERVIEW. CS121: Relational Databases Fall 2017 Lecture 4

Senturus Analytics Connector. User Guide Cognos to Power BI Senturus, Inc. Page 1

Data File Header Structure for the dbase Version 7 Table File

MySQL Creating a Database Lecture 3

OKC MySQL Users Group

WHAT IS A DATABASE? There are at least six commonly known database types: flat, hierarchical, network, relational, dimensional, and object.

QMP 7.1 D/D Channabasaveshwara Institute of Technology (An ISO 9001:2008 Certified Institution) NH 206 (B.H. Road), Gubbi, Tumkur Karnataka.

1 INTRODUCTION TO EASIK 2 TABLE OF CONTENTS

MariaDB 10.3 vs MySQL 8.0. Tyler Duzan, Product Manager Percona

Applied Databases. Sebastian Maneth. Lecture 7 Simple SQL Queries. University of Edinburgh - February 1 st, 2016

CSCI-UA: Database Design & Web Implementation. Professor Evan Sandhaus Lecture #7: Normalization

Part 1: IoT demo Part 2: MySQL, JSON and Flexible storage

Unit 1 - Chapter 4,5

Mysql Manually Set Auto Increment To 1000

Transcription:

CSCI-UA:0060-02 Database Design & Web Implementation Professor Evan Sandhaus sandhaus@cs.nyu.edu evan@nytimes.com Lecture #15: Post Spring Break Massive MySQL Review Database Design and Web Implementation

Database Design and Web Implementation 2

Administrivia Midterm Readings: Read through chapter 10 if you haven t already Homework Homework 5 is due Thursday Database Design and Web Implementation 3

On Today s Menu Creating tables Inserting data Selecting Data Updating Data Deleting Data Database Design and Web Implementation 4

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 5

Create Table Syntax [column_name] [column_type] [column_details] CREATE TABLE example ( example_id int(11) NOT AUTO_INCREMENT, example_col1 varchar(255) DEFAULT Evan, example_col2 timestamp NOT DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP ); Database Design and Web Implementation 6

Create Table Syntax KEYS!! CREATE TABLE example ( example_id int(11) NOT AUTO_INCREMENT, example_col1 varchar(255) DEFAULT Evan, example_col2 timestamp NOT DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, PRIMARY_KEY (example_id), UNIQUE KEY(example_col1), KEY(example_col2) ); Database Design and Web Implementation 7

Create Table Syntax Engine, Charset, Collate CREATE TABLE example ( example_id int(11) NOT AUTO_INCREMENT, example_col1 varchar(255) DEFAULT Evan, example_col2 timestamp NOT DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP ) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_swedish_ci; Database Design and Web Implementation 8

Data Types Type {storage} Name Range Attributes Default Numeric {1 byte} Numeric {2 bytes} Numeric {3 bytes} Numeric {4 bytes} Numeric {8 bytes} Numeric {4 or 8} Numeric {4 bytes} Numeric {8 bytes} Numeric {M+2} Bit {8 bytes} TINYINT[(M)] -128 TO 127 [0 to 255 if UNSIGNED] SMALLINT[(M)] -32,768 to 32,767 [0 to 65,535] MEDIUMINT[(M)] -8,388,608 to 8,388,607 [0 to 16,777,215] INT[(M)] BIGINT[(M)] FLOAT(p) FLOAT[(M,D)] DOUBLE[(M,D)] DECIMAL[(M,[D])] Stored as string BIT[(M)] -2,147,483,648 to 2,147,483,647 [0 to 4,294,967,295] -/+9.223E+18 [0 to 18.45E+18] p=0-24 --> "FLOAT" p=25-53 --> "DOUBLE" Min=+/-1.175E-38 Max=+/-3.403E+38 Min=+/-2.225E-308 Max=+/-1.800E+308 Max Range = DOUBLE range Fixed point vs. DOUBLE float Binary. Display by [add zero converting with BIN()]. M=1-64 AUTO_INCREMENT UNSIGNED, ZEROFILL, SERIAL DEFAULT VALUE AUTO_INCREMENT, UNSIGNED, ZEROFILL, SERIAL DEFAULT VALUE AUTO_INCREMENT, UNSIGNED, ZEROFILL, SERIAL DEFAULT VALUE AUTO_INCREMENT, UNSIGNED, ZEROFILL, SERIAL DEFAULT VALUE AUTO_INCREMENT, UNSIGNED, ZEROFILL UNSIGNED, ZEROFILL UNSIGNED, ZEROFILL UNSIGNED, ZEROFILL UNSIGNED, ZEROFILL Prior to 5.03 TINYINT(1) Synonym Source: http://kimbriggs.com/computers/computer-notes/mysql-notes/mysql-data-types-50.file [0 if NOT ] [0 if NOT ] [0 if NOT ] [0 if NOT ] [0 if NOT ] [0 if NOT ] [0 if NOT ] [0 if NOT ] [0 if NOT ] [0 if NOT ] Database Design and Web Implementation 9

Data Types Type {storage} Name Range Attributes Default String {M char's} String {M char's1} String {#char's1} String {#char's1} String {#char's1} String {#char's1} CHAR[(M)] VARCHAR(M) M=0-255 Characters, FIXED. Right padded with spaces. M=0-65,535 Characters M=0-255 <v5.0.3 BINARY, CHARACTER SET BINARY, CHARACTER SET [ if NOT ] [ if NOT ] TINYTEXT2 0-255 Characters BINARY, CHARACTER SET ["" if NOT ] TEXT2 0-65,535 Char's BINARY, CHARACTER SET ["" if NOT ] MEDIUMTEXT2 0-16,777,215 Char's BINARY, CHARACTER SET ["" if NOT ] LONGTEXT2 0-4,294,967,295 Char's BINARY, CHARACTER SET ["" if NOT ] Source: http://kimbriggs.com/computers/computer-notes/mysql-notes/mysql-data-types-50.file Database Design and Web Implementation 10

Data Types Type {storage} Name Range Attributes Default String {M bytes} String {M bytes} String {#bytes1} String {#bytes1} String {#bytes1} String {#bytes1} String {1-2 bytes} BINARY[(M)] M=0-255 bytes, FIXED. Global Only (case sensitive) VARBINARY(M) 0-65,535 bytes M=0-255 <v5.0.3 Global Only (case sensitive) TINYBLOB 0-255 bytes Global Only (case sensitive) BLOB 0-65,535 bytes Global Only (case sensitive) MEDIUMBLOB 0-16,777,215 bytes Global Only (case sensitive) LONGBLOB 0-4,294,967,295 bytes Global Only (case sensitive) ENUM2 ("A1","A2",...) Column is exactly 1 of values (1-255 values) CHARACTER SET ["" if NOT ] ["" if NOT ] [ if NOT ] [ if NOT ] [ if NOT ] [ if NOT ] [ if NOT ] Source: http://kimbriggs.com/computers/computer-notes/mysql-notes/mysql-data-types-50.file Database Design and Web Implementation 11

Data Types Type {storage} Name Range Attributes Default Date & Time {3 bytes} Date & Time {8 bytes} Date & Time {3 bytes} Date & Time {4 bytes} Date & Time {1 bytes} DATE "1000-01-01" - "9999-12-31" Global Only (YYYY-MM-DD) DATETIME "1000-01-01 00:00:00" - "9999-12-31 23:59:59" TIME "-838:59:59" - "838:59:59" Global Only (hh:mm:ss) TIMESTAMP 19700101000000-2037+ Global Only (YYYY-MM-DD hh:mm:ss) Global Only (YYYYMMDDhhmmss) YEAR 1900-2155 Global Only (YYYY) ["0000-00 -00" if NOT ] ["0000-00 -00 00:00:00" if NOT ] ["00:00:00 " if NOT ] Current Date & Time ["0000" if NOT ] Source: http://kimbriggs.com/computers/computer-notes/mysql-notes/mysql-data-types-50.file Database Design and Web Implementation 12

Article Data { "authors": { "person": [ "Peter APPLEBOME" ] }, "body": "Christopher S. Murphy, a 39-year-old three-term congressman, defeated the former wrestling executive Linda E. McMahon in a long and bitter Senate race.", "headline": "Democrat Elected Senator in Costly Connecticut Race", "publication_date": "2012-11-07", "section": "U.S.", "tags": { "DES": [ "United States Politics and Government", "Voting and Voters" ], "GEO": [ "Connecticut" ], "ORG": [ "Senate" ], "PER": [ "Murphy, Christopher Scott", "McMahon, Linda E" ] } } Database Design and Web Implementation 13

Database Design and Web Implementation 14

CREATE TABLE CREATE TABLE `section` ( `section_name` VARCHAR(45), PRIMARY KEY (`section_name`) ) ENGINE = InnoDB; CREATE TABLE IF `author` ( `author_id` INT NOT AUTO_INCREMENT, `author_name` VARCHAR(255) NOT, `author_type` ENUM('PER','ORG') NOT, PRIMARY KEY (`author_id`) ) ENGINE = InnoDB; Database Design and Web Implementation 15

CREATE TABLE CREATE TABLE `tag` ( `tag_id` INT NOT AUTO_INCREMENT, `tag` VARCHAR(255) NOT, `tag_type` ENUM('PER','ORG','GEO','DES','TTL') NOT, PRIMARY KEY (`tag_id`) ) ENGINE = InnoDB; Database Design and Web Implementation 16

CREATE TABLE CREATE TABLE `article` ( `article_id` INT NOT AUTO_INCREMENT, `article_headline` VARCHAR(255) NOT, `article_publication_date` DATETIME NOT, `article_body` MEDIUMTEXT NOT, `section` VARCHAR(255) NOT, `article_correction` TEXT, PRIMARY KEY (`article_id`), FOREIGN KEY (`section` ) REFERENCES `section` (`section_name` )) ENGINE = InnoDB; Database Design and Web Implementation 17

CREATE TABLE CREATE TABLE `article_tag` ( `article_id` INT NOT, `tag_id` INT NOT, PRIMARY KEY (`article_id`, `tag_id`), FOREIGN KEY (`article_id` ) REFERENCES `article` (`article_id` ) FOREIGN KEY (`tag_id` ) REFERENCES `tag` (`tag_id` )) ENGINE = InnoDB; Database Design and Web Implementation 18

CREATE TABLE CREATE TABLE `article_author` ( `article_id` INT NOT, `author_id` INT NOT, PRIMARY KEY (`article_id`, `author_id`), FOREIGN KEY (`article_id` ) REFERENCES `article` (`article_id` ) FOREIGN KEY (`author_id` ) REFERENCES `author` (`author_id` )) ENGINE = InnoDB; Database Design and Web Implementation 19

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

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 21

Inserting Test Data Insert test article, tag, author and section. Database Design and Web Implementation 22

Loading Data LOAD DATA LOCAL INFILE "[file_name]" INTO TABLE [table_name]( [column_1], [column_2],... [column_n] ); Database Design and Web Implementation 23

Loading Data - From CSV LOAD DATA LOCAL INFILE "[file_name]" INTO TABLE [table_name]( [column_1]... [column_n] ) FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '\n' Database Design and Web Implementation 24

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

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 26

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 27

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

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 29

Select Join Syntax SELECT * FROM table_1 JOIN table_2 ON table_1.id = table_2.id Database Design and Web Implementation 30

Select Alternate Join Syntax SELECT * FROM table_1, table_2 WHERE table_1.id = table_2.id Database Design and Web Implementation 31

Select Alternate Join Syntax SELECT * FROM table_1, table_2, table_3 WHERE table_1.id = table_2.id AND table_2.id = table_3.id Database Design and Web Implementation 32

Select CONCAT SELECT CONCAT(column_1,glue_1,column_2) FROM table_1; Database Design and Web Implementation 33

Select Subselect SELECT column_1 FROM table_1; WHERE column_n < (SELECT FUNC(*) FROM table); Database Design and Web Implementation 34

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 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 35

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 36

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 37