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

Similar documents
Introduction to SQL on GRAHAM ED ARMSTRONG SHARCNET AUGUST 2018

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

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

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

Chapter 3 Introduction to relational databases and MySQL

Chapter 8 How to work with data types

MySQL: an application

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

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

Programming and Database Fundamentals for Data Scientists

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

Data and Tables. Bok, Jong Soon

Basic SQL. Basic SQL. Basic SQL

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

MySQL Creating a Database Lecture 3

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

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

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

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

SQL: Data Definition Language

Overview of MySQL Structure and Syntax [2]

Simple Quesries in SQL & Table Creation and Data Manipulation

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

Mastering phpmyadmiri 3.4 for

PHP and MySQL Programming

Acknowledgments About the Authors

More MySQL ELEVEN Walkthrough examples Walkthrough 1: Bulk loading SESSION

Documentation Accessibility. Access to Oracle Support. Supported Browsers

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

Introduction to relational databases and MySQL

Data Definition Language with mysql. By Kautsar

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

Daffodil DB. Design Document (Beta) Version 4.0

Relational databases and SQL

Database Systems. phpmyadmin Tutorial

1 INTRODUCTION TO EASIK 2 TABLE OF CONTENTS

The Top 20 Design Tips

THE UNIVERSITY OF AUCKLAND

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

MTAT Introduction to Databases

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

MTAT Introduction to Databases

5. SQL Query Syntax 1. Select Statement. 6. CPS: Database Schema

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

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

Department of Computer Science University of Cyprus. EPL342 Databases. Lab 1. Introduction to SQL Server Panayiotis Andreou

Basis Data Terapan. Yoannita

System Pages (Setup Admins Only)

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

Lecture 1. Monday, August 25, 2014

IELM 511 Information Systems Design Labs 5 and 6. DB creation and Population

Lab 4: Tables and Constraints

GridDB Advanced Edition SQL reference

Department of Computer Science University of Cyprus. EPL342 Databases. Lab 2

PART I SQLAlchemy Core

Using PHP with MYSQL

Introduction to IBM DB2

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

Draft. Students Table. FName LName StudentID College Year. Justin Ennen Science Senior. Dan Bass Management Junior

Using Relational Databases for Digital Research

MySQL: Access Via PHP

PERSİSTENCE OBJECT RELATİON MAPPİNG

INFORMATION TECHNOLOGY NOTES

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

Create Basic Databases and Integrate with a Website Lesson 1

BEGINNING T-SQL. Jen McCown MidnightSQL Consulting, LLC MinionWare, LLC

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

sqoop Automatic database import Aaron Kimball Cloudera Inc. June 18, 2009

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

Exact Numeric Data Types

! An organized collection of data. ! Can easily be accessed, managed, and updated. ! Data are organized as a set of tables.

Angela Henry. Data Types Do Matter

Using IBM-Informix datatypes with IDS 10 and web application server Keshava Murthy, IBM Informix Development

PostgreSQL Installation Guide

Database Programming with PL/SQL

IBM DB2 UDB V7.1 Family Fundamentals.

Carnegie Mellon Univ. Dept. of Computer Science /615 - DB Applications. Today's Party. Example Database. Faloutsos/Pavlo CMU /615

GridDB Advanced Edition SQL reference

SQL OVERVIEW. CS121: Relational Databases Fall 2017 Lecture 4

The connection has timed out

Lecture 13: MySQL and PHP. Monday, March 26, 2018

Transbase R PHP Module

Chapters 10 & 11 PHP AND MYSQL

ICS4U Project Development Example Discovery Day Project Requirements. System Description

SQL Functionality SQL. Creating Relation Schemas. Creating Relation Schemas

Introduction to SQL Server 2005/2008 and Transact SQL

Databases and MySQL: The Basics

Database and MySQL Temasek Polytechnic

How to Migrate Microsoft SQL Server Connections from the OLE DB to the ODBC Provider Type

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)

Data Definition and Data Manipulation. Lecture 5: SQL s Data Definition Language CS1106/CS5021/CS6503 Introduction to Relational Databases

Lecture 5: SQL s Data Definition Language

SQL DDL. Intro SQL CREATE TABLE ALTER TABLE Data types Service-based database in Visual Studio Database in PHPMyAdmin

GlobAl EDITION. Database Concepts SEVENTH EDITION. David M. Kroenke David J. Auer

Operating systems fundamentals - B07

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

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

MySQL. A practical introduction to database design

MySQL Workshop. Scott D. Anderson

Introduction to Databases

Transcription:

Often times, in order for us to build the most functional website we can, we depend on a database to store information. If you ve ever used Microsoft Excel or Google Spreadsheets (among others), odds are you re familiar with the notion of a database: a hierarchically organized set of tables, each of which contains a set of rows and columns.

SQL (the Structured Query Language) is a programming language whose purpose is to query a database. MySQL is an open-source platform on which you can establish the type of relational database that SQL is most adept at working with. Many installations of MySQL come with a GUI tool called phpmyadmin which can be used to execute database queries in a more user-friendly way.

After you create a database, the next thing you ll most likely want to do is create a table. The syntax for doing this is actually a bit awkward to do programmatically, at least at the outset, and so this is where phpmyadmin will come in handy. As part of the process of creating a table, you ll be asked to specify all of the columns in that table. Thereafter, all your queries will refer to rows of the table.

Each column of your SQL table is capable of holding data of a particular data type.

Each column of your SQL table is capable of holding data of a particular data type. INT SMALLINT TINYINT MEDIUMINT BIGINT DECIMAL FLOAT BIT DATE TIME DATETIME TIMESTAMP CHAR VARCHAR BINARY BLOB TEXT ENUM GEOMETRY LINESTRING

Each column of your SQL table is capable of holding data of a particular data type. INT SMALLINT TINYINT MEDIUMINT BIGINT DECIMAL FLOAT BIT DATE TIME DATETIME TIMESTAMP CHAR VARCHAR BINARY BLOB TEXT ENUM GEOMETRY LINESTRING

Each column of your SQL table is capable of holding data of a particular data type. INT SMALLINT TINYINT MEDIUMINT BIGINT DECIMAL FLOAT BIT DATE TIME DATETIME TIMESTAMP CHAR VARCHAR BINARY BLOB TEXT ENUM GEOMETRY LINESTRING

Each column of your SQL table is capable of holding data of a particular data type. INT SMALLINT TINYINT MEDIUMINT BIGINT DECIMAL FLOAT BIT DATE TIME DATETIME TIMESTAMP CHAR VARCHAR BINARY BLOB TEXT ENUM GEOMETRY LINESTRING

Each column of your SQL table is capable of holding data of a particular data type. INT SMALLINT TINYINT MEDIUMINT BIGINT DECIMAL FLOAT BIT DATE TIME DATETIME TIMESTAMP CHAR VARCHAR BINARY BLOB TEXT ENUM GEOMETRY LINESTRING

Each column of your SQL table is capable of holding data of a particular data type. INT SMALLINT TINYINT MEDIUMINT BIGINT DECIMAL FLOAT BIT DATE TIME DATETIME TIMESTAMP CHAR VARCHAR BINARY BLOB TEXT ENUM GEOMETRY LINESTRING

Each column of your SQL table is capable of holding data of a particular data type. INT SMALLINT TINYINT MEDIUMINT BIGINT DECIMAL FLOAT BIT DATE TIME DATETIME TIMESTAMP CHAR VARCHAR BINARY BLOB TEXT ENUM GEOMETRY LINESTRING

Each column of your SQL table is capable of holding data of a particular data type. INT SMALLINT TINYINT MEDIUMINT BIGINT DECIMAL FLOAT BIT DATE TIME DATETIME TIMESTAMP CHAR VARCHAR BINARY BLOB TEXT ENUM GEOMETRY LINESTRING

Each column of your SQL table is capable of holding data of a particular data type. INT SMALLINT TINYINT MEDIUMINT BIGINT DECIMAL FLOAT BIT DATE TIME DATETIME TIMESTAMP CHAR VARCHAR BINARY BLOB TEXT ENUM GEOMETRY LINESTRING

Unlike in C, the CHAR data type in SQL does not refer to a single character. Rather, it is a fixed-length string. In most relational databases, including MySQL, you actually specify the fixed-length as part of the type definition, e.g. CHAR(10). A VARCHAR refers to a variable-length string. VARCHARs also require you to specify the maximum possible length of a string that could be stored in that column, e.g. VARCHAR(99).

One other important consideration when constructing a table in SQL is to choose one column to be your primary key. Primary keys enable rows of a table to be uniquely and quickly identified. Choosing your primary key appropriately can make subsequent operations on the table much easier. It is also possible to establish a joint primary key a combination of two columns that is always guaranteed to be unique.

SQL is a programming language, but its vocabulary is fairly limited. We will primarily consider just four operations that one may perform on a table.

SQL is a programming language, but its vocabulary is fairly limited. We will primarily consider just four operations that one may perform on a table. INSERT

SQL is a programming language, but its vocabulary is fairly limited. We will primarily consider just four operations that one may perform on a table. INSERT SELECT

SQL is a programming language, but its vocabulary is fairly limited. We will primarily consider just four operations that one may perform on a table. INSERT SELECT UPDATE

SQL is a programming language, but its vocabulary is fairly limited. We will primarily consider just four operations that one may perform on a table. INSERT SELECT UPDATE DELETE

users idnum username password fullname 10 jerry fus!ll! Jerry Seinfeld 11 gcostanza b0sc0 George Costanza moms username jerry gcostanza mother Helen Seinfeld Estelle Costanza

INSERT Add information to a table.

INSERT Add information to a table. INSERT INTO <table> (<columns>) VALUES (<values>)

INSERT Add information to a table. INSERT INTO users (username, password, fullname) VALUES ( newman, USMAIL, Newman )

users idnum username password fullname 10 jerry fus!ll! Jerry Seinfeld 11 gcostanza b0sc0 George Costanza moms username jerry gcostanza mother Helen Seinfeld Estelle Costanza

users idnum username password fullname 10 jerry fus!ll! Jerry Seinfeld 11 gcostanza b0sc0 George Costanza 12 newman USMAIL Newman moms username jerry gcostanza mother Helen Seinfeld Estelle Costanza

users idnum username password fullname 10 jerry fus!ll! Jerry Seinfeld 11 gcostanza b0sc0 George Costanza 12 newman USMAIL Newman moms username jerry gcostanza mother Helen Seinfeld Estelle Costanza

When defining the column that ultimately ends up being your table s primary key, it s usually a good idea to have that column be an integer. Moreover, so as to eliminate the situation where you may accidentally forget to specify a real value for the primary key column, you can configure that column to autoincrement, so it will pre-populate that column for you automatically when rows are added to the table.

users idnum username password fullname 10 jerry fus!ll! Jerry Seinfeld 11 gcostanza b0sc0 George Costanza 12 newman USMAIL Newman moms username jerry gcostanza mother Helen Seinfeld Estelle Costanza

INSERT Add information to a table. INSERT INTO moms (username, mother) VALUES ( kramer, Babs Kramer )

users idnum username password fullname 10 jerry fus!ll! Jerry Seinfeld 11 gcostanza b0sc0 George Costanza 12 newman USMAIL Newman moms username jerry gcostanza mother Helen Seinfeld Estelle Costanza

users idnum username password fullname 10 jerry fus!ll! Jerry Seinfeld 11 gcostanza b0sc0 George Costanza 12 newman USMAIL Newman moms username jerry gcostanza kramer mother Helen Seinfeld Estelle Costanza Babs Kramer

SELECT Extract information from a table.

SELECT Extract information from a table. SELECT <columns> FROM <table> WHERE <condition> ORDER BY <column>

SELECT Extract information from a table. SELECT <columns> FROM <table> WHERE <predicate> ORDER BY <column>

SELECT Extract information from a table. SELECT idnum, fullname FROM users

users idnum username password fullname 10 jerry fus!ll! Jerry Seinfeld 11 gcostanza b0sc0 George Costanza 12 newman USMAIL Newman moms username jerry gcostanza kramer mother Helen Seinfeld Estelle Costanza Babs Kramer

users idnum username password fullname 10 jerry fus!ll! Jerry Seinfeld 11 gcostanza b0sc0 George Costanza 12 newman USMAIL Newman moms username jerry gcostanza kramer mother Helen Seinfeld Estelle Costanza Babs Kramer

SELECT Extract information from a table. SELECT password FROM users WHERE idnum < 12

users idnum username password fullname 10 jerry fus!ll! Jerry Seinfeld 11 gcostanza b0sc0 George Costanza 12 newman USMAIL Newman moms username jerry gcostanza kramer mother Helen Seinfeld Estelle Costanza Babs Kramer

users idnum username password fullname 10 jerry fus!ll! Jerry Seinfeld 11 gcostanza b0sc0 George Costanza 12 newman USMAIL Newman moms username jerry gcostanza kramer mother Helen Seinfeld Estelle Costanza Babs Kramer

SELECT Extract information from a table. SELECT * FROM moms WHERE username = jerry

users idnum username password fullname 10 jerry fus!ll! Jerry Seinfeld 11 gcostanza b0sc0 George Costanza 12 newman USMAIL Newman moms username jerry gcostanza kramer mother Helen Seinfeld Estelle Costanza Babs Kramer

users idnum username password fullname 10 jerry fus!ll! Jerry Seinfeld 11 gcostanza b0sc0 George Costanza 12 newman USMAIL Newman moms username jerry gcostanza kramer mother Helen Seinfeld Estelle Costanza Babs Kramer

Databases empower us to organize information into tables efficiently. We don t always need to store every possible relevant piece of information in the same table, but can use relationships across the tables to let us pull information from where we need it.

users idnum username password fullname 10 jerry fus!ll! Jerry Seinfeld 11 gcostanza b0sc0 George Costanza 12 newman USMAIL Newman moms username jerry gcostanza kramer mother Helen Seinfeld Estelle Costanza Babs Kramer

Databases empower us to organize information into tables efficiently. We don t always need to store every possible relevant piece of information in the same table, but can use relationships across the tables to let us pull information from where we need it. What if we now find ourselves in a situation where we need to get a user s full name (from the users table) and their mother s name (from the mother table).

SELECT (JOIN) Extract information from multiple tables.

SELECT (JOIN) Extract information from multiple tables. SELECT <columns> FROM <table1> JOIN <table2> ON <predicate>

SELECT (JOIN) Extract information from multiple tables. SELECT users.fullname, moms.mother FROM users JOIN moms ON users.username = moms.username

SELECT (JOIN) Extract information from multiple tables. SELECT users.fullname, moms.mother FROM users JOIN moms ON users.username = moms.username

users idnum username password fullname 10 jerry fus!ll! Jerry Seinfeld 11 gcostanza b0sc0 George Costanza 12 newman USMAIL Newman moms username jerry gcostanza kramer mother Helen Seinfeld Estelle Costanza Babs Kramer

users idnum username password fullname 10 jerry fus!ll! Jerry Seinfeld 11 gcostanza b0sc0 George Costanza 12 newman USMAIL Newman moms username jerry gcostanza kramer mother Helen Seinfeld Estelle Costanza Babs Kramer

users idnum username password fullname 10 jerry fus!ll! Jerry Seinfeld 11 gcostanza b0sc0 George Costanza 12 newman USMAIL Newman moms username jerry gcostanza kramer mother Helen Seinfeld Estelle Costanza Babs Kramer

users & moms users.idnum users.username moms.username users.password users.fullname moms.mother 10 jerry fus!ll! Jerry Seinfeld Helen Seinfeld 11 gcostanza b0sc0 George Costanza Estelle Costanza

users & moms users.idnum users.username moms.username users.password users.fullname moms.mother 10 jerry fus!ll! Jerry Seinfeld Helen Seinfeld 11 gcostanza b0sc0 George Costanza Estelle Costanza

UPDATE Modify information in a table.

UPDATE Modify information in a table. UPDATE <table> SET <column> = <value> WHERE <predicate>

UPDATE Modify information in a table. UPDATE users SET password = yadayada WHERE idnum = 10

users idnum username password fullname 10 jerry fus!ll! Jerry Seinfeld 11 gcostanza b0sc0 George Costanza 12 newman USMAIL Newman moms username jerry gcostanza kramer mother Helen Seinfeld Estelle Costanza Babs Kramer

users idnum username password fullname 10 jerry yadayada Jerry Seinfeld 11 gcostanza b0sc0 George Costanza 12 newman USMAIL Newman moms username jerry gcostanza kramer mother Helen Seinfeld Estelle Costanza Babs Kramer

DELETE Remove information from a table.

DELETE Remove information from a table. DELETE FROM <table> WHERE <predicate>

DELETE Remove information from a table. DELETE FROM users WHERE username = newman

users idnum username password fullname 10 jerry yadayada Jerry Seinfeld 11 gcostanza b0sc0 George Costanza 12 newman USMAIL Newman moms username jerry gcostanza kramer mother Helen Seinfeld Estelle Costanza Babs Kramer

users idnum username password fullname 10 jerry yadayada Jerry Seinfeld 11 gcostanza b0sc0 George Costanza moms username jerry gcostanza kramer mother Helen Seinfeld Estelle Costanza Babs Kramer

All of these operations are pretty easy to do in the graphical interface of phpmyadmin. We want a way to do this programmatically, not just typing SQL commands into the SQL tab of phpmyadmin. Fortunately, SQL integrates with PHP very well, by way of functions like query().

After you ve connected to your database with PHP (using a process called MySQLi or PDO), you can make pass query strings as arguments to functions in PHP and store the result set in an associative array.

After you ve connected to your database with PHP (using a process called MySQLi or PDO), you can make pass query strings as arguments to functions in PHP and store the result set in an associative array. $results = query( SELECT fullname FROM users WHERE idnum = 10 );

After you ve connected to your database with PHP (using a process called MySQLi or PDO), you can make pass query strings as arguments to functions in PHP and store the result set in an associative array. $results = query( SELECT fullname FROM users WHERE idnum = 10 );

After you ve connected to your database with PHP (using a process called MySQLi or PDO), you can make pass query strings as arguments to functions in PHP and store the result set in an associative array. $results = query( SELECT fullname FROM users WHERE idnum = 10 );

After you ve connected to your database with PHP (using a process called MySQLi or PDO), you can make pass query strings as arguments to functions in PHP and store the result set in an associative array. $results = query( SELECT fullname FROM users WHERE idnum = 10 ); print( Thanks for logging in, {$results[ fullname ]}! );

After you ve connected to your database with PHP (using a process called MySQLi or PDO), you can make pass query strings as arguments to functions in PHP and store the result set in an associative array. $results = query( SELECT fullname FROM users WHERE idnum =?, $_SESSION[ id ]); print( Thanks for logging in, {$results[ fullname ]}! );

After you ve connected to your database with PHP (using a process called MySQLi or PDO), you can make pass query strings as arguments to functions in PHP and store the result set in an associative array. It s also possible your result set might consist of multiple rows, in which case the result set would be an array of associative arrays, so just need to iterate through it!

<p>the moms of TV s Seinfeld:</p> <table> <?php $results = query( SELECT mothers FROM moms );?> if($results!== false) { foreach($results as $result) { print( <tr><td>. $result[ mothers ]. </td></tr> ); } } </table>

<p>the moms of TV s Seinfeld:</p> <table> <?php $results = query( SELECT mothers FROM moms );?> if($results!== false) { foreach($results as $result) { print( <tr><td>. $result[ mothers ]. </td></tr> ); } } </table>

<p>the moms of TV s Seinfeld:</p> <table> <?php $results = query( SELECT mothers FROM moms );?> if($results!== false) { foreach($results as $result) { print( <tr><td>. $result[ mothers ]. </td></tr> ); } } </table>

<p>the moms of TV s Seinfeld:</p> <table> <?php $results = query( SELECT mothers FROM moms );?> if($results!== false) { foreach($results as $result) { print( <tr><td>. $result[ mothers ]. </td></tr> ); } } </table>