13-php and mysql
TRANSCRIPT
-
7/31/2019 13-Php and Mysql
1/57
PHP and MySQL
John Ryan B. Lorca
Instructor I
1
-
7/31/2019 13-Php and Mysql
2/57
Introduction to MySQL
SQL is a standard computer language for accessingand manipulating databases.
SQL stands for Structured Query Language
SQL allows you to access a database
SQL is an ANSI standard computer language
SQL can execute queries against a database
SQL can retrieve data from a database
SQL can insert new records in a database
SQL can delete records from a database
SQL can update records in a database
SQL is easy to learn
2
-
7/31/2019 13-Php and Mysql
3/57
Introduction to MySQL
SQL is an ANSI (American National Standards Institute)standard computer language for accessing andmanipulating database systems. SQL statements are usedto retrieve and update data in a database. SQL works withdatabase programs like MS Access, DB2, Informix, MS SQLServer, Oracle, Sybase, etc.
Unfortunately, there are many different versions of the SQLlanguage, but to be in compliance with the ANSI standard,they must support the same major keywords in a similarmanner (such as SELECT, UPDATE, DELETE, INSERT, WHERE,and others).
Note: Most of the SQL database programs also have theirown proprietary extensions in addition to the SQLstandard!
3
-
7/31/2019 13-Php and Mysql
4/57
SQL links
Tutorials http://www.w3schools.com/sql/
http://www.sqlzoo.net
http://sqlcourse.com (part 2) http://sqlcourse2/com (part 1)
MySQL online reference manual
http://dev.mysql.com/doc/mysql/en/Reference.html
4
http://www.w3schools.com/sql/http://www.sqlzoo.net/http://sqlcourse.com/http://sqlcourse2/comhttp://dev.mysql.com/doc/mysql/en/Reference.htmlhttp://dev.mysql.com/doc/mysql/en/Reference.htmlhttp://sqlcourse2/comhttp://sqlcourse.com/http://www.sqlzoo.net/http://www.w3schools.com/sql/http://www.w3schools.com/sql/ -
7/31/2019 13-Php and Mysql
5/57
Introduction to MySQL
SQL Database Tables
A database most often contains one or more tables. Each table isidentified by a name
(e.g. "Customers" or "Orders"). Tables contain records (rows) withdata.
Below is an example of a table called "Persons":
The table above contains three records (one for each person) andfour columns
(LastName, FirstName, Address, and City).
5
-
7/31/2019 13-Php and Mysql
6/57
Introduction to MySQL
With SQL, we can query a database and have a
result set returned.
A query like this:
Gives a result set like this:
6
-
7/31/2019 13-Php and Mysql
7/57
WARNING Always assume that everything is case sensitive,
especially table names.
This is not the case in Windows XP but it is thecase in Linux
7
-
7/31/2019 13-Php and Mysql
8/57
Entering commands (1)
Show all the databases SHOW DATABASES;
mysql> SHOW DATABASES;
+-------------+| Database |
+-------------+
| bookstore |
| employee_db |
| mysql || student_db |
| test |
| web_db |
+-------------+
8
-
7/31/2019 13-Php and Mysql
9/57
Entering commands (2)
Choosing a database and showing its tables USE test;
SHOW tables;
mysql> USE test;Database changed
mysql> SHOW tables;
+----------------+
| Tables_in_test |
+----------------+
| books |
| name2 || names |
| test |
+----------------+
4 rows in set (0.00 sec)
mysql>
9
-
7/31/2019 13-Php and Mysql
10/57
Entering commands (3)
Show the structure of a table DESCRIBE names;
mysql> DESCRIBE names;
+-----------+-------------+------+-----+---------+----------------+| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+----------------+
| id | int(11) | | PRI | NULL | auto_increment |
| firstName | varchar(20) | | | | |
| lastName | varchar(20) | | | | |
+-----------+-------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)
mysql>
10
-
7/31/2019 13-Php and Mysql
11/57
Entering commands (4)
Show the rows of a table (all columns) SELECT * FROM names;
mysql> SELECT * FROM names;
+----+-----------+------------+| id | firstName | lastName |
+----+-----------+------------+
| 1 | Fred | Flintstone |
| 2 | Barney | Rubble |
+----+-----------+------------+2 rows in set (0.00 sec)
mysql>
11
-
7/31/2019 13-Php and Mysql
12/57
Entering commands (5)
Inserting a new record INSERT INTO names (firstName,
lastName) VALUES ('Rock','Quarry');
SELECT * FROM names;mysql> INSERT INTO names (firstName, lastName) VALUES ('Ralph', 'Quarry');
Query OK, 1 row affected (0.02 sec)
mysql> SELECT * FROM names;
+----+-----------+------------+
| id | firstName | lastName |
+----+-----------+------------+| 1 | Fred | Flintstone |
| 2 | Barney | Rubble |
| 3 | Ralph | Quarry |
+----+-----------+------------+
3 rows in set (0.00 sec)
mysql>
12
-
7/31/2019 13-Php and Mysql
13/57
Entering commands (6)
Updating a record UPDATE names SET lastName = 'Stone'
WHERE id=3;
SELECT * FROM names;mysql> UPDATE names SET lastName = 'Stone' WHERE id=3;
Query OK, 1 row affected (0.28 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> SELECT * FROM names;
+----+-----------+------------+
| id | firstName | lastName |
+----+-----------+------------+
| 1 | Fred | Flintstone |
| 2 | Barney | Rubble |
| 3 | Ralph | Stone |
+----+-----------+------------+
3 rows in set (0.00 sec)
mysql>
13
-
7/31/2019 13-Php and Mysql
14/57
Database concepts (1)
A relational database management systemconsists of a number of databases.
Each database consists of a number of tables.
Example table
isbn title author pub year price
bookstable
rows
(records)
column
headings
14
-
7/31/2019 13-Php and Mysql
15/57
Some SQL data types (1)
Each entry in a row has a type specified by thecolumn.
Numeric data types
TINYINT, SMALLINT, MEDIUMINT, INT, BIGINT
FLOAT(display_length, decimals)
DOUBLE(display_length, decimals) DECIMAL(display_length, decimals)
NUMERIC is the same as DECIMAL
15
-
7/31/2019 13-Php and Mysql
16/57
Some SQL data types (2)
Date and time types DATE
format is YYYY-MM-DD
DATETIME
format YYYY-MM-DD HH:MM:SS
TIMESTAMP
format YYYYMMDDHHMMSS
TIME
format HH:MM:SS
YEAR
default length is 4
16
-
7/31/2019 13-Php and Mysql
17/57
SQL data types (3)
String types CHAR
fixed length string, e.g., CHAR(20)
VARCHAR variable length string, e.g., VARCHAR(20)
BLOB, TINYBLOB, MEDIUMBLOB, LONGBLOB
same as TEXT, TINYTEXT ...
ENUM
list of items from which value is selected
17
-
7/31/2019 13-Php and Mysql
18/57
SQL commands SHOW, USE
SHOW Display databases or tables in current database;
Example (command line client):
show databases; show tables;
USE
Specify which database to use Example
use bookstore;
18
-
7/31/2019 13-Php and Mysql
19/57
Introduction to MySQL Data
Definition Language CREATE creates a database table
CREATE TABLE table_name
(
column_name1 column_type1,column_name2 column_type2,
...
column_nameN column_typeN
);
Note: To create a database use the statement
CREATE db_name;
19
-
7/31/2019 13-Php and Mysql
20/57
Introduction to MySQL Data
Definition Language Specifying primary keys
CREATE TABLE table_name
(
column_name1 column_type1 NOT NULLDEFAULT '0',
column_name2 column_type2,
...
column_nameN column_typeN,PRIMARY KEY (column_name1)
);
20
-
7/31/2019 13-Php and Mysql
21/57
Introduction to MySQL Data
Definition Language
autoincrement primary integer keys
CREATE TABLE table_name
( column_name1 column_type1 PRIMARY
KEY NOT NULL DEFAULT '0'
AUTO_INCREMENT,
column_name2 column_type2,...
column_nameN column_typeN,
);
21
-
7/31/2019 13-Php and Mysql
22/57
Introduction to MySQL Data
Definition Language
Can also create UNIQUE keys. They are similar to PRIMARY KEYS
but can have NULL values.
Can also create INDEX fields.
22
-
7/31/2019 13-Php and Mysql
23/57
Introduction to MySQL Data
Manipulation Language
SQL Data Manipulation Language (DML)
SQL (Structured Query Language) is a syntax forexecuting queries. But the SQL
language also includes a syntax to update, insert, and
delete records. These query and update commands together form the
Data Manipulation Language
(DML) part of SQL:
SELECT - extracts data from a database table UPDATE - updates data in a database table
DELETE - deletes data from a database table
INSERT INTO - inserts new data into a database table
23
-
7/31/2019 13-Php and Mysql
24/57
Introduction to MySQL Data
Manipulation Language
The SQL SELECT Statement Syntax
The SELECT statement is used to select data
from a table. The tabular result is stored in a
result table (called the result-set).
24
-
7/31/2019 13-Php and Mysql
25/57
Introduction to MySQL Data
Manipulation LanguageThe SQL SELECT Statement Example
To select the content of columns named "LastName" and "FirstName",
from the database table called "Persons", use a SELECT statement like this:
The database table "Persons":
The result
25
-
7/31/2019 13-Php and Mysql
26/57
Introduction to MySQL Data
Manipulation Language
Select All Columns
To select all columns from the "Persons" table,
use a * symbol instead of column names, like
this:
Result
26
-
7/31/2019 13-Php and Mysql
27/57
Introduction to MySQL Data
Manipulation Language
The SELECT DISTINCT Statement Syntax
The DISTINCT keyword is used to return only distinct
(different) values.
The SELECT statement returns information from tablecolumns. But what if we only want to select distinct
elements?
With SQL, all we need to do is to add a DISTINCT
keyword to the SELECT statement:
27
-
7/31/2019 13-Php and Mysql
28/57
Introduction to MySQL Data
Manipulation LanguageUsing the DISTINCT keyword
To select ALL values from the column named "Company" we use a SELECT
statement
like this:
"Orders" table
Results
28
-
7/31/2019 13-Php and Mysql
29/57
Introduction to MySQL Data
Manipulation LanguageUsing the DISTINCT keyword
Note that "W3Schools" is listed twice in the result-set.
To select only DIFFERENT values from the column named "Company" we use a
SELECT DISTINCT statement like this:
Result
Now "W3Schools" is listed only once in the result-set.
29
-
7/31/2019 13-Php and Mysql
30/57
Introduction to MySQL Data
Manipulation LanguageThe WHERE Clause - Syntax To conditionally select data from a table, a WHERE clause can be added to
the SELECT statement.
With the WHERE clause, the following operators can be used:
Note: In some versions of SQL the operator may be written as != 30
-
7/31/2019 13-Php and Mysql
31/57
Introduction to MySQL Data
Manipulation LanguageUsing the WHERE Clause
To select only the persons living in the city "Sandnes", we add a WHERE
clause to the SELECT statement:
"Persons" table
Result
31
-
7/31/2019 13-Php and Mysql
32/57
Introduction to MySQL Data
Manipulation LanguageUsing Quotes Note that we have used single quotes around the conditional values in the
examples.
SQL uses single quotes around text values (most database systems will also
accept
double quotes). Numeric values should not be enclosed in quotes. For text values:
For numeric values:
32
-
7/31/2019 13-Php and Mysql
33/57
Introduction to MySQL Data
Manipulation Language
The LIKE Condition - Syntax The LIKE condition is used to specify a search for
a pattern in a column.
A "%" sign can be used to define wildcards(missing letters in the pattern) both before andafter the pattern.
33
-
7/31/2019 13-Php and Mysql
34/57
Introduction to MySQL Data
Manipulation LanguageUsing LIKE
The following SQL statement will return persons with first names that start
with an 'O':
The following SQL statement will return persons with first names that end
with an 'a':
The following SQL statement will return persons with first names that
contain the pattern 'la':
34
-
7/31/2019 13-Php and Mysql
35/57
Introduction to MySQL Data
Manipulation Language
The INSERT INTO Statement Syntax
The INSERT INTO statement is used to insert
new rows into a table.
You can also specify the columns for whichyou want to insert data:
35
-
7/31/2019 13-Php and Mysql
36/57
Introduction to MySQL Data
Manipulation Language
Insert a New Row
This "Persons" table:
And this SQL statement:
Will give this result:
36
-
7/31/2019 13-Php and Mysql
37/57
Introduction to MySQL Data
Manipulation Language
The Update Statement Syntax
The UPDATE statement is used to modify the
data in a table.
Person
37
-
7/31/2019 13-Php and Mysql
38/57
Introduction to MySQL Data
Manipulation Language
Update one Column in a Row
We want to add a first name to the person
with a last name of "Rasmussen":
Result
38
-
7/31/2019 13-Php and Mysql
39/57
Introduction to MySQL Data
Manipulation Language
Update several Columns in a Row
We want to change the address and add the
name of the city:
Result
39
-
7/31/2019 13-Php and Mysql
40/57
Introduction to MySQL Data
Manipulation Language
The DELETE Statement Syntax
The DELETE statement is used to delete rows
in a table.
Person
40
-
7/31/2019 13-Php and Mysql
41/57
Introduction to MySQL Data
Manipulation Language
Delete a Row
"Nina Rasmussen" is going to be deleted:
Result
41
-
7/31/2019 13-Php and Mysql
42/57
Introduction to MySQL Data
Manipulation Language
Delete All Rows
It is possible to delete all rows in a table
without deleting the table. This means that
the table structure, attributes, and indexes
will be intact:
42
-
7/31/2019 13-Php and Mysql
43/57
Introduction to MySQL Data
Manipulation LanguageSort the Rows
The ORDER BY clause is used to sort the rows.
Orders:
To display the company names in alphabetical order:
Result:
43
-
7/31/2019 13-Php and Mysql
44/57
Introduction to MySQL Data
Manipulation Language
Sort the Rows
To display the company names in alphabetical order
AND the OrderNumber in numerical order:
Result:
44
-
7/31/2019 13-Php and Mysql
45/57
Introduction to MySQL Data
Manipulation Language
Sort the Rows
To display the company names in reverse
alphabetical order:
Result:
45
d
-
7/31/2019 13-Php and Mysql
46/57
Introduction to MySQL Data
Manipulation LanguageSort the Rows To display the company names in reverse alphabetical order AND the
OrderNumber in numerical order:
Result:
Notice that there are two equal company names (W3Schools) in the result
above. The only time you will see the second column in ASC order would
be when there are duplicated values in the first sort column, or a handful
of nulls.
46
d i
-
7/31/2019 13-Php and Mysql
47/57
Introduction to MySQL Data
Manipulation Language
AND & OR
AND and OR join two or more conditions in a
WHERE clause.
The AND operator displays a row if ALL
conditions listed are true. The OR operator
displays a row if ANY of the conditions listed
are true.
Original Table (used in the examples)
47
I d i M SQL D
-
7/31/2019 13-Php and Mysql
48/57
Introduction to MySQL Data
Manipulation Language
AND & OR
Use AND to display each person with the first
name equal to "Tove", and the last name
equal to "Svendson":
Result:
48
I d i M SQL D
-
7/31/2019 13-Php and Mysql
49/57
Introduction to MySQL Data
Manipulation Language
AND & OR
Use OR to display each person with the first
name equal to "Tove", or the last name equal
to "Svendson":
Result:
49
I d i M SQL D
-
7/31/2019 13-Php and Mysql
50/57
Introduction to MySQL Data
Manipulation Language
AND & OR
You can also combine AND and OR (use
parentheses to form complex expressions):
Result:
50
I t d ti t M SQL D t
-
7/31/2019 13-Php and Mysql
51/57
Introduction to MySQL Data
Manipulation Language
IN The IN operator may be used if you know
the exact value you want to return for at
least one of the columns.
Original Table (used in the examples)
51
I t d ti t M SQL D t
-
7/31/2019 13-Php and Mysql
52/57
Introduction to MySQL Data
Manipulation Language
IN
To display the persons with LastName equal to
"Hansen" or "Pettersen", use the following
SQL:
Result:
52
I t d ti t M SQL D t
-
7/31/2019 13-Php and Mysql
53/57
Introduction to MySQL Data
Manipulation Language
BETWEEN ... AND
The BETWEEN ... AND operator selects a range
of data between two values. These values can
be numbers, text, or dates.
Original Table (used in the examples)
53
I t d ti t M SQL D t
-
7/31/2019 13-Php and Mysql
54/57
Introduction to MySQL Data
Manipulation Language
BETWEEN ... AND
To display the persons alphabetically between
(and including) "Hansen" and exclusive
"Pettersen", use the following SQL:
Result:
54
I t d ti t M SQL D t
-
7/31/2019 13-Php and Mysql
55/57
Introduction to MySQL Data
Manipulation Language BETWEEN ... AND IMPORTANT!
The BETWEEN...AND operator is treated differently in different databases.
With some databases a person with the LastName of "Hansen" or"Pettersen" will not be listed (BETWEEN..AND only selects fields that arebetween and excluding the test values).
With some databases a person with the last name of "Hansen" or"Pettersen" will be listed (BETWEEN..AND selects fields that are betweenand including the test values).
With other databases a person with the last name of "Hansen" will belisted, but "Pettersen" will not be listed (BETWEEN..AND selects fieldsbetween the test values, including the first test value and excluding the
last test value). Therefore: Check how your database treats the BETWEEN....AND operator!
55
I t d ti t M SQL D t
-
7/31/2019 13-Php and Mysql
56/57
Introduction to MySQL Data
Manipulation Language
BETWEEN ... AND
To display the persons outside the range used
in the previous example, use the NOT
operator:
Result:
56
-
7/31/2019 13-Php and Mysql
57/57
PHP and MySQL