SlideShare a Scribd company logo
1 of 52
BIT 2105
WEB DESIGN III
Lecture 3
MySQL®/PHP Database
Applications
How This Course Is Organized
Part I: Working with MySQL
 Before you code any PHP scripts you need to
know how to design a database, create tables in
your database, and get the information you want
from the database.
 Part I of this course shows you just about
everything you need to know to work with
MySQL.
22/10/2018
Part II: Working with PHP
 As an application’s developer, you will spend the
bulk of your time writing scripts that access the
database and present HTML to a user’s browser.
 This will cover the basics of the PHP scripting
language, how PHP works with variables,
conditions, and control structures, functions and
discusses techniques for writing clean,
manageable code.
32/10/2018
Part III: Applications
Part III: Simple Applications
 In this part we will present two applications. Here
you see the lessons from Parts I and II put into
practice as we build working applications.
Part IV: Not So Simple Applications
 Here the applications become more complex, as
we present applications commonly used on the
Web.
42/10/2018
Architecture of Web applications
52/10/2018
Relational Database
(MySQL, Oracle, MS SQL)
Internet
Middleware
PHP, ColdFusion ASP, JSP
Web Server
(Apache, IIS)
Web Browser
(Internet Explore Netscape)
The Web Server
 A web server is simply a computer program that
dispenses web pages as they are requested.
 The dominate Web servers out there:
 Apache
 Microsoft’s Internet Information Server (IIS)
 The Apache Web server is the most popular. It is
extremely quick and amazingly stable.
 Internet Information Server(IIS) is deeply tied to the
Windows environment and is a key component of
Microsoft’s Active Server Pages (ASP).
62/10/2018
Middleware
 PHP belongs to a class of languages known as
middleware.
 These languages work closely with the Web
server to interpret the requests made from the
World Wide Web, process these requests,
interact with other programs on the server to
fulfill the requests, and then indicate to the
Web server exactly what to serve to the client’s
browser.
72/10/2018
Relational Databases
 Relational database management systems
(RDBMSes) provide a great way to store and
access complex information.
 All the major databases make use of the Structured
Query Language (SQL).
 Some popular commercial RDBMSes are Oracle,
Sybase, Informix, Microsoft’s SQL Server, and
IBM’s DB2.
 In addition to MySQL, there are now two major
open-source relational databases. Postgres has
been the major alternative to MySQL
82/10/2018
Column Types
 MySQL provides you with a range of column types.
 Eight MySQL column types are suitable for storing text strings:
 char
 varchar
 tinytext/tinyblob
 text/blob
 mediumtext/mediumblob
 longtext/longblob
 enum
 set
112/10/2018
Numeric column types
 MySQL provides you with seven column types
suitable for storing numeric values.
 int/integer
 tinyint
 mediumint
 bigint
 float
 double/double precision/real
 decimal/numeric
122/10/2018
MySQL Statements
14
Road Map
 Introduction to MySQL
 Connecting and Disconnecting
 Entering Basic Queries
 Creating and Using a Database
15
Connecting to MySQL
 MySQL provides an interactive shell for
creating tables, inserting data, etc.
 On Windows, just go to c:mysqlbin,
and type:
 mysql
 Or, click on the Windows icon
16
Sample Session
 For example:
Enter password: *****
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 241 to server version: 3.23.49
Type 'help;' or 'h' for help. Type 'c' to clear the buffer.
mysql>
 To exit the MySQL Shell, just type QUIT or EXIT:
mysql> QUIT
mysql> exit
17
Basic Queries
 CREATE DATABASE databaseName;
 DROP DATABASE databaseName;
 SHOW DATABASES;
 USE databaseName;
 SHOW TABLES;
 CREATE TABLE tableName(name1 type1, name2
type2, ...);
 DROP TABLE tableName;
 DESCRIBE table;
 SELECT * FROM table;
 INSERT INTO TABLE VALUES( value1, value2, ...);
18
Using a Database
 To get started on your own database, first check
which databases currently exist.
 Use the SHOW statement to find out which
databases currently exist on the server:
mysql> show databases;
+----------+
| Database |
+----------+
| mysql |
| test |
+----------+
2 rows in set (0.01 sec)
19
Using a Database
 To create a new database, issue the
“create database” command:
 mysql> create database webdb;
 To the select a database, issue the
“use” command:
 mysql> use webdb;
20
Creating a Table
 Once you have selected a database,
you can view all database tables:
mysql> show tables;
Empty set (0.02 sec)
 An empty set indicates that I have not
created any tables yet.
21
Creating a Table
 Let’s create a table for storing pets.
 Table: pets
name: VARCHAR(20)
owner: VARCHAR(20)
species: VARCHAR(20)
sex: CHAR(1)
birth: DATE
date: DATE
VARCHAR is
usually used
to store string
data.
22
Creating a Table
 To create a table, use the CREATE TABLE
command:
mysql> CREATE TABLE pet (
-> name VARCHAR(20),
-> owner VARCHAR(20),
-> species VARCHAR(20),
-> sex CHAR(1),
-> birth DATE, death DATE);
Query OK, 0 rows affected (0.04 sec)
23
Showing Tables
 To verify that the table has been created:
mysql> show tables;
+------------------+
| Tables_in_test |
+------------------+
| pet |
+------------------+
1 row in set (0.01 sec)
24
Describing Tables
 To view a table structure, use the DESCRIBE
command:
mysql> describe pet;
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| name | varchar(20) | YES | | NULL | |
| owner | varchar(20) | YES | | NULL | |
| species | varchar(20) | YES | | NULL | |
| sex | char(1) | YES | | NULL | |
| birth | date | YES | | NULL | |
| death | date | YES | | NULL | |
+---------+-------------+------+-----+---------+-------+
6 rows in set (0.02 sec)
25
Deleting a Table
 To delete an entire table, use the DROP
TABLE command:
mysql> drop table pet;
Query OK, 0 rows affected (0.02 sec)
26
Loading Data
 Use the INSERT statement to enter data into
a table.
 For example:
INSERT INTO pet VALUES
('Fluffy','Harold','cat','f',
'1999-02-04',NULL);
 The next slide shows a full set of sample
data.
27
More data…
name owner species sex birth death
Fluffy Harold cat f 1993-02-04
Claws Gwen cat m 1994-03-17
Buffy Harold dog f 1989-05-13
Fang Benny dog m 1990-08-27
Bowser Diane dog m 1998-08-31 1995-07-29
Chirpy Gwen bird f 1998-09-11
Whistler Gwen bird 1997-12-09
Slim Benny snake m 1996-04-29
28
For each of the examples,
assume the following set of data.
name owner species sex birth death
Fluffy Harold cat f 1993-02-04
Claws Gwen cat m 1994-03-17
Buffy Harold dog f 1989-05-13
Fang Benny dog m 1990-08-27
Bowser Diane dog m 1998-08-31 1995-07-29
Chirpy Gwen bird f 1998-09-11
Whistler Gwen bird 1997-12-09
Slim Benny snake m 1996-04-29
29
SQL Select
 The SELECT statement is used to pull
information from a table.
 The general format is:
SELECT what_to_select
FROM which_table
WHERE conditions_to_satisfy
30
Selecting All Data
 The simplest form of SELECT retrieves everything
from a table
mysql> select * from pet;
+----------+--------+---------+------+------------+------------+
| name | owner | species | sex | birth | death |
+----------+--------+---------+------+------------+------------+
| Fluffy | Harold | cat | f | 1999-02-04 | NULL |
| Claws | Gwen | cat | f | 1994-03-17 | NULL |
| Buffy | Harold | dog | f | 1989-05-13 | NULL |
| Fang | Benny | dog | m | 1999-08-27 | NULL |
| Bowser | Diane | dog | m | 1998-08-31 | 1995-07-29 |
| Chirpy | Gwen | bird | f | 1998-09-11 | NULL |
| Whistler | Gwen | bird | | 1997-12-09 | NULL |
| Slim | Benny | snake | m | 1996-04-29 | NULL |
+----------+--------+---------+------+------------+------------+
8 rows in set (0.00 sec)
31
Selecting Particular Rows
 You can select only particular rows from your
table.
 For example, if you want to verify the change
that you made to Bowser's birth date, select
Bowser's record like this:
mysql> SELECT * FROM pet WHERE name = "Bowser";
+--------+-------+---------+------+------------+------------+
| name | owner | species | sex | birth | death |
+--------+-------+---------+------+------------+------------+
| Bowser | Diane | dog | m | 1998-08-31 | 1995-07-29 |
+--------+-------+---------+------+------------+------------+
1 row in set (0.00 sec)
32
Selecting Particular Rows
 To find all animals born after 1998
SELECT * FROM pet WHERE birth >= "1998-1-1";
 To find all female dogs, use a logical AND
SELECT * FROM pet WHERE species = "dog" AND sex = "f";
 To find all snakes or birds, use a logical OR
SELECT * FROM pet WHERE species = "snake"
OR species = "bird";
33
Selecting Particular Columns
 If you don’t want to see entire rows from
your table, just name the columns in
which you are interested, separated by
commas.
 For example, if you want to know when
your pets were born, select the name
and birth columns.
 (see example next slide.)
34
Selecting Particular Columns
mysql> select name, birth from pet;
+----------+------------+
| name | birth |
+----------+------------+
| Fluffy | 1999-02-04 |
| Claws | 1994-03-17 |
| Buffy | 1989-05-13 |
| Fang | 1999-08-27 |
| Bowser | 1998-08-31 |
| Chirpy | 1998-09-11 |
| Whistler | 1997-12-09 |
| Slim | 1996-04-29 |
+----------+------------+
8 rows in set (0.01 sec)
35
Sorting Data
 To sort a result, use an ORDER BY clause.
 For example, to view animal birthdays, sorted by
date:
mysql> SELECT name, birth FROM pet ORDER BY birth;
+----------+------------+
| name | birth |
+----------+------------+
| Buffy | 1989-05-13 |
| Claws | 1994-03-17 |
| Slim | 1996-04-29 |
| Whistler | 1997-12-09 |
| Bowser | 1998-08-31 |
| Chirpy | 1998-09-11 |
| Fluffy | 1999-02-04 |
| Fang | 1999-08-27 |
+----------+------------+
8 rows in set (0.02 sec)
36
Sorting Data
 To sort in reverse order, add the DESC
(descending keyword)
mysql> SELECT name, birth FROM pet ORDER BY birth DESC;
+----------+------------+
| name | birth |
+----------+------------+
| Fang | 1999-08-27 |
| Fluffy | 1999-02-04 |
| Chirpy | 1998-09-11 |
| Bowser | 1998-08-31 |
| Whistler | 1997-12-09 |
| Slim | 1996-04-29 |
| Claws | 1994-03-17 |
| Buffy | 1989-05-13 |
+----------+------------+
8 rows in set (0.02 sec)
37
Pattern Matching
 MySQL provides:
 standard SQL pattern matching; and
 regular expression pattern matching, similar to those used
by Unix utilities such as vi, grep and sed.
 SQL Pattern matching:
 To perform pattern matching, use the LIKE or NOT LIKE
comparison operators
 By default, patterns are case insensitive.
 Special Characters:
 _ Used to match any single character.
 % Used to match an arbitrary number of characters.
38
Pattern Matching Example
 To find names beginning with ‘b’:
mysql> SELECT * FROM pet WHERE name LIKE "b%";
+--------+--------+---------+------+------------+------------+
| name | owner | species | sex | birth | death |
+--------+--------+---------+------+------------+------------+
| Buffy | Harold | dog | f | 1989-05-13 | NULL |
| Bowser | Diane | dog | m | 1989-08-31 | 1995-07-29 |
+--------+--------+---------+------+------------+------------+
39
Pattern Matching Example
 To find names ending with `fy':
mysql> SELECT * FROM pet WHERE name LIKE "%fy";
+--------+--------+---------+------+------------+-------+
| name | owner | species | sex | birth | death |
+--------+--------+---------+------+------------+-------+
| Fluffy | Harold | cat | f | 1993-02-04 | NULL |
| Buffy | Harold | dog | f | 1989-05-13 | NULL |
+--------+--------+---------+------+------------+-------+
40
Pattern Matching Example
 To find names containing a ‘w’:
mysql> SELECT * FROM pet WHERE name LIKE "%w%";
+----------+-------+---------+------+------------+------------+
| name | owner | species | sex | birth | death |
+----------+-------+---------+------+------------+------------+
| Claws | Gwen | cat | m | 1994-03-17 | NULL |
| Bowser | Diane | dog | m | 1989-08-31 | 1995-07-29 |
| Whistler | Gwen | bird | NULL | 1997-12-09 | NULL |
+----------+-------+---------+------+------------+------------+
41
Pattern Matching Example
 To find names containing exactly five characters, use the _
pattern character:
mysql> SELECT * FROM pet WHERE name LIKE "_____";
+-------+--------+---------+------+------------+-------+
| name | owner | species | sex | birth | death |
+-------+--------+---------+------+------------+-------+
| Claws | Gwen | cat | m | 1994-03-17 | NULL |
| Buffy | Harold | dog | f | 1989-05-13 | NULL |
+-------+--------+---------+------+------------+-------+
42
Regular Expression Matching
 The other type of pattern matching
provided by MySQL uses extended
regular expressions.
 When you test for a match for this type
of pattern, use the REGEXP and NOT
REGEXP operators (or RLIKE and NOT
RLIKE, which are synonyms).
43
Regular Expressions
 Some characteristics of extended regular
expressions are:
 . matches any single character.
 A character class [...] matches any character within the
brackets. For example, [abc] matches a, b, or c. To name a
range of characters, use a dash. [a-z] matches any
lowercase letter, whereas [0-9] matches any digit.
 * matches zero or more instances of the thing preceding it.
For example, x* matches any number of x characters, [0-9]*
matches any number of digits, and .* matches any number of
anything.
 To anchor a pattern so that it must match the beginning or
end of the value being tested, use ^ at the beginning or $ at
the end of the pattern.
44
Reg Ex Example
 To find names beginning with b, use ^ to match the
beginning of the name:
mysql> SELECT * FROM pet WHERE name REGEXP "^b";
+--------+--------+---------+------+------------+------------+
| name | owner | species | sex | birth | death |
+--------+--------+---------+------+------------+------------+
| Buffy | Harold | dog | f | 1989-05-13 | NULL |
| Bowser | Diane | dog | m | 1989-08-31 | 1995-07-29 |
+--------+--------+---------+------+------------+------------+
45
Reg Ex Example
 To find names ending with `fy', use `$' to match the
end of the name:
mysql> SELECT * FROM pet WHERE name REGEXP "fy$";
+--------+--------+---------+------+------------+-------+
| name | owner | species | sex | birth | death |
+--------+--------+---------+------+------------+-------+
| Fluffy | Harold | cat | f | 1993-02-04 | NULL |
| Buffy | Harold | dog | f | 1989-05-13 | NULL |
+--------+--------+---------+------+------------+-------+
46
Counting Rows
 Databases are often used to answer the question,
"How often does a certain type of data occur in a
table?"
 For example, you might want to know how many pets
you have, or how many pets each owner has.
 Counting the total number of animals you have is the
same question as “How many rows are in the pet
table?” because there is one record per pet.
 The COUNT() function counts the number of non-
NULL results.
47
Counting Rows Example
 A query to determine total number of pets:
mysql> SELECT COUNT(*) FROM pet;
+----------+
| COUNT(*) |
+----------+
| 9 |
+----------+
48
Batch Mode
 In the previous sections, you used
mysql interactively to enter queries and
view the results.
 You can also run mysql in batch mode.
To do this, put the commands you want
to run in a file, then tell mysql to read its
input from the file:
 shell> mysql < batch-file
Using operators in Where clause
BETWEEN – Selects range of data between two
values
SELECT column_names(s) FROM table_name
WHERE column_name BETWEEN value1 AND Value
2
◦ SELECT * from patientd WHERE nationality
BETWEEN ‘Kenyan’ AND ‘Ugandan’
NOT BETWEEN
49
Update command
 Used to update existing records in a table
 UPDATE table_name SET column1 = “new
value”, Column2 = “new value” Where
some_column = some_Value;
 UPDATE patientd SET patientname=‘Nanyonjo’
WHERE pno=2 AND patientname = ‘Mukasa’;
50
Delete command
The DELETE statement deletes records in a table without
deleting the table
• DELETE FROM table_name WHERE some_column =
some_value;
• DELETE FROM patientd WHERE patientname = ‘Mukasa’;
To delete all rows, use:
• DELETE * FROM table_name OR DELETE FROM
table_name
• DELETE FROM patientd;
51
TRUNCATE command
•Truncate command deletes the data
inside the table but not the table itself.
•TRUNCATE TABLE table_name;
▫ E.g. TRUNCATE TABLE patientd;
52
53
Is that all there is to MySQL?
 Of course not!
 Understanding databases and MySQL
could take us several weeks (perhaps
months!)
 For now, focus on:
 using the MySQL shell
 creating tables
 creating basic SQL queries
54
Summary
 SQL provides a structured language for
querying/updating multiple databases.
 The more you know SQL, the better.
 The most important part of SQL is learning to
retrieve data.
 selecting rows, columns, boolean operators,
pattern matching, etc.
 Keep playing around in the MySQL Shell.

More Related Content

What's hot

Sql injection
Sql injectionSql injection
Sql injectionNa Ni
 
Introduction To MySQL Lecture 1
Introduction To MySQL Lecture 1Introduction To MySQL Lecture 1
Introduction To MySQL Lecture 1Ajay Khatri
 
0888 learning-mysql
0888 learning-mysql0888 learning-mysql
0888 learning-mysqlsabir18
 
Efficient Pagination Using MySQL
Efficient Pagination Using MySQLEfficient Pagination Using MySQL
Efficient Pagination Using MySQLEvan Weaver
 
MySQL Built-In Functions
MySQL Built-In FunctionsMySQL Built-In Functions
MySQL Built-In FunctionsSHC
 
15 protips for mysql users pfz
15 protips for mysql users   pfz15 protips for mysql users   pfz
15 protips for mysql users pfzJoshua Thijssen
 
Advanced MySQL Query and Schema Tuning
Advanced MySQL Query and Schema TuningAdvanced MySQL Query and Schema Tuning
Advanced MySQL Query and Schema TuningMYXPLAIN
 
Introduction to R
Introduction to RIntroduction to R
Introduction to RStacy Irwin
 
Python data structures
Python data structuresPython data structures
Python data structuresHarry Potter
 

What's hot (13)

MySQL constraints
MySQL constraintsMySQL constraints
MySQL constraints
 
Sql injection
Sql injectionSql injection
Sql injection
 
My sql.ppt
My sql.pptMy sql.ppt
My sql.ppt
 
Introduction To MySQL Lecture 1
Introduction To MySQL Lecture 1Introduction To MySQL Lecture 1
Introduction To MySQL Lecture 1
 
0888 learning-mysql
0888 learning-mysql0888 learning-mysql
0888 learning-mysql
 
dbms lab manual
dbms lab manualdbms lab manual
dbms lab manual
 
Intro to my sql
Intro to my sqlIntro to my sql
Intro to my sql
 
Efficient Pagination Using MySQL
Efficient Pagination Using MySQLEfficient Pagination Using MySQL
Efficient Pagination Using MySQL
 
MySQL Built-In Functions
MySQL Built-In FunctionsMySQL Built-In Functions
MySQL Built-In Functions
 
15 protips for mysql users pfz
15 protips for mysql users   pfz15 protips for mysql users   pfz
15 protips for mysql users pfz
 
Advanced MySQL Query and Schema Tuning
Advanced MySQL Query and Schema TuningAdvanced MySQL Query and Schema Tuning
Advanced MySQL Query and Schema Tuning
 
Introduction to R
Introduction to RIntroduction to R
Introduction to R
 
Python data structures
Python data structuresPython data structures
Python data structures
 

Similar to BIT 2105 MySQL/PHP Database Applications Lecture 3

mysql-Tutorial with Query presentation.ppt
mysql-Tutorial with Query presentation.pptmysql-Tutorial with Query presentation.ppt
mysql-Tutorial with Query presentation.pptaptechaligarh
 
4. Data Manipulation.ppt
4. Data Manipulation.ppt4. Data Manipulation.ppt
4. Data Manipulation.pptKISHOYIANKISH
 
MySQL Kitchen : spice up your everyday SQL queries
MySQL Kitchen : spice up your everyday SQL queriesMySQL Kitchen : spice up your everyday SQL queries
MySQL Kitchen : spice up your everyday SQL queriesDamien Seguy
 
Introduction into MySQL Query Tuning
Introduction into MySQL Query TuningIntroduction into MySQL Query Tuning
Introduction into MySQL Query TuningSveta Smirnova
 
Introduction databases and MYSQL
Introduction databases and MYSQLIntroduction databases and MYSQL
Introduction databases and MYSQLNaeem Junejo
 
PHP mysql Introduction database
 PHP mysql  Introduction database PHP mysql  Introduction database
PHP mysql Introduction databaseMudasir Syed
 
15 MySQL Basics #burningkeyboards
15 MySQL Basics #burningkeyboards15 MySQL Basics #burningkeyboards
15 MySQL Basics #burningkeyboardsDenis Ristic
 
L203326 intro-maria db-techu2020-v9
L203326 intro-maria db-techu2020-v9L203326 intro-maria db-techu2020-v9
L203326 intro-maria db-techu2020-v9Tony Pearson
 
DATA BASE || INTRODUCTION OF DATABASE \\ SQL 2018
DATA BASE || INTRODUCTION OF DATABASE \\ SQL 2018DATA BASE || INTRODUCTION OF DATABASE \\ SQL 2018
DATA BASE || INTRODUCTION OF DATABASE \\ SQL 2018teachersduniya.com
 
MySQL Database System Hiep Dinh
MySQL Database System Hiep DinhMySQL Database System Hiep Dinh
MySQL Database System Hiep Dinhwebhostingguy
 
Applied Partitioning And Scaling Your Database System Presentation
Applied Partitioning And Scaling Your Database System PresentationApplied Partitioning And Scaling Your Database System Presentation
Applied Partitioning And Scaling Your Database System PresentationRichard Crowley
 
Need for Speed: MySQL Indexing
Need for Speed: MySQL IndexingNeed for Speed: MySQL Indexing
Need for Speed: MySQL IndexingMYXPLAIN
 
MySQL 8.0 New Features -- September 27th presentation for Open Source Summit
MySQL 8.0 New Features -- September 27th presentation for Open Source SummitMySQL 8.0 New Features -- September 27th presentation for Open Source Summit
MySQL 8.0 New Features -- September 27th presentation for Open Source SummitDave Stokes
 
mysqlanditsbasiccommands-150226033905-conversion-gate02.pdf
mysqlanditsbasiccommands-150226033905-conversion-gate02.pdfmysqlanditsbasiccommands-150226033905-conversion-gate02.pdf
mysqlanditsbasiccommands-150226033905-conversion-gate02.pdfpradnyamulay
 
MySQL Baics - Texas Linxufest beginners tutorial May 31st, 2019
MySQL Baics - Texas Linxufest beginners tutorial May 31st, 2019MySQL Baics - Texas Linxufest beginners tutorial May 31st, 2019
MySQL Baics - Texas Linxufest beginners tutorial May 31st, 2019Dave Stokes
 

Similar to BIT 2105 MySQL/PHP Database Applications Lecture 3 (20)

Intro to my sql
Intro to my sqlIntro to my sql
Intro to my sql
 
mysql-Tutorial with Query presentation.ppt
mysql-Tutorial with Query presentation.pptmysql-Tutorial with Query presentation.ppt
mysql-Tutorial with Query presentation.ppt
 
4. Data Manipulation.ppt
4. Data Manipulation.ppt4. Data Manipulation.ppt
4. Data Manipulation.ppt
 
MySQL Kitchen : spice up your everyday SQL queries
MySQL Kitchen : spice up your everyday SQL queriesMySQL Kitchen : spice up your everyday SQL queries
MySQL Kitchen : spice up your everyday SQL queries
 
Introduction into MySQL Query Tuning
Introduction into MySQL Query TuningIntroduction into MySQL Query Tuning
Introduction into MySQL Query Tuning
 
Introduction databases and MYSQL
Introduction databases and MYSQLIntroduction databases and MYSQL
Introduction databases and MYSQL
 
PHP mysql Introduction database
 PHP mysql  Introduction database PHP mysql  Introduction database
PHP mysql Introduction database
 
Mysql basics1
Mysql basics1Mysql basics1
Mysql basics1
 
MySQLinsanity
MySQLinsanityMySQLinsanity
MySQLinsanity
 
15 MySQL Basics #burningkeyboards
15 MySQL Basics #burningkeyboards15 MySQL Basics #burningkeyboards
15 MySQL Basics #burningkeyboards
 
L203326 intro-maria db-techu2020-v9
L203326 intro-maria db-techu2020-v9L203326 intro-maria db-techu2020-v9
L203326 intro-maria db-techu2020-v9
 
DATA BASE || INTRODUCTION OF DATABASE \\ SQL 2018
DATA BASE || INTRODUCTION OF DATABASE \\ SQL 2018DATA BASE || INTRODUCTION OF DATABASE \\ SQL 2018
DATA BASE || INTRODUCTION OF DATABASE \\ SQL 2018
 
MySql slides (ppt)
MySql slides (ppt)MySql slides (ppt)
MySql slides (ppt)
 
MySQL Database System Hiep Dinh
MySQL Database System Hiep DinhMySQL Database System Hiep Dinh
MySQL Database System Hiep Dinh
 
Applied Partitioning And Scaling Your Database System Presentation
Applied Partitioning And Scaling Your Database System PresentationApplied Partitioning And Scaling Your Database System Presentation
Applied Partitioning And Scaling Your Database System Presentation
 
Need for Speed: MySQL Indexing
Need for Speed: MySQL IndexingNeed for Speed: MySQL Indexing
Need for Speed: MySQL Indexing
 
MySQL 8.0 New Features -- September 27th presentation for Open Source Summit
MySQL 8.0 New Features -- September 27th presentation for Open Source SummitMySQL 8.0 New Features -- September 27th presentation for Open Source Summit
MySQL 8.0 New Features -- September 27th presentation for Open Source Summit
 
MySQL and its basic commands
MySQL and its basic commandsMySQL and its basic commands
MySQL and its basic commands
 
mysqlanditsbasiccommands-150226033905-conversion-gate02.pdf
mysqlanditsbasiccommands-150226033905-conversion-gate02.pdfmysqlanditsbasiccommands-150226033905-conversion-gate02.pdf
mysqlanditsbasiccommands-150226033905-conversion-gate02.pdf
 
MySQL Baics - Texas Linxufest beginners tutorial May 31st, 2019
MySQL Baics - Texas Linxufest beginners tutorial May 31st, 2019MySQL Baics - Texas Linxufest beginners tutorial May 31st, 2019
MySQL Baics - Texas Linxufest beginners tutorial May 31st, 2019
 

More from okelloerick

Lecture8 php page control by okello erick
Lecture8 php page control by okello erickLecture8 php page control by okello erick
Lecture8 php page control by okello erickokelloerick
 
Lecture7 form processing by okello erick
Lecture7 form processing by okello erickLecture7 form processing by okello erick
Lecture7 form processing by okello erickokelloerick
 
Lecture6 display data by okello erick
Lecture6 display data by okello erickLecture6 display data by okello erick
Lecture6 display data by okello erickokelloerick
 
Lecture4 php by okello erick
Lecture4 php by okello erickLecture4 php by okello erick
Lecture4 php by okello erickokelloerick
 
Lecture3 php by okello erick
Lecture3 php by okello erickLecture3 php by okello erick
Lecture3 php by okello erickokelloerick
 
Lecture1 introduction by okello erick
Lecture1 introduction by okello erickLecture1 introduction by okello erick
Lecture1 introduction by okello erickokelloerick
 
Data commn intro by okello erick
Data commn intro by okello erickData commn intro by okello erick
Data commn intro by okello erickokelloerick
 
Computer networks--networking hardware
Computer networks--networking hardwareComputer networks--networking hardware
Computer networks--networking hardwareokelloerick
 

More from okelloerick (8)

Lecture8 php page control by okello erick
Lecture8 php page control by okello erickLecture8 php page control by okello erick
Lecture8 php page control by okello erick
 
Lecture7 form processing by okello erick
Lecture7 form processing by okello erickLecture7 form processing by okello erick
Lecture7 form processing by okello erick
 
Lecture6 display data by okello erick
Lecture6 display data by okello erickLecture6 display data by okello erick
Lecture6 display data by okello erick
 
Lecture4 php by okello erick
Lecture4 php by okello erickLecture4 php by okello erick
Lecture4 php by okello erick
 
Lecture3 php by okello erick
Lecture3 php by okello erickLecture3 php by okello erick
Lecture3 php by okello erick
 
Lecture1 introduction by okello erick
Lecture1 introduction by okello erickLecture1 introduction by okello erick
Lecture1 introduction by okello erick
 
Data commn intro by okello erick
Data commn intro by okello erickData commn intro by okello erick
Data commn intro by okello erick
 
Computer networks--networking hardware
Computer networks--networking hardwareComputer networks--networking hardware
Computer networks--networking hardware
 

Recently uploaded

Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024BookNet Canada
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions
 
costume and set research powerpoint presentation
costume and set research powerpoint presentationcostume and set research powerpoint presentation
costume and set research powerpoint presentationphoebematthew05
 
APIForce Zurich 5 April Automation LPDG
APIForce Zurich 5 April  Automation LPDGAPIForce Zurich 5 April  Automation LPDG
APIForce Zurich 5 April Automation LPDGMarianaLemus7
 
Unlocking the Potential of the Cloud for IBM Power Systems
Unlocking the Potential of the Cloud for IBM Power SystemsUnlocking the Potential of the Cloud for IBM Power Systems
Unlocking the Potential of the Cloud for IBM Power SystemsPrecisely
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxnull - The Open Security Community
 
Bluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdfBluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdfngoud9212
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 

Recently uploaded (20)

Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping Elbows
 
costume and set research powerpoint presentation
costume and set research powerpoint presentationcostume and set research powerpoint presentation
costume and set research powerpoint presentation
 
APIForce Zurich 5 April Automation LPDG
APIForce Zurich 5 April  Automation LPDGAPIForce Zurich 5 April  Automation LPDG
APIForce Zurich 5 April Automation LPDG
 
Unlocking the Potential of the Cloud for IBM Power Systems
Unlocking the Potential of the Cloud for IBM Power SystemsUnlocking the Potential of the Cloud for IBM Power Systems
Unlocking the Potential of the Cloud for IBM Power Systems
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
 
Bluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdfBluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdf
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 

BIT 2105 MySQL/PHP Database Applications Lecture 3

  • 1. BIT 2105 WEB DESIGN III Lecture 3 MySQL®/PHP Database Applications
  • 2. How This Course Is Organized Part I: Working with MySQL  Before you code any PHP scripts you need to know how to design a database, create tables in your database, and get the information you want from the database.  Part I of this course shows you just about everything you need to know to work with MySQL. 22/10/2018
  • 3. Part II: Working with PHP  As an application’s developer, you will spend the bulk of your time writing scripts that access the database and present HTML to a user’s browser.  This will cover the basics of the PHP scripting language, how PHP works with variables, conditions, and control structures, functions and discusses techniques for writing clean, manageable code. 32/10/2018
  • 4. Part III: Applications Part III: Simple Applications  In this part we will present two applications. Here you see the lessons from Parts I and II put into practice as we build working applications. Part IV: Not So Simple Applications  Here the applications become more complex, as we present applications commonly used on the Web. 42/10/2018
  • 5. Architecture of Web applications 52/10/2018 Relational Database (MySQL, Oracle, MS SQL) Internet Middleware PHP, ColdFusion ASP, JSP Web Server (Apache, IIS) Web Browser (Internet Explore Netscape)
  • 6. The Web Server  A web server is simply a computer program that dispenses web pages as they are requested.  The dominate Web servers out there:  Apache  Microsoft’s Internet Information Server (IIS)  The Apache Web server is the most popular. It is extremely quick and amazingly stable.  Internet Information Server(IIS) is deeply tied to the Windows environment and is a key component of Microsoft’s Active Server Pages (ASP). 62/10/2018
  • 7. Middleware  PHP belongs to a class of languages known as middleware.  These languages work closely with the Web server to interpret the requests made from the World Wide Web, process these requests, interact with other programs on the server to fulfill the requests, and then indicate to the Web server exactly what to serve to the client’s browser. 72/10/2018
  • 8. Relational Databases  Relational database management systems (RDBMSes) provide a great way to store and access complex information.  All the major databases make use of the Structured Query Language (SQL).  Some popular commercial RDBMSes are Oracle, Sybase, Informix, Microsoft’s SQL Server, and IBM’s DB2.  In addition to MySQL, there are now two major open-source relational databases. Postgres has been the major alternative to MySQL 82/10/2018
  • 9. Column Types  MySQL provides you with a range of column types.  Eight MySQL column types are suitable for storing text strings:  char  varchar  tinytext/tinyblob  text/blob  mediumtext/mediumblob  longtext/longblob  enum  set 112/10/2018
  • 10. Numeric column types  MySQL provides you with seven column types suitable for storing numeric values.  int/integer  tinyint  mediumint  bigint  float  double/double precision/real  decimal/numeric 122/10/2018
  • 12. 14 Road Map  Introduction to MySQL  Connecting and Disconnecting  Entering Basic Queries  Creating and Using a Database
  • 13. 15 Connecting to MySQL  MySQL provides an interactive shell for creating tables, inserting data, etc.  On Windows, just go to c:mysqlbin, and type:  mysql  Or, click on the Windows icon
  • 14. 16 Sample Session  For example: Enter password: ***** Welcome to the MySQL monitor. Commands end with ; or g. Your MySQL connection id is 241 to server version: 3.23.49 Type 'help;' or 'h' for help. Type 'c' to clear the buffer. mysql>  To exit the MySQL Shell, just type QUIT or EXIT: mysql> QUIT mysql> exit
  • 15. 17 Basic Queries  CREATE DATABASE databaseName;  DROP DATABASE databaseName;  SHOW DATABASES;  USE databaseName;  SHOW TABLES;  CREATE TABLE tableName(name1 type1, name2 type2, ...);  DROP TABLE tableName;  DESCRIBE table;  SELECT * FROM table;  INSERT INTO TABLE VALUES( value1, value2, ...);
  • 16. 18 Using a Database  To get started on your own database, first check which databases currently exist.  Use the SHOW statement to find out which databases currently exist on the server: mysql> show databases; +----------+ | Database | +----------+ | mysql | | test | +----------+ 2 rows in set (0.01 sec)
  • 17. 19 Using a Database  To create a new database, issue the “create database” command:  mysql> create database webdb;  To the select a database, issue the “use” command:  mysql> use webdb;
  • 18. 20 Creating a Table  Once you have selected a database, you can view all database tables: mysql> show tables; Empty set (0.02 sec)  An empty set indicates that I have not created any tables yet.
  • 19. 21 Creating a Table  Let’s create a table for storing pets.  Table: pets name: VARCHAR(20) owner: VARCHAR(20) species: VARCHAR(20) sex: CHAR(1) birth: DATE date: DATE VARCHAR is usually used to store string data.
  • 20. 22 Creating a Table  To create a table, use the CREATE TABLE command: mysql> CREATE TABLE pet ( -> name VARCHAR(20), -> owner VARCHAR(20), -> species VARCHAR(20), -> sex CHAR(1), -> birth DATE, death DATE); Query OK, 0 rows affected (0.04 sec)
  • 21. 23 Showing Tables  To verify that the table has been created: mysql> show tables; +------------------+ | Tables_in_test | +------------------+ | pet | +------------------+ 1 row in set (0.01 sec)
  • 22. 24 Describing Tables  To view a table structure, use the DESCRIBE command: mysql> describe pet; +---------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +---------+-------------+------+-----+---------+-------+ | name | varchar(20) | YES | | NULL | | | owner | varchar(20) | YES | | NULL | | | species | varchar(20) | YES | | NULL | | | sex | char(1) | YES | | NULL | | | birth | date | YES | | NULL | | | death | date | YES | | NULL | | +---------+-------------+------+-----+---------+-------+ 6 rows in set (0.02 sec)
  • 23. 25 Deleting a Table  To delete an entire table, use the DROP TABLE command: mysql> drop table pet; Query OK, 0 rows affected (0.02 sec)
  • 24. 26 Loading Data  Use the INSERT statement to enter data into a table.  For example: INSERT INTO pet VALUES ('Fluffy','Harold','cat','f', '1999-02-04',NULL);  The next slide shows a full set of sample data.
  • 25. 27 More data… name owner species sex birth death Fluffy Harold cat f 1993-02-04 Claws Gwen cat m 1994-03-17 Buffy Harold dog f 1989-05-13 Fang Benny dog m 1990-08-27 Bowser Diane dog m 1998-08-31 1995-07-29 Chirpy Gwen bird f 1998-09-11 Whistler Gwen bird 1997-12-09 Slim Benny snake m 1996-04-29
  • 26. 28 For each of the examples, assume the following set of data. name owner species sex birth death Fluffy Harold cat f 1993-02-04 Claws Gwen cat m 1994-03-17 Buffy Harold dog f 1989-05-13 Fang Benny dog m 1990-08-27 Bowser Diane dog m 1998-08-31 1995-07-29 Chirpy Gwen bird f 1998-09-11 Whistler Gwen bird 1997-12-09 Slim Benny snake m 1996-04-29
  • 27. 29 SQL Select  The SELECT statement is used to pull information from a table.  The general format is: SELECT what_to_select FROM which_table WHERE conditions_to_satisfy
  • 28. 30 Selecting All Data  The simplest form of SELECT retrieves everything from a table mysql> select * from pet; +----------+--------+---------+------+------------+------------+ | name | owner | species | sex | birth | death | +----------+--------+---------+------+------------+------------+ | Fluffy | Harold | cat | f | 1999-02-04 | NULL | | Claws | Gwen | cat | f | 1994-03-17 | NULL | | Buffy | Harold | dog | f | 1989-05-13 | NULL | | Fang | Benny | dog | m | 1999-08-27 | NULL | | Bowser | Diane | dog | m | 1998-08-31 | 1995-07-29 | | Chirpy | Gwen | bird | f | 1998-09-11 | NULL | | Whistler | Gwen | bird | | 1997-12-09 | NULL | | Slim | Benny | snake | m | 1996-04-29 | NULL | +----------+--------+---------+------+------------+------------+ 8 rows in set (0.00 sec)
  • 29. 31 Selecting Particular Rows  You can select only particular rows from your table.  For example, if you want to verify the change that you made to Bowser's birth date, select Bowser's record like this: mysql> SELECT * FROM pet WHERE name = "Bowser"; +--------+-------+---------+------+------------+------------+ | name | owner | species | sex | birth | death | +--------+-------+---------+------+------------+------------+ | Bowser | Diane | dog | m | 1998-08-31 | 1995-07-29 | +--------+-------+---------+------+------------+------------+ 1 row in set (0.00 sec)
  • 30. 32 Selecting Particular Rows  To find all animals born after 1998 SELECT * FROM pet WHERE birth >= "1998-1-1";  To find all female dogs, use a logical AND SELECT * FROM pet WHERE species = "dog" AND sex = "f";  To find all snakes or birds, use a logical OR SELECT * FROM pet WHERE species = "snake" OR species = "bird";
  • 31. 33 Selecting Particular Columns  If you don’t want to see entire rows from your table, just name the columns in which you are interested, separated by commas.  For example, if you want to know when your pets were born, select the name and birth columns.  (see example next slide.)
  • 32. 34 Selecting Particular Columns mysql> select name, birth from pet; +----------+------------+ | name | birth | +----------+------------+ | Fluffy | 1999-02-04 | | Claws | 1994-03-17 | | Buffy | 1989-05-13 | | Fang | 1999-08-27 | | Bowser | 1998-08-31 | | Chirpy | 1998-09-11 | | Whistler | 1997-12-09 | | Slim | 1996-04-29 | +----------+------------+ 8 rows in set (0.01 sec)
  • 33. 35 Sorting Data  To sort a result, use an ORDER BY clause.  For example, to view animal birthdays, sorted by date: mysql> SELECT name, birth FROM pet ORDER BY birth; +----------+------------+ | name | birth | +----------+------------+ | Buffy | 1989-05-13 | | Claws | 1994-03-17 | | Slim | 1996-04-29 | | Whistler | 1997-12-09 | | Bowser | 1998-08-31 | | Chirpy | 1998-09-11 | | Fluffy | 1999-02-04 | | Fang | 1999-08-27 | +----------+------------+ 8 rows in set (0.02 sec)
  • 34. 36 Sorting Data  To sort in reverse order, add the DESC (descending keyword) mysql> SELECT name, birth FROM pet ORDER BY birth DESC; +----------+------------+ | name | birth | +----------+------------+ | Fang | 1999-08-27 | | Fluffy | 1999-02-04 | | Chirpy | 1998-09-11 | | Bowser | 1998-08-31 | | Whistler | 1997-12-09 | | Slim | 1996-04-29 | | Claws | 1994-03-17 | | Buffy | 1989-05-13 | +----------+------------+ 8 rows in set (0.02 sec)
  • 35. 37 Pattern Matching  MySQL provides:  standard SQL pattern matching; and  regular expression pattern matching, similar to those used by Unix utilities such as vi, grep and sed.  SQL Pattern matching:  To perform pattern matching, use the LIKE or NOT LIKE comparison operators  By default, patterns are case insensitive.  Special Characters:  _ Used to match any single character.  % Used to match an arbitrary number of characters.
  • 36. 38 Pattern Matching Example  To find names beginning with ‘b’: mysql> SELECT * FROM pet WHERE name LIKE "b%"; +--------+--------+---------+------+------------+------------+ | name | owner | species | sex | birth | death | +--------+--------+---------+------+------------+------------+ | Buffy | Harold | dog | f | 1989-05-13 | NULL | | Bowser | Diane | dog | m | 1989-08-31 | 1995-07-29 | +--------+--------+---------+------+------------+------------+
  • 37. 39 Pattern Matching Example  To find names ending with `fy': mysql> SELECT * FROM pet WHERE name LIKE "%fy"; +--------+--------+---------+------+------------+-------+ | name | owner | species | sex | birth | death | +--------+--------+---------+------+------------+-------+ | Fluffy | Harold | cat | f | 1993-02-04 | NULL | | Buffy | Harold | dog | f | 1989-05-13 | NULL | +--------+--------+---------+------+------------+-------+
  • 38. 40 Pattern Matching Example  To find names containing a ‘w’: mysql> SELECT * FROM pet WHERE name LIKE "%w%"; +----------+-------+---------+------+------------+------------+ | name | owner | species | sex | birth | death | +----------+-------+---------+------+------------+------------+ | Claws | Gwen | cat | m | 1994-03-17 | NULL | | Bowser | Diane | dog | m | 1989-08-31 | 1995-07-29 | | Whistler | Gwen | bird | NULL | 1997-12-09 | NULL | +----------+-------+---------+------+------------+------------+
  • 39. 41 Pattern Matching Example  To find names containing exactly five characters, use the _ pattern character: mysql> SELECT * FROM pet WHERE name LIKE "_____"; +-------+--------+---------+------+------------+-------+ | name | owner | species | sex | birth | death | +-------+--------+---------+------+------------+-------+ | Claws | Gwen | cat | m | 1994-03-17 | NULL | | Buffy | Harold | dog | f | 1989-05-13 | NULL | +-------+--------+---------+------+------------+-------+
  • 40. 42 Regular Expression Matching  The other type of pattern matching provided by MySQL uses extended regular expressions.  When you test for a match for this type of pattern, use the REGEXP and NOT REGEXP operators (or RLIKE and NOT RLIKE, which are synonyms).
  • 41. 43 Regular Expressions  Some characteristics of extended regular expressions are:  . matches any single character.  A character class [...] matches any character within the brackets. For example, [abc] matches a, b, or c. To name a range of characters, use a dash. [a-z] matches any lowercase letter, whereas [0-9] matches any digit.  * matches zero or more instances of the thing preceding it. For example, x* matches any number of x characters, [0-9]* matches any number of digits, and .* matches any number of anything.  To anchor a pattern so that it must match the beginning or end of the value being tested, use ^ at the beginning or $ at the end of the pattern.
  • 42. 44 Reg Ex Example  To find names beginning with b, use ^ to match the beginning of the name: mysql> SELECT * FROM pet WHERE name REGEXP "^b"; +--------+--------+---------+------+------------+------------+ | name | owner | species | sex | birth | death | +--------+--------+---------+------+------------+------------+ | Buffy | Harold | dog | f | 1989-05-13 | NULL | | Bowser | Diane | dog | m | 1989-08-31 | 1995-07-29 | +--------+--------+---------+------+------------+------------+
  • 43. 45 Reg Ex Example  To find names ending with `fy', use `$' to match the end of the name: mysql> SELECT * FROM pet WHERE name REGEXP "fy$"; +--------+--------+---------+------+------------+-------+ | name | owner | species | sex | birth | death | +--------+--------+---------+------+------------+-------+ | Fluffy | Harold | cat | f | 1993-02-04 | NULL | | Buffy | Harold | dog | f | 1989-05-13 | NULL | +--------+--------+---------+------+------------+-------+
  • 44. 46 Counting Rows  Databases are often used to answer the question, "How often does a certain type of data occur in a table?"  For example, you might want to know how many pets you have, or how many pets each owner has.  Counting the total number of animals you have is the same question as “How many rows are in the pet table?” because there is one record per pet.  The COUNT() function counts the number of non- NULL results.
  • 45. 47 Counting Rows Example  A query to determine total number of pets: mysql> SELECT COUNT(*) FROM pet; +----------+ | COUNT(*) | +----------+ | 9 | +----------+
  • 46. 48 Batch Mode  In the previous sections, you used mysql interactively to enter queries and view the results.  You can also run mysql in batch mode. To do this, put the commands you want to run in a file, then tell mysql to read its input from the file:  shell> mysql < batch-file
  • 47. Using operators in Where clause BETWEEN – Selects range of data between two values SELECT column_names(s) FROM table_name WHERE column_name BETWEEN value1 AND Value 2 ◦ SELECT * from patientd WHERE nationality BETWEEN ‘Kenyan’ AND ‘Ugandan’ NOT BETWEEN 49
  • 48. Update command  Used to update existing records in a table  UPDATE table_name SET column1 = “new value”, Column2 = “new value” Where some_column = some_Value;  UPDATE patientd SET patientname=‘Nanyonjo’ WHERE pno=2 AND patientname = ‘Mukasa’; 50
  • 49. Delete command The DELETE statement deletes records in a table without deleting the table • DELETE FROM table_name WHERE some_column = some_value; • DELETE FROM patientd WHERE patientname = ‘Mukasa’; To delete all rows, use: • DELETE * FROM table_name OR DELETE FROM table_name • DELETE FROM patientd; 51
  • 50. TRUNCATE command •Truncate command deletes the data inside the table but not the table itself. •TRUNCATE TABLE table_name; ▫ E.g. TRUNCATE TABLE patientd; 52
  • 51. 53 Is that all there is to MySQL?  Of course not!  Understanding databases and MySQL could take us several weeks (perhaps months!)  For now, focus on:  using the MySQL shell  creating tables  creating basic SQL queries
  • 52. 54 Summary  SQL provides a structured language for querying/updating multiple databases.  The more you know SQL, the better.  The most important part of SQL is learning to retrieve data.  selecting rows, columns, boolean operators, pattern matching, etc.  Keep playing around in the MySQL Shell.