Introduction To MYSQL: Objectives

Download as pdf or txt
Download as pdf or txt
You are on page 1of 22

Web Development

1
Introduction to MYSQL

Introduction to MYSQL

Objectives:
At the end of the module the student is expected to:
1. Understand the meaning of MYSQL and its functionalities
2. Understand the application of phpmyadmin.
3. Apply SQL statements using
a. Select
b. Select individual and multiple column
c. All column
d. District row
e. Sorting data
f. Filtering using where clause
g. Wildcard filtering

Introduction to MYSQL

MySQL is the world's most popular open source database. With its

proven performance, reliability and ease-of-use, MySQL has become the

leading database choice for web-based applications, used by high profile web

properties including Facebook, Twitter, YouTube, Yahoo! and many more.

What is MySQL?
 MySQL is a database system used on the web
 MySQL is a database system that runs on a server
 MySQL is ideal for both small and large applications
 MySQL is very fast, reliable, and easy to use
 MySQL uses standard SQL
 MySQL compiles on a number of platforms
 MySQL is free to download and use
 MySQL is developed, distributed, and supported by Oracle
Corporation
 MySQL is named after co-founder Monty Widenius's daughter: My
The data in a MySQL database are stored in tables. A table is a collection
of related data, and it consists of columns and rows.
Course Module
PHP + MySQL Database System
 PHP combined with MySQL are cross-platform (you can develop in
Windows and serve on a Unix platform)

Database Queries
A query is a question or a request.
We can query a database for specific information and have a recordset
returned.

Since we have installed xampp as our server, MySQL is already


included in the package, all you need to do is to start the database service.

What is PhpMyAdmin?

PhpMyAdmin is one of the most popular applications for MySQL

databases management. It is a free tool written in PHP. Through this software

you can create, alter, drop, delete, import and export MySQL database tables.

You can run MySQL queries, optimize, repair and check tables, change

collation and execute other database management commands.

The main PhpMyAdmin features are as follows:


1. User-friendly web interface;
Web Development
3
Introduction to MYSQL

2. Support for most MySQL functions like browse, drop, create, copy and
alter databases, tables, views, fields and indexes, execute MySQL
queries, manage stored procedures and functions;
3. Import data from CSV and SQL files;
4. Export data to various formats: CSV, SQL, XML, PDF, ISO/IEC 26300 -
OpenDocument Text and Spreadsheet, Word, Excel, LATEX and
others;
5. Searching globally in a database or a subset of it;
6. And much more.

In the following pages of our PhpMyAdmin Tutorial we will describe the


functionality of the software, integrated in cPanel.
Once you enter your PhpMyAdmin application, you will see different areas.

In the upper part you will find the server hostname. The databases

which you will manage are stored on the same server as the software and

the hostname is: localhost.

Under it there is information regarding the MySQL server, the

MySQL client and the PhpMyAdmin version.

Course Module
Databases

In the Databases tab you will find a list with all the databases which

can be managed through the cPanel user.

SELECT Statement

The SELECT statement is used to select data from a database. The


result is stored in a result table, called the result-set.

SQL SELECT Syntax

SELECT column_name,column_name
FROM table_name;

Retrieving Individual Columns

SQL Statement

SELECT prod_name
FROM products;

Result
Web Development
5
Introduction to MYSQL

Analysis

SELECT statement to retrieve a single column called


prod_name from the products table.

Retrieving Multiple Columns

To retrieve multiple columns from a table, the same SELECT


statement is used. The only difference is that multiple column names must be
specified after the SELECT keyword, and each column must be separated by a
comma.

SQL Statement

SELECT prod_id, prod_name, prod_price


FROM products;

Course Module
Result

Analysis

This statement uses the SELECT statement to retrieve data


from the products table. In this example, three column names
are specified, each separated by a comma.

Retrieving All Columns

You can use SELECT statements to request all columns without


having to list them individually. This is done using the asterisk (*) wildcard
character in lieu of actual column names

SQL Statement

Select *
FROM products;
Web Development
7
Introduction to MYSQL

Result

Analysis

Wildcard (*) is specified, all the columns in the table are


returned. The columns are in the order in which they appear
in the table definition.

Retrieving Distinct Rows

To retrieve unique rows in a table the keyword to use is DISTINCT

SQL Statement

SELECT DISTINCT vend_id


FROM products;

Course Module
Result

Analysis

SELECT DISTINCT vend_id tells SQL Server to only return distinct


(unique) vend_id rows, and therefore only three rows are returned.

Using Fully Qualified Table Names

SQL Statement

SELECT products.prod_name
FROM tutorial.products;

Result

Analysis
In this section we have specified the name of the table including the
symbol (.), we also specify the database name, in this example we
have use the database name tutorial.
Web Development
9
Introduction to MYSQL

Sorting Data

SELECT statements return all matched rows, possibly every row in the
specified table.

Sorting single column

SQL Statement

SELECT prod_name
FROM products
ORDER BY prod_name;

Result

Analysis
To explicitly sort data retrieved using a SELECT statement, you use
the ORDER BY clause. ORDER BY takes the name of one or more
columns by which to sort the output.
Course Module
Sorting by Multiple Columns

SQL Statement

SELECT prod_id, prod_price, prod_name


FROM products
ORDER BY prod_price, prod_name;

Result

Specifying Sort Direction

Data sorting is not limited to ascending sort orders (from A to Z).


Although this is the default sort order, the ORDER BY clause can also be
used to sort in descending order (from Z to A). To sort by descending
order, you must specify the keyword DESC.

SQL Statement

SELECT prod_id, prod_price, prod_name


FROM products
ORDER BY prod_price DESC;
Web Development
11
Introduction to MYSQL

Result

Filtering Data

Using the WHERE Clause

Within a SELECT statement, you filter data by specifying search


criteria in the WHERE clause.

SQL Statement

SELECT prod_name, prod_price


FROM products
WHERE prod_price = 2.50;

Result

Analysis
This statement retrieves two columns from the products table, but
instead of returning all rows, it returns only rows with a prod_price
value of 2.50.
Course Module
The WHERE Clause Operators

Example 1

SQL Statement

SELECT prod_name, prod_price


FROM products
WHERE prod_name = 'fuses';

Result

Analysis
This statement retrieves two columns from the products table, the
prod_name and prod_price where the prod_name is equal to ‘fuses

Example 2

SQL Statement

SELECT prod_name, prod_price


FROM products
WHERE prod_price < 10;
Web Development
13
Introduction to MYSQL

Result

Analysis
This statement retrieves two columns from the products table, the
prod_name and prod_price where prod_price < 10.

Example 3

SQL Statement

SELECT prod_name, prod_price


FROM products
WHERE prod_price <= 10;

Result

Course Module
Analysis
This statement retrieves two columns from the products table, the
prod_name and prod_price where prod_price <=10.

Checking for Nonmatches

SQL Statement

SELECT vend_id, prod_name


FROM products
WHERE vend_id <> 1003;

OR

SELECT vend_id, prod_name


FROM products
WHERE vend_id != 1003;

Result

Checking for a Range of Values

SQL Statement

SELECT prod_name, prod_price


FROM products
WHERE prod_price BETWEEN 5 AND 10;
Result
Web Development
15
Introduction to MYSQL

Analysis
When BETWEEN is used, two values must be specified, the low end
and high end of the desired range. The two values must also be
separated by the AND keyword. BETWEEN matches all the values in
the range, including the specified range start and end values.

Checking for No Value

NULL No value, as opposed to a field containing 0, or an empty string,


or just spaces.

SQL Statement

SELECT cust_id
FROM customers
WHERE cust_email IS NULL;

Result

Analysis
Here the query retrieves the columns with empty cust_email.

Course Module
Advanced Data Filtering

Combining WHERE Clauses with AND OPERATOR

SQL Statement

SELECT prod_id, prod_price, prod_name


FROM products
WHERE vend_id = 1003 AND prod_price <= 10;

Result

Analysis
The preceding SQL statement retrieves the product name and price
for all products made by vendor 1003 as long as the price is 10 or less.
The WHERE clause in this SELECT statement is made up of two
conditions, and the keyword AND is used to join them. AND instructs
the SQL Server to return only rows that meet all the conditions
specified. If a product is made by vendor 1003 but it costs more than
10, it is not retrieved. Similarly, products that cost less than 10 that
are made by a vendor other than the one specified are not retrieved.

Using the OR Operator

SQL Statement

SELECT prod_name, prod_price,vend_id


FROM products
WHERE vend_id = 1002 OR vend_id = 1003;
Web Development
17
Introduction to MYSQL

Result

Analysis
This SQL statement retrieves the product name and price for any
products made by either of the two specified vendors. The OR
operator tells the DBMS to match either condition, not necessarily
both. If an AND operator would have been used here, no data would
be returned. (It would have created a WHERE clause that could never
be matched.)

Using the IN Operator

SQL Statement

SELECT prod_name, prod_price, vend_id


FROM products
WHERE vend_id IN (1002,1003)
ORDER BY prod_name;

Same as

SELECT prod_name, prod_price


FROM products
WHERE vend_id = 1002 OR vend_id = 1003
ORDER BY prod_name;

The IN operator is much cleaner compare to the second statement.


Course Module
Result

Analysis
The SELECT statement retrieves all products made by vendor 1002
and vendor 1003. The IN operator is followed by a comma-delimited
list of valid values, and the entire list must be enclosed within
parentheses.

Using the NOT Operator

The WHERE clause’s NOT operator has one function and one
function only: NOT negates whatever condition comes next.

SQL Statement

SELECT prod_name, prod_price, vend_id


FROM products
WHERE vend_id NOT IN (1002,1003)
ORDER BY prod_name;

Result
Web Development
19
Introduction to MYSQL

Analysis
The NOT here negates the condition that follows it; so instead of
matching vend_id to ‘1002 or ‘1003’, SQL Server matches vend_id to
anything that is not ‘1003 or ‘1002

Using Wildcard Filtering

Wildcards Special characters used to match parts of a value.

Search pattern A search condition made up of literal text, wildcard


characters, or any combination of the two.

The Percent Sign (%) Wildcard

SQL Statement

SELECT prod_id, prod_name


FROM products
WHERE prod_name LIKE 'jet%'

Result

Analysis
The search pattern ‘’jet%’ means match any value that contains the
text teddy anywhere within it, regardless of any characters before or
after that text.

Course Module
SQL Statement

SELECT prod_id, prod_name


FROM products
WHERE prod_name LIKE '%anvil%';

Result

Analysis
The search pattern '%anvil%' means match any value that contains
the text anvil anywhere within it, regardless of any characters before
or after that text.

SQL STATEMENT

SELECT prod_name
FROM products
WHERE prod_name LIKE 's%e'

RESULT

Analysis
Wildcards can also be used in the middle of a search pattern, although
that is rarely useful. The following example finds all products that
begin with an s and end with an e:
Web Development
21
Introduction to MYSQL

The Underscore (_) Wildcard

Another useful wildcard is the underscore (_). The underscore is used just
like %, but instead of matching multiple characters, the underscore matches
just a single character.

SQL Statement

SELECT prod_id, prod_name


FROM products
WHERE prod_name LIKE '_ ton anvil';

Result

Analysis
The search pattern used in this WHERE clause specifies a wildcard
followed by literal text. The results shown are the only rows that
match the search pattern: The underscore matches 1 in the first row
and 2 in the second row. The .5 ton anvil product did not match
because the search pattern matched a single character, not two. By
contrast, the following SELECT statement uses the % wildcard and
returns three matching products:

SQL Statement

SELECT prod_id, prod_name


FROM products
WHERE prod_name LIKE '% ton anvil';

Course Module
Result

Analysis
Unlike %, which can match zero characters, _ always matches one
character no more and no less.

References
Murach, J. (2014) Murach’s PHP and MYSQL (2nd Edition)

Yank, K, PHP and MYSQL: Novice to Ninja

WEBSITE
http://php.net/

http://www.w3schools.com/php/

https://www.tutorialspoint.com/php/

You might also like