SlideShare a Scribd company logo
IT3101
Web-based Database
Development
Lecture 2
MySQL®/PHP Database
Applications
Architecture of Web applications
22/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).
32/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
fulfil the requests, and then indicate to the Web
server exactly what to serve to the client’s
browser.
42/10/2018
Relational Databases
 Relational database management systems
(RDBMS) 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
52/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
82/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
92/10/2018
MySQL Statements
11
Road Map
 Introduction to MySQL
 Connecting and Disconnecting
 Entering Basic Queries
 Creating and Using a Database
12
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
13
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
14
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, ...);
15
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)
16
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;
17
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.
18
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.
19
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)
20
Showing Tables
 To verify that the table has been created:
mysql> show tables;
+------------------+
| Tables_in_test |
+------------------+
| pet |
+------------------+
1 row in set (0.01 sec)
21
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)
22
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)
23
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.
24
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
25
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
26
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
27
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)
28
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)
29
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";
30
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.)
31
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)
32
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)
33
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)
34
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.
35
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 |
+--------+--------+---------+------+------------+------------+
36
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 |
+--------+--------+---------+------+------------+-------+
37
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 |
+----------+-------+---------+------+------------+------------+
38
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 |
+-------+--------+---------+------+------------+-------+
39
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).
40
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.
41
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 |
+--------+--------+---------+------+------------+------------+
42
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 |
+--------+--------+---------+------+------------+-------+
43
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.
44
Counting Rows Example
 A query to determine total number of pets:
mysql> SELECT COUNT(*) FROM pet;
+----------+
| COUNT(*) |
+----------+
| 9 |
+----------+
45
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
46
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 pet SET owner="lawrence", sex="F"
WHERE name="Slim";
47
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 pet WHERE name=“ ";
48
TRUNCATE command
•Truncate command deletes the data
inside the table but not the table itself.
•TRUNCATE TABLE table_name;
▫ E.g. TRUNCATE TABLE patientd;
49
50
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
51
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

Massively Distributed Backups at Facebook Scale - Shlomo Priymak, Facebook - ...
Massively Distributed Backups at Facebook Scale - Shlomo Priymak, Facebook - ...Massively Distributed Backups at Facebook Scale - Shlomo Priymak, Facebook - ...
Massively Distributed Backups at Facebook Scale - Shlomo Priymak, Facebook - ...DevOpsDays Tel Aviv
 
Introduction To MySQL Lecture 1
Introduction To MySQL Lecture 1Introduction To MySQL Lecture 1
Introduction To MySQL Lecture 1Ajay Khatri
 
MySQL's JSON Data Type and Document Store
MySQL's JSON Data Type and Document StoreMySQL's JSON Data Type and Document Store
MySQL's JSON Data Type and Document StoreDave Stokes
 
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
 
Procedure To Store Database Object Size And Number Of Rows In Custom Table
Procedure To Store Database Object Size And Number Of Rows In Custom TableProcedure To Store Database Object Size And Number Of Rows In Custom Table
Procedure To Store Database Object Size And Number Of Rows In Custom TableAhmed Elshayeb
 
MySQL Idiosyncrasies That Bite
MySQL Idiosyncrasies That BiteMySQL Idiosyncrasies That Bite
MySQL Idiosyncrasies That BiteRonald Bradford
 
Intro to OTP in Elixir
Intro to OTP in ElixirIntro to OTP in Elixir
Intro to OTP in ElixirJesse Anderson
 
Python data structures
Python data structuresPython data structures
Python data structuresHarry Potter
 

What's hot (16)

Explain
ExplainExplain
Explain
 
My sq ltutorial
My sq ltutorialMy sq ltutorial
My sq ltutorial
 
Massively Distributed Backups at Facebook Scale - Shlomo Priymak, Facebook - ...
Massively Distributed Backups at Facebook Scale - Shlomo Priymak, Facebook - ...Massively Distributed Backups at Facebook Scale - Shlomo Priymak, Facebook - ...
Massively Distributed Backups at Facebook Scale - Shlomo Priymak, Facebook - ...
 
Codigos
CodigosCodigos
Codigos
 
mysqlHiep.ppt
mysqlHiep.pptmysqlHiep.ppt
mysqlHiep.ppt
 
Introduction To MySQL Lecture 1
Introduction To MySQL Lecture 1Introduction To MySQL Lecture 1
Introduction To MySQL Lecture 1
 
Materi my sql part 1
Materi my sql part 1Materi my sql part 1
Materi my sql part 1
 
MySQL's JSON Data Type and Document Store
MySQL's JSON Data Type and Document StoreMySQL's JSON Data Type and Document Store
MySQL's JSON Data Type and Document Store
 
Efficient Pagination Using MySQL
Efficient Pagination Using MySQLEfficient Pagination Using MySQL
Efficient Pagination Using MySQL
 
Tugas praktikum smbd
Tugas praktikum smbdTugas praktikum smbd
Tugas praktikum smbd
 
MySQL constraints
MySQL constraintsMySQL constraints
MySQL constraints
 
MySQL Built-In Functions
MySQL Built-In FunctionsMySQL Built-In Functions
MySQL Built-In Functions
 
Procedure To Store Database Object Size And Number Of Rows In Custom Table
Procedure To Store Database Object Size And Number Of Rows In Custom TableProcedure To Store Database Object Size And Number Of Rows In Custom Table
Procedure To Store Database Object Size And Number Of Rows In Custom Table
 
MySQL Idiosyncrasies That Bite
MySQL Idiosyncrasies That BiteMySQL Idiosyncrasies That Bite
MySQL Idiosyncrasies That Bite
 
Intro to OTP in Elixir
Intro to OTP in ElixirIntro to OTP in Elixir
Intro to OTP in Elixir
 
Python data structures
Python data structuresPython data structures
Python data structures
 

Similar to Lecture2 mysql by okello erick

mysql-Tutorial with Query presentation.ppt
mysql-Tutorial with Query presentation.pptmysql-Tutorial with Query presentation.ppt
mysql-Tutorial with Query presentation.pptaptechaligarh
 
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
 
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
 
MySQL Database System Hiep Dinh
MySQL Database System Hiep DinhMySQL Database System Hiep Dinh
MySQL Database System Hiep Dinhwebhostingguy
 
Short Intro to PHP and MySQL
Short Intro to PHP and MySQLShort Intro to PHP and MySQL
Short Intro to PHP and MySQLJussi Pohjolainen
 
Percona Live 4/15/15: Transparent sharding database virtualization engine (DVE)
Percona Live 4/15/15: Transparent sharding database virtualization engine (DVE)Percona Live 4/15/15: Transparent sharding database virtualization engine (DVE)
Percona Live 4/15/15: Transparent sharding database virtualization engine (DVE)Tesora
 
4. Data Manipulation.ppt
4. Data Manipulation.ppt4. Data Manipulation.ppt
4. Data Manipulation.pptKISHOYIANKISH
 
Ten Reasons Why You Should Prefer PostgreSQL to MySQL
Ten Reasons Why You Should Prefer PostgreSQL to MySQLTen Reasons Why You Should Prefer PostgreSQL to MySQL
Ten Reasons Why You Should Prefer PostgreSQL to MySQLanandology
 
database-querry-student-note
database-querry-student-notedatabase-querry-student-note
database-querry-student-noteLeerpiny Makouach
 
15 protips for mysql users pfz
15 protips for mysql users   pfz15 protips for mysql users   pfz
15 protips for mysql users pfzJoshua Thijssen
 
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
 
Introduction into MySQL Query Tuning
Introduction into MySQL Query TuningIntroduction into MySQL Query Tuning
Introduction into MySQL Query TuningSveta Smirnova
 

Similar to Lecture2 mysql by okello erick (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
 
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
 
15 MySQL Basics #burningkeyboards
15 MySQL Basics #burningkeyboards15 MySQL Basics #burningkeyboards
15 MySQL Basics #burningkeyboards
 
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
 
MySQLinsanity
MySQLinsanityMySQLinsanity
MySQLinsanity
 
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
 
MySQL SQL Tutorial
MySQL SQL TutorialMySQL SQL Tutorial
MySQL SQL Tutorial
 
Mysql basics1
Mysql basics1Mysql basics1
Mysql basics1
 
Short Intro to PHP and MySQL
Short Intro to PHP and MySQLShort Intro to PHP and MySQL
Short Intro to PHP and MySQL
 
Percona Live 4/15/15: Transparent sharding database virtualization engine (DVE)
Percona Live 4/15/15: Transparent sharding database virtualization engine (DVE)Percona Live 4/15/15: Transparent sharding database virtualization engine (DVE)
Percona Live 4/15/15: Transparent sharding database virtualization engine (DVE)
 
4. Data Manipulation.ppt
4. Data Manipulation.ppt4. Data Manipulation.ppt
4. Data Manipulation.ppt
 
Ten Reasons Why You Should Prefer PostgreSQL to MySQL
Ten Reasons Why You Should Prefer PostgreSQL to MySQLTen Reasons Why You Should Prefer PostgreSQL to MySQL
Ten Reasons Why You Should Prefer PostgreSQL to MySQL
 
MYSQL
MYSQLMYSQL
MYSQL
 
database-querry-student-note
database-querry-student-notedatabase-querry-student-note
database-querry-student-note
 
15 protips for mysql users pfz
15 protips for mysql users   pfz15 protips for mysql users   pfz
15 protips for mysql users pfz
 
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
 
Introduction into MySQL Query Tuning
Introduction into MySQL Query TuningIntroduction into MySQL Query Tuning
Introduction into MySQL Query Tuning
 

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

Search and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical FuturesSearch and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical FuturesBhaskar Mitra
 
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...Product School
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance
 
Demystifying gRPC in .Net by John Staveley
Demystifying gRPC in .Net by John StaveleyDemystifying gRPC in .Net by John Staveley
Demystifying gRPC in .Net by John StaveleyJohn Staveley
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Thierry Lestable
 
Knowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and backKnowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and backElena Simperl
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...Elena Simperl
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Alison B. Lowndes
 
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesThousandEyes
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...Product School
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3DianaGray10
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf91mobiles
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsPaul Groth
 
Quantum Computing: Current Landscape and the Future Role of APIs
Quantum Computing: Current Landscape and the Future Role of APIsQuantum Computing: Current Landscape and the Future Role of APIs
Quantum Computing: Current Landscape and the Future Role of APIsVlad Stirbu
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...BookNet Canada
 
IoT Analytics Company Presentation May 2024
IoT Analytics Company Presentation May 2024IoT Analytics Company Presentation May 2024
IoT Analytics Company Presentation May 2024IoTAnalytics
 
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...Product School
 
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance
 
In-Depth Performance Testing Guide for IT Professionals
In-Depth Performance Testing Guide for IT ProfessionalsIn-Depth Performance Testing Guide for IT Professionals
In-Depth Performance Testing Guide for IT ProfessionalsExpeed Software
 
Unpacking Value Delivery - Agile Oxford Meetup - May 2024.pptx
Unpacking Value Delivery - Agile Oxford Meetup - May 2024.pptxUnpacking Value Delivery - Agile Oxford Meetup - May 2024.pptx
Unpacking Value Delivery - Agile Oxford Meetup - May 2024.pptxDavid Michel
 

Recently uploaded (20)

Search and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical FuturesSearch and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical Futures
 
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
 
Demystifying gRPC in .Net by John Staveley
Demystifying gRPC in .Net by John StaveleyDemystifying gRPC in .Net by John Staveley
Demystifying gRPC in .Net by John Staveley
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
 
Knowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and backKnowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and back
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
 
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
 
Quantum Computing: Current Landscape and the Future Role of APIs
Quantum Computing: Current Landscape and the Future Role of APIsQuantum Computing: Current Landscape and the Future Role of APIs
Quantum Computing: Current Landscape and the Future Role of APIs
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
 
IoT Analytics Company Presentation May 2024
IoT Analytics Company Presentation May 2024IoT Analytics Company Presentation May 2024
IoT Analytics Company Presentation May 2024
 
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
 
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
 
In-Depth Performance Testing Guide for IT Professionals
In-Depth Performance Testing Guide for IT ProfessionalsIn-Depth Performance Testing Guide for IT Professionals
In-Depth Performance Testing Guide for IT Professionals
 
Unpacking Value Delivery - Agile Oxford Meetup - May 2024.pptx
Unpacking Value Delivery - Agile Oxford Meetup - May 2024.pptxUnpacking Value Delivery - Agile Oxford Meetup - May 2024.pptx
Unpacking Value Delivery - Agile Oxford Meetup - May 2024.pptx
 

Lecture2 mysql by okello erick

  • 2. Architecture of Web applications 22/10/2018 Relational Database (MySQL, Oracle, MS SQL) Internet Middleware PHP, ColdFusion ASP, JSP Web Server (Apache, IIS) Web Browser (Internet Explore Netscape)
  • 3. 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). 32/10/2018
  • 4. 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 fulfil the requests, and then indicate to the Web server exactly what to serve to the client’s browser. 42/10/2018
  • 5. Relational Databases  Relational database management systems (RDBMS) 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 52/10/2018
  • 6. 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 82/10/2018
  • 7. 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 92/10/2018
  • 9. 11 Road Map  Introduction to MySQL  Connecting and Disconnecting  Entering Basic Queries  Creating and Using a Database
  • 10. 12 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
  • 11. 13 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
  • 12. 14 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, ...);
  • 13. 15 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)
  • 14. 16 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;
  • 15. 17 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.
  • 16. 18 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.
  • 17. 19 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)
  • 18. 20 Showing Tables  To verify that the table has been created: mysql> show tables; +------------------+ | Tables_in_test | +------------------+ | pet | +------------------+ 1 row in set (0.01 sec)
  • 19. 21 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)
  • 20. 22 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)
  • 21. 23 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.
  • 22. 24 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
  • 23. 25 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
  • 24. 26 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
  • 25. 27 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)
  • 26. 28 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)
  • 27. 29 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";
  • 28. 30 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.)
  • 29. 31 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)
  • 30. 32 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)
  • 31. 33 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)
  • 32. 34 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.
  • 33. 35 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 | +--------+--------+---------+------+------------+------------+
  • 34. 36 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 | +--------+--------+---------+------+------------+-------+
  • 35. 37 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 | +----------+-------+---------+------+------------+------------+
  • 36. 38 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 | +-------+--------+---------+------+------------+-------+
  • 37. 39 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).
  • 38. 40 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.
  • 39. 41 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 | +--------+--------+---------+------+------------+------------+
  • 40. 42 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 | +--------+--------+---------+------+------------+-------+
  • 41. 43 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.
  • 42. 44 Counting Rows Example  A query to determine total number of pets: mysql> SELECT COUNT(*) FROM pet; +----------+ | COUNT(*) | +----------+ | 9 | +----------+
  • 43. 45 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
  • 44. 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 46
  • 45. 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 pet SET owner="lawrence", sex="F" WHERE name="Slim"; 47
  • 46. 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 pet WHERE name=“ "; 48
  • 47. TRUNCATE command •Truncate command deletes the data inside the table but not the table itself. •TRUNCATE TABLE table_name; ▫ E.g. TRUNCATE TABLE patientd; 49
  • 48. 50 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
  • 49. 51 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.