Professional Documents
Culture Documents
and MySQL
Introduction to Databases
A database is an ordered collection of
information from which a computer program
can quickly access information
Each row in a database table is called a record
A record in a database is a single complete
set of related information
Each column in a database table is called a
field
Fields are the individual categories of
information stored in a record
Name of Book
Introduction to Databases
(continued)
Introduction to Databases
(continued)
A flat-file database stores
information in a single table
A relational database stores
information across multiple related
tables
Understanding Relational
Databases
Relational databases consist of one or more
related tables
A primary table is the main table in a
relationship that is referenced by another
table
A related table (or child table)
references a primary table in a relational
database
A primary key is a field that contains a
unique identifier for each record in a
primary table
5
One-to-One Relationships
A one-to-one relationship exists between
two tables when a related table contains
exactly one record for each record in the
primary table
Create one-to-one relationships to break
information into multiple, logical sets
Information in the tables in a one-to-one
relationship can be placed within a single
table
Make the information in one of the tables
confidential and accessible only by certain
individuals
7
One-to-One Relationships
(continued)
One-to-One Relationships
(continued)
One-to-Many Relationship
A one-to-many relationship exists in a
relational database when one record in a
primary table has many related records in
a related table
Breaking tables into multiple related tables
to reduce redundant and duplicate
information is called normalization
Provides a more efficient and less
redundant method of storing this
information in a database
10
One-to-Many Relationship
(continued)
One-to-Many Relationship
(continued)
12
One-to-Many Relationship
(continued)
Many-to-Many Relationship
A many-to-many relationship exists
in a relational database when many
records in one table are related to
many records in another table
A junction table creates a one-tomany relationship for each of the two
tables in a
many-to-many relationship
A junction table contains foreign keys
from the two tables
14
15
18
20
MYSQL
Name of Book
21
What is MySQL?
MySQL is a database engine that uses
the standard SQL interface. MySQL is
very popular as a back end for web
applications.
The MySQL engine can be accessed
from most major
programming/scripting languages
such as perl, php, and ruby making it
easy to develop applications.
Name of Book
22
23
MySQL Commands
To list / view / see all the database just
type:
show databases;
To use the database just type:
use <database_name>;
Ex:
use `sample1`;
24
MySQL Commands
To list / view / see all the tables in the
database just type:
show tables; (show the tables inside
the
current database)
or
show tables from <database_name>; (to
Ex:
MySQL Commands
Create a database:
Syntax:
CREATE DATABASE <database_name>;
Ex:
CREATE DATABASE `sample1` ;
26
MySQL Commands
Edit / Rename a database:
MySql remove the rename feature so you
need to drop and create a new database
instead of just renaming it...
27
MySQL Commands
Delete a database:
Syntax:
DROP DATABASE <database_name>;
Ex:
DROP DATABASE `sample1` ;
28
MySQL Commands
Create a table:
29
MySQL Commands
Create a table:
Syntax:
CREATE TABLE <table_name> (<field_name>
<type>(size_of_the_field) NULL / NOT NULL)
Ex:
CREATE TABLE `sample1` (`student_name`
VARCHAR( 100 ) NULL);
30
MySQL Commands
Edit / Rename a table:
Syntax:
ALTER TABLE <old_table_name>
RENAME <new_table_name>;
Ex:
ALTER TABLE `sample1` RENAME
`sample2` ;
31
MySQL Commands
Delete a table:
Syntax:
DROP TABLE <table_name>;
Ex:
32
MySQL Commands
To show all columns / fields from a
table just type:
Syntax:
Show columns from <table_name>;
Ex:
Show columns from `sample1`;
33
MySQL Commands
Insert a field / column to a table:
Syntax:
ALTER TABLE <table_name> ADD
<field_name> <type>( <size> ) NULL /
NULL ;
NOT
Ex:
ALTER TABLE `sample1` ADD `address`
VARCHAR( 100 ) NULL ;
34
MySQL Commands
Edit / Rename a field / column of a
table;
Syntax:
ALTER TABLE <table_name> CHANGE
<old_field_name> <new_field_name>
<type>(<size>) NULL / NOT NULL;
Ex:
ALTER TABLE `sample1` CHANGE `name1`
`name` varchar(100) NULL;
35
MySQL Commands
Delete a field / column of a table;
Syntax:
ALTER TABLE <table_name> DROP
<field_name>;
Ex:
ALTER TABLE `sample1` DROP
`address`;
36
MySQL Commands
Inserting new record:
Syntax:
INSERT INTO <table_name>
(<field1>,<field2>...) VALUES
(<value1>,<value2>...);
Ex:
INSERT INTO `sample1` ( ` name` ,
`address` ) VALUES ('marco elias palic
valencia', 'porac, pampanga');
37
MySQL Commands
Edit a record:
A: Without a condition:
Syntax:
UPDATE <table_name> set <field_name1>
= <new_value1>, <field_name2>
=<new_value2>... ;
Ex:
UPDATE `sample1` SET ` name` = 'marco
elias palic valencia1',`address` =
'porac, pampanga1';
38
MySQL Commands
Edit a record:
B: With a condition:
Syntax:
UPDATE <table_name> set <field_name1> =
<new_value1>, <field_name2> = <new_value2>...
Where <field_name1> = <condition_value1> and
<field_name2> = <condition_value2>...;
Ex:
UPDATE `sample1` SET ` name` = 'marco elias
palic valencia1',`address` =
'porac,
pampanga1'
where name` = 'marco elias palic valencia' and
`address` =
'porac, pampanga';
39
MySQL Commands
Delete a record:
A: Without a condition:
Syntax:
DELETE from <table_name>;
Ex:
DELETE from `sample1`;
40
MySQL Commands
Delete a record:
B: With a condition:
Syntax:
DELETE from <table_name> where
<field_name1> = <condition_value1> and
<field_name2> = <condition_value2>...;
Ex:
DELETE FROM `sample1` WHERE `name` =
elias palic valencia' AND
pampanga';
'marco
`address` = 'porac,
41
MySQL Commands
Viewing / Searching for record(s)
A: Viewing all records:
Syntax:
SELECT <field1>, <field2> ... from <table_name>;
or if you want to select all fields just type *
(asterisk)
SELECT * from <table_name>;
Ex:
SELECT `name`,`address` from `sample1`;
or
SELECT * from `sample1`;
42
MySQL Commands
Viewing / Searching for record(s)
B: Searching for a particular record:
Syntax:
SELECT <field1>, <field2> ... from <table_name>
where <field1>=<condition_value1> and
<field2>=<condition_value2>...;
or if you want to select all fields just type * (asterisk)
SELECT * from <table_name> where
<field1>=<condition_value1> and
<field2>=<condition_value2>...;
43
MySQL Commands
Viewing / Searching for record(s)
B: Searching for a particular record:
Ex:
SELECT `name`,`address` FROM `sample1`
WHERE `name` = 'marco elias palic
valencia'
AND `address` = 'porac,
pampanga';
or
SELECT * FROM `sample1` WHERE `name` =
'marco elias palic valencia' AND `address` =
'porac, pampanga';
44
Name of Book
45
Creating Users
A proxy is someone or something that acts
or performs a request for another person
Create a separate account for each Web
application that needs to access a
database
Use a GRANT statement to create user
accounts and assign privileges
Privileges are the operations that a user
can perform with a database
47
48
GRANT Statement
The syntax for the GRANT statement is:
GRANT privilege [(column)] [, privilege [(columns)]] ...
ON {table | * | *.* | database.*}
TO user [IDENTIFIED BY 'password'];
Revoking Privileges
The syntax for the REVOKE statement is:
REVOKE privilege [(column)] [, privilege [(columns)]] ...
ON {table | * | *.* | database.*}
FROM user;
Deleting Users
To delete a user:
Revoke all privileges assigned to the user
account
for all databases
Use the REVOKE ALL PRIVILEGES statement
View the privileges assigned to a user account
with the SHOW GRANTS FOR user statement
51
52
53
Chapter 9
Manipulating MySQL
Databases with PHP
PHP Overview
PHP has the ability to access and
manipulate any database that is ODBC
compliant
PHP includes functionality that allows
you to work directly with different types
of databases, without going through
ODBC
PHP supports SQLite, database
abstraction layer functions, and PEAR DB
57
On Windows:
Copy the files libmysql.dll and
php_mysqli.dll to the installation directory
Edit the php.ini configuration file and
enable the extension=php_mysqli.dll
directive
58
61
Selecting a Database
Select a database with the use
database statement when you log on
to the MySQL Monitor
The syntax for the
mysqli_select_db() function is:
mysqli_select_db(connection, database)
64
65
66
68
69
70
71
72
73
74
77
82
85
Figure 9-9 Error code and message that prints when you attempt
to create a database that already exists
86
87
88
90
Figure 9-11 Error code and message that prints when you
attempt to create a table that already exists
91
Using the
mysqli_affected_rows() Function
With queries that return results (SELECT
queries), use the mysqli_num_rows()
function to find the number of records
returned from the query
With queries that modify tables but do
not return results (INSERT, UPDATE, and
DELETE queries), use the
mysqli_affected_rows() function to
determine the number of affected rows
95
96
97
INSERT INTO...SELECT...
INSERT INTO...VALUES (...),(...),(...)
LOAD DATA INFILE ...
ALTER TABLE ...
UPDATE
100
101
102
103
Name of Book
104
<?php
$connection1 =
mysql_connect("localhost","root","");
mysql_select_db("mydb",$connection1);
?>
<html>
<head>
</head>
<body>
<center>
Name of Book
105