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

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

Introduction to SQL on GRAHAM ED ARMSTRONG SHARCNET AUGUST 2018

Overview of MySQL Structure and Syntax [2]

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

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

Basis Data Terapan. Yoannita

Chapter 3 Introduction to relational databases and MySQL

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

Chapter 8 How to work with data types

Data and Tables. Bok, Jong Soon

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

Create Basic Databases and Integrate with a Website Lesson 1

Data Definition Language with mysql. By Kautsar

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

Simple Quesries in SQL & Table Creation and Data Manipulation

More MySQL ELEVEN Walkthrough examples Walkthrough 1: Bulk loading SESSION

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)

Programming and Database Fundamentals for Data Scientists

SQL Functionality SQL. Creating Relation Schemas. Creating Relation Schemas

*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.

MySQL: an application

Basic SQL. Basic SQL. Basic SQL

PHP Development - Introduction

Acknowledgments About the Authors

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

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

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

SQL: Data Definition Language

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

4) PHP and MySQL. Emmanuel Benoist. Spring Term Berner Fachhochschule Haute cole spcialise bernoise Berne University of Applied Sciences 1

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

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

Advanced SQL. Nov 21, CS445 Pacific University 1

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

SQL Data Definition Language: Create and Change the Database Ray Lockwood

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

user specifies what is wanted, not how to find it

SQL Commands & Mongo DB New Syllabus

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

Deepak Bhinde PGT Comp. Sc.

HOW TO CREATE AND MAINTAIN DATABASES AND TABLES. By S. Sabraz Nawaz Senior Lecturer in MIT FMC, SEUSL

MySQL Creating a Database Lecture 3

Chapter 3. Introduction to relational databases and MySQL. 2010, Mike Murach & Associates, Inc. Murach's PHP and MySQL, C3

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

Database Applications and Web-Enabled Databases. University of California, Berkeley School of Information IS 257: Database Management

The Top 20 Design Tips

MySQL: Access Via PHP

COSC 304 Introduction to Database Systems SQL DDL. Dr. Ramon Lawrence University of British Columbia Okanagan

MySQL Introduction. By Prof. B.A.Khivsara

Introduction to relational databases and MySQL

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

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

Information Systems Engineering. SQL Structured Query Language DDL Data Definition (sub)language

Exact Numeric Data Types

Lab # 4. Data Definition Language (DDL)

EE 495M - Lecture 3. Overview

School of Information and Computer Technology Sirindhorn International Institute of Technology Thammasat University

Provider: MySQLAB Web page:

D B M G. SQL language: basics. Managing tables. Creating a table Modifying table structure Deleting a table The data dictionary Data integrity

GridDB Advanced Edition SQL reference

Intro to Database Commands


DB Programming. Database Systems

Lecture 07. Spring 2018 Borough of Manhattan Community College

SQL Introduction. CS 377: Database Systems

How to use SQL to create a database

Mysql Insert Manual Datetime Format Dd Mm Yyyy

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

School of Information and Computer Technology Sirindhorn International Institute of Technology Thammasat University

Operating systems fundamentals - B07

MySQL Guide. Meher Krishna Patel. Created on : Octorber, 2017 Last updated : December, More documents are freely available at PythonDSP

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

Principles of Data Management

Short List of MySQL Commands

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

MySQL Workshop. Scott D. Anderson

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

MTAT Introduction to Databases

Full file at

Model Question Paper. Credits: 4 Marks: 140

CSC Web Programming. Introduction to SQL

Course Topics. Microsoft SQL Server. Dr. Shohreh Ajoudanian. 01 Installing MSSQL Server Data types

DS Introduction to SQL Part 1 Single-Table Queries. By Michael Hahsler based on slides for CS145 Introduction to Databases (Stanford)

SQL Data Definition Language

UNIT V ESTABLISHING A DATABASE CONNECTION AND WORKING WITH DATABASE

Chapter 4. Basic SQL. Copyright 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Relational databases and SQL

SQL language. Jaroslav Porubän, Miroslav Biňas, Milan Nosáľ (c)

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

618 Index. BIT data type, 108, 109 BIT_LENGTH, 595f BIT VARYING data type, 108 BLOB data type, 108 Boolean data type, 109

1 INTRODUCTION TO EASIK 2 TABLE OF CONTENTS

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

Objectives. Chapter 10. Developing Object-Oriented PHP. Introduction to Object-Oriented Programming

SQL Structured Query Language Introduction

DATABASE SYSTEMS. Introduction to MySQL. Database System Course, 2016

Introduction to Databases. MySQL Syntax Guide: Day 1

High Performance MySQL Practical Tuesday, April 01, :45

APLIKACJE INTERNETOWE 8 PHP WYKORZYSTANIE BAZY DANYCH MYSQL

Documentation Accessibility. Access to Oracle Support. Supported Browsers

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

Transcription:

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

MySQL Setup, using console Data types Overview Creating users, databases and tables SQL queries INSERT, SELECT, DELETE WHERE, ORDER BY, GROUP BY, LIKE, LIMIT, COUNT(*) Using from PHP Procedural vs. Object-oriented Iterating over results 2

MySQL MySQL history "My ess queue ell", "My sequel" 1995, MySQL AB founded in Sweden 2000, goes open source 2003, 4M active installations, 30K downloads/day 2006, 33% market share, 0.2% of revenue 2008, acquired by Sun for $1B 3

Database popularity http://db-engines.com/en/ranking_trend 4

Some numeric data types Type Storage Signed range TINYINT 1 byte -128 to +127 SMALLINT 2 bytes -32768 to +32767 MEDIUMINT 3 bytes -8388608 to +8388607 INT, INTEGER 4 bytes -2147483648 to +2147483647 BIGINT 8 bytes -9223372036854775808 to 9223372036854775807 Type Storage FLOAT 4 bytes Single precision, approximate DOUBLE 8 bytes Double precision, approximate DECIMAL(x,y) varies Exact value, x significant figures, y decimal places BIT(M) varies Stores 1-64 bits 5

Some string data types Type CHAR(X) VARCHAR(X) BLOB TEXT TINYBLOB TINYTEXT MEDIUMBLOB MEDIUMTEXT LONGBLOB LONGTEXT ENUM Fixed-length text data, 0-255 in length Variable-length text data, 0 to 65,535 in length Binary Large Object, used to store large amounts of binary data such as image or files, 64K max length Large amounts of text data, 64K max length 255 max length 16,777,215 max length 4,294,967,295 max length Enumerated type, string value in a specified set of allowed values 6

Some date/time data types Type DATE DATETIME TIMESTAMP TIME YEAR(M) YYYY-MM-DD YYYY-MM-DD HH:MM:SS YYYYMMDDHHMMSS Automatically update when row changed HH:HMM:SS YY, or YYYY 7

Setting up a database Log in as root Create a new database: CREATE DATABASE grocery; Create a new user, grant privileges: CREATE USER 'username'@'localhost' IDENTIFIED BY 'pwd'; GRANT ALL PRIVILEGES ON grocery.* TO 'username'@'localhost'; 8

Table creation syntax: Creating a table CREATE TABLE table_name (col_name1 col_type1, col_name2 col_type2,...) CREATE TABLE inven ( id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, name VARCHAR(50) NOT NULL, details TEXT, price FLOAT NOT NULL, qty INT NOT NULL ); 9

Insertion syntax: Inserting data into a table INSERT INTO table_name (col_name1, col_name2,...) VALUES (col_val1, col_val2,...); INSERT INTO inven (name, details, price, qty) VALUES ('Apples', 'Ripe apples.', '0.25', 1000); INSERT INTO inven (name, details, price, qty) VALUES ('Apples', 'Rotten apples.', '0.02', 594); INSERT INTO inven VALUES (NULL, 'Apples', 'Ripe apples.', '0.25', 1000); Need to include values for every column if you don't provide column name list! 10

Select syntax: Selecting data form a table SELECT col_name1, col_name2,... FROM table_name [WHERE condition] [GROUP BY col_name] [ORDER BY condition [ASC DESC]] [LIMIT [offset,] rows] SELECT * FROM inven; SELECT name, qty FROM inven; SELECT name, qty FROM inven ORDER BY price LIMIT 2; 11

Select syntax: Selecting data form a table SELECT col_name1, col_name2,... FROM table_name [WHERE condition] [GROUP BY col_name] [ORDER BY condition [ASC DESC]] [LIMIT [offset,] rows] SELECT * FROM inven WHERE qty <= 500; SELECT * FROM inven WHERE name LIKE 'a%'; Any names that begin with the letter a. 12

Select syntax: Selecting data form a table SELECT col_name1, col_name2,... FROM table_name [WHERE condition] [GROUP BY col_name] [ORDER BY condition [ASC DESC]] [LIMIT [offset,] rows] SELECT *, count(*) as freq FROM inven GROUP BY name; Causes generation of a new column that counts number of rows that were aggregated by GROUP BY clause 13

Delete syntax: Deleting data from a table DELETE FROM table_name [WHERE condition] [LIMIT rows] DELETE FROM inven; DELETE FROM inven WHERE qty < 500; 14

Using MySQL from PHP PHP's MySQL extension Original extension mysql_* functions PHP's mysqli extension New improved extension Takes advantage of new MySQL v4.1.3+ features Supported in PHP v5+ Object-oriented interface Support for multiple statements Support for transactions mysqli_* functions 15

Procedural style <?php $mysqli = mysqli_connect("localhost", "webuser", "password", "grocery"); if (mysqli_connect_errno()) { printf("connect failed: %s\n", mysqli_connect_error()); exit(); } $sql = "SELECT * FROM inven"; $res = mysqli_query($mysqli, $sql); if ($res) { while ($newarray = mysqli_fetch_array($res, MYSQLI_ASSOC)) { $name = $newarray['name']; $details = $newarray['details']; $price = $newarray['price']; echo "$name $details $price <br />"; } mysqli_free_result($res); } mysqli_close($mysqli);?> 16

Object-oriented style <?php $mysqli = new mysqli("localhost", "webuser", "password", "grocery"); if ($mysqli->connect_errno) { printf("connect failed: %s\n", $mysqli->connect_error); exit(); } $sql = "SELECT * FROM inven"; $res = $mysqli->query($sql); if ($res) { while ($newarray = $res->fetch_array(mysqli_assoc)) { $name = $newarray['name']; $details = $newarray['details']; $price = $newarray['price']; echo "$name $details $price <br />"; } $res->close(); } $mysqli->close();?> 17

MySQL Summary The most popular open source database More than good enough for most web apps Supports standard SQL syntax SELECT, INSERT, DELETE, UPDATE WHERE, ORDER BY, GROUP BY, LIMIT LIKE, COUNT Use in PHP Procedural style Object-oriented style 18