Download - Ch.8a Mysql Database
-
8/9/2019 Ch.8a Mysql Database
1/18
MySQL Database Management System
By Tony Chun-Kuen WONG
8/03/2010
Problems with Data in file
To store data into a file Difficult to retrieve the data
No standard format for the data stored in the file
Difficult to handle data within same category
Difficult to search a specific data within a file
..
retrieval of large amount of data. Amazon.com
Internet banking
-
8/9/2019 Ch.8a Mysql Database
2/18
There are different ways to store and manipulate data:
A simple text file-- . .
A relational database (e.g. Oracle, Sybase, MySQL, ACCESS)
For large amount of data and complex structure (i.e.,
large number of tables related to one another),
RDBMS is a better choice.
More flexible
Better access control (granting different privileges to different users
for the same database, table, or even attribute)
Examples
Show catalogue using products databases
Recognize the user using membership database
Dynamic Posting by examining users characteristics and
posting appropriate information that retrieve data fromrelated database.
-
8/9/2019 Ch.8a Mysql Database
3/18
MySQL DBMS
The RDBMS that we use is called MySQL
MySQL DBMS is a Client-Server application
A database server program is running on a server machine In our case, the server machine is ism
The database server program is call mysqld
and updates
The server program wait for requests from clients
Client use SQL to request data
The clients can be run on the same machine as the server,or on other mach nes on a networ
Accessing Database Server by a Client
Database Server Machine (Wisdom)isem03
MySQL
Database
Database
INTERNET
SQL
Server(mysqld)DATA
Database Server Machine (Wisdom)isem03
MySQL
Database
ServerMySQL
(mysqld)en
(mysql)
SQL
-
8/9/2019 Ch.8a Mysql Database
4/18
Accessing DBMS by Web Applications
Web servers do not access the DBMS directly
A web-to-database middleware does
CGI program written in C, Pascal, PERL, etc.
ColdFusion, Active Server Page, PHP (Scripting Language)
We use PHP in our course
PHP has functions (application programming interface or
API) for manipulating a number of DBMS, such as Oracle,
, an y .
Accessing DBMS by Web Applications
book.php
Web Server Machine
< >
PHP
Interpreter
MySQL
Database
Server
< >
Napoleon< /HTML>
W eb
Database
HTTP Requesthttp:/ /abc.com/book.php
INTERNET
Napoleon
-
8/9/2019 Ch.8a Mysql Database
5/18
Example Form
ttp: sem . u.e u. ~ semuser mysq orm. tm
Customer ID Input Form
-
8/9/2019 Ch.8a Mysql Database
6/18
Accessing DBMS by Web Applications
The previous arrangement is a two-tier architecture, i.e., the web
browser (client) is responsible for presentation and the server machine
is responsible for business logic (processing) and data retrieval and
update.
What happen if the load of the web-server machine is very high or the
data base is very large (i.e., millions of records)?
e server mac nes can e arrange us ng ree- er en erver
Architecture
Web browser responsible for presentation (note: if javascript is used, the
browser also responsible for part of the processing)
Web Server machine responsible for business logic
Database Server machine responsible for data retrieval and update
You can arrange servers in n-tier architecture
Three-Tier Client Server Architecture
book.php
Web Server Machine (Wisdom)isem03
PHP
Interpreter
< >
Napoleon
Web
INTERNET
Server
INTERNET
MySQL
Database
Database
eques
http://abc.com/book.php
Napoleon
Server
(mysqld)
Database Server Machine (ISM02)
-
8/9/2019 Ch.8a Mysql Database
7/18
Three-Tier Example
http://isem03.hkbu.edu.hk/~isemuser20/mysql/formthree.html
This is Three-tier demonstration
-
8/9/2019 Ch.8a Mysql Database
8/18
Data Definition CommandsData Definition Commands
Creatin the Database Structure
Example:CREATE DATABASE db1020;
Data Definition CommandsData Definition Commands
Creatin Table Structures
CREATE TABLE
);
-
8/9/2019 Ch.8a Mysql Database
9/18
Data Definition CommandsData Definition Commands
create table customer
(customer_id int not null unique,
customer_name varchar(20)not null,_ ,
primary key (customer_id));
create table orders
_
order_total float,
_ ,
order_date date,
_
Basic Data ManagementBasic Data Management
Data Entr
INSERT INTO VALUES (attribute 1
value, attribute 2 value, etc.);
insert into customer
values(1, ludwig chang, 12345678);
values(1, 200, 1, 2009-02-14);
-
8/9/2019 Ch.8a Mysql Database
10/18
Basic Data ManagementBasic Data Management
Makin a CorrectionUPDATE table name SET attribute = new_value
[, attribute = new_value ]
where conditions
update customer
se cus omer_p one =
where customer_id = 1;
Basic Data ManagementBasic Data Management
DELETE FROMtable
WHERE conditions
or ers
WHERE order_id = 6;
-
8/9/2019 Ch.8a Mysql Database
11/18
QueriesQueries
General Format of SELECT statement
SELECT [DISTINCT | ALL] {* | column_list}
FROM table name [alias] [, table_name [alias]] ....
[WHERE conditions ]
[GROUP BY column_list ]
[HAVING condition ]
ORDER BY column list _
select order_id, order_total from orderswhere customer id = 1_
+----------+-------------+
| order_id | order_total |
+----------+-------------+
| 1 | 200 |
| 2 | 500 |
+----------+-------------+
How to run MySQL statements in php ?
We just use a string to store the MySQL statement and
execute it using a php MySQL function statement
-
8/9/2019 Ch.8a Mysql Database
12/18
MySQL statements
CREATE TABLE tonytable (first_name VARCHAR(30),second nameVARCHAR 30 a e INT_
INSERT INTO tonytable (first_name, second_name, age) VALUES (Tony,WONG,37);
*
UPDATE tonytable SET first_name = Samuel;
Using WHERE for checking conditions
Connecting to the MySQL sever mysql_connect ( )
Open a connection to a MySQL Server
mysql_connect ([string hostname [:port][:/path/to/socket] [, string username [, stringpasswor
Returns a pos t ve My L n dent er on success, or an
error message on failure.
-
8/9/2019 Ch.8a Mysql Database
13/18
The following defaults are assumed for missing optional parameters:
= ' ' =,
the server process andpassword= empty password.
If a second call is made to mysql_connect( ) with the same arguments,
no new link will be established, but instead, the link identifier of the
already opened link will be returned.
The link to the server will be closed as soon as the execution of the
script ends, unless it's closed earlier by explicitly calling
mysql_close( ).
MySQL connect example
Ref : mysql01.php
htt ://isem03.hkbu.edu.hk/~isemuser20/m s l/m s l01. h
-
8/9/2019 Ch.8a Mysql Database
14/18
_ _
with two major differences.
First, when connecting, the function would first try to find a
(persistent) link that's already open with the same host, username. ,
instead of opening a new connection.
Second, the connection to the SQL server will not be closed when
the execution of the script ends. Instead, the link will remain open
_
mysql_pconnect( )).
This type of links is therefore called 'persistent'.
- _ _
$db = db1020;
mysql_select_db($db) or die (Couldnt open $db);
Returns: true on success, false on error.
mysql_select_db( ) sets the current active database on the server that's associated
with the specified link identifier. If no link identifier is specified, the last
o ened link is assumed. If no link is o en the function will tr to establish a
link as ifmysql_connect( ) was called, and use it.
Every subsequent call to mysql_query( ) will be made on the active database.
-
8/9/2019 Ch.8a Mysql Database
15/18
pen ng a connec on an e ec ng a a a ases
$user = isemuserXX;
$pass = your_password;
$db = db10XX;
$link = mysql_connect(localhost, $user, $pass);
If ! link
die (couldnt connect to MySQL);
print Successfully connected to sever
;
mysql_select_db( db) or
die (couldnt open $db : .mysql_error ( ));
print Sucessfully select database \$db\
;mysql_close($link);
?>
Ref : mysql02.php
http://isem03.hkbu.edu.hk/~isemuser20/mysql/mysql02.php
Other MySQL functions
-
8/9/2019 Ch.8a Mysql Database
16/18
mysql_affected_rows ( ) Get number of affected rows inprevious MySQL operation
mysql_change_user ( ) Change logged in user of the activeconnec on
mysq _c ose ose y connec on
mysq _create_ reate a y ata ase
mysql_data_seek ( ) Move internal result pointer
mysq _ _name et resu t ata
mysq _db_query end a y query
mysql_drop_db ( ) Drop (delete) a MySQL database
mysql_errno ( ) Returns the numerical value of the errormessage from previous MySQL operation
mysql_error ( ) Returns the text of the error message fromprev ous y opera on
-
8/9/2019 Ch.8a Mysql Database
17/18
mysq _ etc _array etc a resu t row as an assoc at vearray, a numeric array, or both.
mysql_fetch_assoc ( ) Fetch a result row as an associative
_ _and return as an object
mysql_fetch_lengths ( ) Get the length of each output in a
result
m s l fetch ob ect Fetch a result row as an ob ect_ _
mysql_fetch_row ( ) Get a result row as an enumerated array
mysql_field_flags ( ) Get the flags associated with thespecified field in a result
mysql_field_name ( ) Get the name of the specified field in aresult
mysql_field_len ( ) Returns the length of the specified field
mysql_field_seek ( ) Set result pointer to a specified fieldoffset
-
8/9/2019 Ch.8a Mysql Database
18/18
mysql_field_table ( ) Get name of the table the specified fieldis in
mysql_field_type ( ) Get the type of the specified field in a
result
mysql_free_result ( ) Free result memory
mysql_insert_id ( ) Get the id generated from the previousINSERT operation
mysql_list_dbs ( ) List databases available on a MySQLserver
mysql_list_fields ( ) List MySQL result fields
m s l list tables ( ) List tables in a M S L database_ _
mysql_num_fields ( ) Get number of fields in result
m s l num rows Get number of rows in result_ _
mysql_query ( ) Send a MySQL query
m s l result Get result data_
mysql_tablename ( ) Get table name of field