You are on page 1of 8

Database Design

PHP 101
Why This Matters
PHP 101 – Database Design

Your database structure represents your Your code is really secondary to your database We’re going to talk about your object model.
upper limit in terms of flexibility, scalability, structure. The structure of the database
ease of upgrade, scope, clarity, etc. determines what/how you’ll have to code.

The more modular and flexible your So, make it easier on yourself. The three different types of tables you MUST have.
database structure is, the easier it will be go
scale your application.

Getting clear on your database structure Develop a database structure that makes the code And, consistent step-by-step “mapping” process
first makes writing your code much easier. you have to write cleaner, clearer and more you can follow for every application you build.
efficient… while still remaining easily scalable.
What IS An Object Model?
The Object Model is the “blueprint” of your application. It’s the objects your
application will support, the properties of those objects, the actions those objects
can take and the relationships between the objects.

Object Model For example, consider a blogging application. A simple blog application would likely
PHP 101 – Database Design consist of USER, POST and CATEGORY objects. The POST object might have the
properties: title, content, date, status, etc. and may have relationships with the
CATEGORY object. The USER object may have the properties: username, password,
email, etc. and be able to perform the actions: CREATE POST, EDIT POST, etc.
Example Object Model
PHP 101 – Database Design
Object Properties Actions Relationships
What IS a Data Model?
The Data Model is your application “blueprint” at the database level. It deals with
how data will ultimately be stored in your database. Essentially, you “map” your
Object Model to your Data Model detailing where the properties, meta and
relationships data will be stored in your database.

Data Model
PHP 101 – Database Design For example, the properties of each instance of your POST object may be stored in
a table named “users” and include the columns title, content, date and status. It
may have relationships to the CATEGORY object stored in the post_categories
table… which shows that XYZ POST belongs to XYZ CATEGORY.
Create Data Model
PHP 101 – Database Design

CREATE OBJECT TABLES MAP PROPERTIES META TABLES


1. Create a table for each object in your object
model. For example, a blogging application
might have these tables: posts, users and
categories. Feel free to prefix your tables if you
2. Turn your object properties into your table
columns. For our POST object, we would then
have the columns: title, content, date, status,
etc. Also, include an ID field that auto-
3. Create a meta table for each object. Meta
tables let you attach “optional” data to objects.
Data that some instances may have while
others may not. This adds the initial flexibility
wish. E.g. wp_posts, wp_users, wp_categories. increments to create an index. to your application.

RELATIONSHIPS
4. Create a relationships table for each relationship between objects in your
application. Normally, the table will simply have two columns: object1_id and
object2_id. For example, indicating that a post belongs to a category would result
in a post_categories table with the columns: post_id and category_id. The IDs
then come from the auto-incrementing “ID” column in each object able.
Example Database Structure
PHP 101 – Database Design

POSTS POST_META 03 POST_CATEGORIES


01 02
Stores all property data about the POST Stores optional data about individual Stores the relationships between the
object. All required info. POST object instances. POST and CATEGORY objects.

04 USERS 05 USER_META 06 CATEGORIES


Stores all property data about the USER Stores optional data about individual Stores all property data about the
object. All required info. USER object instances. CATEGORY object. All required info.

07 CATEGORY_META
Stores optional data about individual
CATEGORY object instances.

You might also like