MySQL is very fast, reliable,
MySQL Tutorial
scalable, and easy to use
MySQL is cross-platform
MySQL is a widely used relational MySQL is compliant with the
database management system ANSI SQL standard
(RDBMS). MySQL was first released in
1995
MySQL is free and open-source. MySQL is developed,
distributed, and supported by
MySQL is ideal for both small and Oracle Corporation
large applications. MySQL is named after co-
founder Monty Widenius's
Examples in Each daughter: My
Chapter Who Uses MySQL?
With our online MySQL editor, you
Huge websites like Facebook,
can edit the SQL statements, and
Twitter, Airbnb, Booking.com,
click on a button to view the result.
Uber, GitHub, YouTube, etc.
Content Management
Example Systems like WordPress,
Drupal, Joomla!, Contao, etc.
SELECT * FROM Customers; A very large number of web
developers around the world
Click on the "Try it Yourself" button
to see how it works.
Show Data On Your
Introduction to Web Site
MySQL To build a web site that shows data
from a database, you will need:
MySQL is a very popular open- An RDBMS database program
source relational database (like MySQL)
management system (RDBMS). A server-side scripting
language, like PHP
What is MySQL? To use SQL to get the data
you want
MySQL is a relational To use HTML / CSS to style
database management the page
system
MySQL is open-source
MySQL is free MySQL RDBMS
MySQL is ideal for both small
and large applications What is RDBMS?
1
RDBMS stands for Relational
Database Management System.
What is a Database
Table?
RDBMS is a program used to
maintain a relational database. A table is a collection of related
data entries, and it consists of
RDBMS is the basis for all modern columns and rows.
database systems such as MySQL,
Microsoft SQL Server, Oracle, and A column holds specific information
Microsoft Access. about every record in the table.
RDBMS uses SQL queries to access A record (or row) is each individual
the data in the database. entry that exists in a table.
Look at a selection from the
Northwind "Customers" table:
CustomerI ContactNam PostalCo Countr
CustomerName Address City
D e de y
German
1 Alfreds Futterkiste Maria Anders Obere Str. 57 Berlin 12209
y
Ana Trujillo Avda. de la
México
2 Emparedados y Ana Trujillo Constitución 05021 Mexico
D.F.
helados 2222
Antonio Moreno Antonio México
3 Mataderos 2312 05023 Mexico
Taquería Moreno D.F.
Thomas
4 Around the Horn 120 Hanover Sq. London WA1 1DP UK
Hardy
Christina
5 Berglunds snabbköp Berguvsvägen 8 Luleå S-958 22 Sweden
Berglund
The columns in the "Customers" A relational database defines
table above are: CustomerID, database relationships in the form
CustomerName, ContactName, of tables. The tables are related to
Address, City, PostalCode and each other - based on data
Country. The table has 5 records common to each.
(rows).
Look at the following three tables
What is a Relational "Customers", "Orders", and
"Shippers" from the Northwind
Database? database:
Customers Table
CustomerI CustomerName ContactNam Address City PostalCo Countr
2
D e de y
German
1 Alfreds Futterkiste Maria Anders Obere Str. 57 Berlin 12209
y
Ana Trujillo Avda. de la
México
2 Emparedados y Ana Trujillo Constitución 05021 Mexico
D.F.
helados 2222
Antonio Moreno Antonio México
3 Mataderos 2312 05023 Mexico
Taquería Moreno D.F.
Thomas
4 Around the Horn 120 Hanover Sq. London WA1 1DP UK
Hardy
Christina
5 Berglunds snabbköp Berguvsvägen 8 Luleå S-958 22 Sweden
Berglund
The relationship between the Orders Table
"Customers" table and the "Orders"
table is the CustomerID column:
OrderI CustomerI EmployeeI OrderDat ShipperI
D D D e D
1996-08-
10278 5 8 2
12
1996-08-
10280 5 2 1
14
1996-09-
10308 2 7 3
18
1996-11-
10355 4 6 1
15
1996-11-
10365 3 3 2
27
1996-12-
10383 4 8 3
16
1996-12-
10384 5 3 3
16
The relationship between the Shippers Table
"Orders" table and the "Shippers"
table is the ShipperID column:
ShipperI ShipperNam
Phone
D e
Speedy (503) 555-
1
Express 9831
2 United (503) 555-
3
Package 3199
Federal (503) 555-
3
Shipping 9931
Semicolon is the standard way to
separate each SQL statement in
MySQL SQL database systems that allow more
than one SQL statement to be
executed in the same call to the
What is SQL? server.
In this tutorial, we will use
SQL is the standard language for
semicolon at the end of each SQL
dealing with Relational Databases.
statement.
SQL is used to insert, search,
update, and delete database Some of The Most
records. Important SQL
How to Use SQL Commands
SELECT - extracts data from a
The following SQL statement database
selects all the records in the UPDATE - updates data in a
"Customers" table: database
DELETE - deletes data from a
Example database
SELECT * FROM Customers; INSERT INTO - inserts new data
into a database
CREATE DATABASE - creates a
Keep in Mind That... new database
ALTER DATABASE - modifies a
SQL keywords are NOT case database
sensitive: select is the same as CREATE TABLE - creates a new
SELECT table
ALTER TABLE - modifies a table
In this tutorial we will write all SQL DROP TABLE - deletes a table
keywords in upper-case. CREATE INDEX - creates an index
(search key)
DROP INDEX - deletes an index
Semicolon after SQL
Statements? MySQL SELECT
Some database systems require a
semicolon at the end of each SQL
Statement
statement.
4
The MySQL SELECT Here, column1, column2, ... are the
field names of the table you want
Statement to select data from. If you want to
select all the fields available in the
The SELECT statement is used to table, use the following syntax:
select data from a database.
SELECT * FROM table_name;
The data returned is stored in a
result table, called the result-set. Demo Database
In this tutorial we will use the well-
SELECT Syntax known Northwind sample database.
SELECT column1, column2, ...
FROM table_name; Below is a selection from the
"Customers" table in the Northwind
sample database:
Custo
CustomerNam PostalCod
merI ContactName Address City Country
e e
D
Alfreds
1 Maria Anders Obere Str. 57 Berlin 12209 Germany
Futterkiste
Ana Trujillo
Avda. de la México
2 Emparedados y Ana Trujillo 05021 Mexico
Constitución 2222 D.F.
helados
Antonio Moreno México
3 Antonio Moreno Mataderos 2312 05023 Mexico
Taquería D.F.
4 Around the Horn Thomas Hardy 120 Hanover Sq. London WA1 1DP UK
Berglunds Christina
5 Berguvsvägen 8 Luleå S-958 22 Sweden
snabbköp Berglund
SELECT Columns The following SQL statement
selects ALL the columns from the
Example "Customers" table:
The following SQL statement Example
selects the "CustomerName", SELECT * FROM Customers;
"City", and "Country" columns from
the "Customers" table:
The MySQL SELECT
Example DISTINCT Statement
SELECT CustomerName, City, Country
FROM Customers; The SELECT DISTINCT statement is
used to return only distinct
SELECT * Example (different) values.
5
Inside a table, a column often The following SQL statement
contains many duplicate values; counts and returns the number of
and sometimes you only want to different (distinct) countries in the
list the different (distinct) values. "Customers" table:
SELECT DISTINCT Syntax Example
SELECT DISTINCT column1, SELECT COUNT(DISTINCT Country)
column2, ... FROM Customers;
FROM table_name;
SELECT Example MySQL WHERE
Without DISTINCT Clause
The following SQL statement The MySQL WHERE
selects all (including the
duplicates) values from the Clause
"Country" column in the
"Customers" table: The WHERE clause is used to filter
records.
Example
SELECT Country FROM Customers; It is used to extract only those
records that fulfill a specified
Now, let us use the SELECT DISTINCT condition.
statement and see the result.
WHERE Syntax
SELECT DISTINCT SELECT column1, column2, ...
FROM table_name
Examples WHERE condition;
The following SQL statement Note: The WHERE clause is not only
selects only the DISTINCT values used in SELECT statements, it is also
from the "Country" column in the used in UPDATE, DELETE, etc.!
"Customers" table:
Example
Demo Database
SELECT DISTINCT Country FROM
Below is a selection from the
Customers;
"Customers" table in the Northwind
sample database:
CustomerI CustomerNam ContactNam PostalCo Countr
Address City
D e e de y
Alfreds German
1 Maria Anders Obere Str. 57 Berlin 12209
Futterkiste y
2 Ana Trujillo Ana Trujillo Avda. de la México 05021 Mexico
6
Emparedados y Constitución
D.F.
helados 2222
Antonio Moreno Antonio Mataderos México
3 05023 Mexico
Taquería Moreno 2312 D.F.
Thomas 120 Hanover Londo
4 Around the Horn WA1 1DP UK
Hardy Sq. n
Berglunds Christina Berguvsväge
5 Luleå S-958 22 Sweden
snabbköp Berglund n8
WHERE Clause SQL requires single quotes around
text values (most database
Example systems will also allow double
quotes). However, numeric fields
The following SQL statement should not be enclosed in quotes:
selects all the customers from
"Mexico": Example
SELECT * FROM Customers
Example WHERE CustomerID = 1;
SELECT * FROM Customers
WHERE Country = 'Mexico';
Operators in The
Text Fields vs. WHERE Clause
Numeric Fields The following operators can be
used in the WHERE clause:
Operator Description Example
= Equal
> Greater than
< Less than
>= Greater than or equal
<= Less than or equal
Not equal. Note: In some versions of SQL this operator may be
<>
written as !=
BETWEEN Between a certain range
LIKE Search for a pattern
IN To specify multiple possible values for a column