PHP DATABASE
Lecture 6
Models of Reality
DML REALITY structures processes DDL DATABASE SYSTEM DATABASE
A database is a model of structures of reality The use of a database reflect processes of reality A database system is a software system which supports the definition and use of a database
Data Modeling
DATABASE SYSTEM REALITY structures processes data modeling MODEL
The model represents a perception of structures of reality The data modeling process is to fix a perception of structures of reality and represent this perception In the data modeling process we select aspects and we abstract
Database Design
The purpose of database design is to create a database which is a model of structures of reality supports queries and updates modeling processes of reality runs efficiently
Abstraction
It is very important that the language used for data representation supports abstraction There is three kinds of abstraction: Classification Aggregation Generalization
Database Normalization
Database normalization is the process of removing redundant data from your tables in to improve storage efficiency, data integrity, and scalability. Normalization generally involves splitting existing tables into multiple ones, which must be re-joined or linked each time a query is issued.
Database Normalization
Normalization consist of 1NF, 2NF and 3NF. Most tables when reaching 3NF are also in BCNF (Boyce-Codd Normal Form).
DATABASE SERVER
A database server is a program that can store large amounts of information in an organized format that is easily accessible from scripting languages like PHP. For example, you could tell PHP to look in the database for a list of jokes that you would like to appear on your Web site. In our case MYSQL will be our Database
What is MySQL?
MySQL is a database. The data in MySQL is stored in database objects called tables. A table is a collections of related data entries and it consists of columns and rows.
Database Tables
A database most often contains one or more tables. Each table is identified by a name (e.g. "Customers" or "Orders"). Tables contain records (rows) with data.
Table
Below is an example of a table called "Persons":
Lastname Mongi Makundi Mrema FirstName John Anna Jackson Address 332 7 8 City Arusha Mwanza Pugu
Queries
A query is a question or a request sent to database server. With MySQL, we can query a database for specific information and have a record set returned. Look at the following queries:
select * from persons select lastname from persons
Example of Query
Select lastname from persons
The query above selects all the data in the "LastName" column from the "Persons" table, and will return a recordset like this
Lastname
Mongi Makundi Mrema
Database Connection
Create a Connection to a MySQL Database Before you can access data in a database, you must create a connection to the database. Connection is done by
Find host machine of the database Select a required database in the host machine
Database Connection
Function Syntax
mysql_connect(servername,username,password);
Parameter servername username password Description Optional. Specifies the server to connect to. Default value is "localhost:3306 Optional. Specifies the username to log in with. Default value is the name of the user that owns the server process Optional. Specifies the password to log in with. Default is ""
Database Connection
In the following example we store the connection in a variable ($con) for later use in the script. con.php
<?php $con = mysql_connect("localhost",root","123"); if (!$con) { die('Could not connect: ' . mysql_error()); } // some code ?>
?>
Create a Table
The CREATE TABLE statement is used to create a table in MySQL. Syntax CREATE TABLE table_name ( column_name1 data_type, column_name2 data_type, column_name3 data_type, .... )
Primary Keys
Each table should have a primary key field. A primary key is used to uniquely identify the rows in a table. Each primary key value must be unique within the table. Furthermore, the primary key field cannot be null because the database engine requires a value to locate the record.
Example
$sql = "CREATE TABLE Persons ( personID int NOT NULL AUTO_INCREMENT, PRIMARY KEY(personID), FirstName varchar(15), LastName varchar(15), Age int )";
mysql_query($sql,$con);
Example of Insert
// establish connection <?php $con = mysql_connect("localhost","peter","abc123"); if (!$con) { die('Could not connect: ' . mysql_error()); } //create connection mysql_select_db("my_db", $con); //insert data into database mysql_query("INSERT INTO Persons (FirstName, LastName, Age)
VALUES ('Peter', 'Griffin', '35')");
?>
END