Forum Topic: Php: Database Structuring 101

(1,513 views • 25 replies)

This topic is 1 page long.

<< < > >>
None

DFox

Reply To Post Reply & Quote

Posted at: 9/2/06 05:15 PM

DFox LIGHT LEVEL 30

Sign-Up: 08/09/03

Posts: 9,270

PHP: Main

PHP: Database Structuring 101

Introduction

As of late, I've been helping a number of people with their PHP inquiries. A lot of those people had MySQL questions.

As I talked with these people, it came very clear to me that most people don't know how to properly structure a database. They don't know about relational data, unique ids, things like that.

So, if you're new to MySQL, you should read this to learn how to property structure your database.

Unique IDs, what are the used for, why should I use them?

Unique IDs, know as the primary key in your database table, are one of the most important concepts you need to understand when using databases.

What a unique ID is, is a number that is unique for each row in the table of a database. So each row has a different number, and usually these numbers go in sequential order. MySQL has a feature called "auto_increment". What auto_increment does is cause the primary key, your unique IDs to increment one value every time a row is inserted into that table.

Why is it so important to use unique IDs? Well, let's use this example. I'm making a movie database. Two movies can easily have the same name. Let's say I'm not using a unique ID for my primary field, by table would look like this:

------------------------------
|movie_name|movie_description|
------------------------------
|Titanic|Titanic movie from 1953 staring Barbara Stanwyck.|
------------------------------------------
---------------
|Titanic|The gripping tale of the Titanic from 1997 staring Leonardo DiCaprio|
------------------------------------------
---------------

So, we have a problem. What if I want to select the Titanic movie from 1997 and show it's description? There's two movies named Titanic, and I have no way of telling them apart.

Now, how would this have looked if we had a primary, unique ID?

------------------------------
movie_id (auto_increment)(primary)(int)|movie_name|
movie_description|
------------------------------
1|Titanic|Titanic movie from 1953 staring Barbara Stanwyck.|
------------------------------------------
---------------
2|Titanic|The gripping tale of the Titanic from 1997 staring Leonardo DiCaprio|
------------------------------------------
---------------

Now each movie has it's own unique number assigned to it. If I wanted to pull the data on the awesome 1997 Titanic, all I have to do is select movie_name and movie_description WHERE movie_id = 2. It's as simple as that.

So, you may be asking, how do you make an auto increment unique ID primary field? It's simple using PHPMyAdmin.

Simply add a new field with these settings:
Field = any name you want for your unique ID
Type = INT
Extra = auto_increment
Primary = selected

And you're all set!

Relational data, what is it, and how do I use it effeciently?

The way you store data is EXTREMELY important. Storing data in an impractical way can cause major problems with large systems.

First, I want to go over relational data. Let's say you have a users table in your database. That table has fields like username, password, location, email address, stuff like that. It should also have a unique auto increment ID like: user_id.

OK, so let's say we have our users table set up like this:

------------------------------------------
----------
user_id|username|password|location|email|
------------------------------------------
----------
3|DFox2|loldonthackme|Syosset|david@phpmai
n.com
|
------------------------------------------
----------

OK, so that's my user record in the database. Let's say I'm making a portal, and users are going to be submitted tons of Flash content.

When a user submits a piece of content, I need to save that info in another table, called flashes. Here's the important part: IN flashes, I don't want to store the username of the user who submitted it. I want to store their USER ID as the creator, NOT username. Why? What if a user has submitted 100 flashes, and wants to change their username? I'm I going to change it in 100 rows? No, all I have to do is change the username in the USERS table, and it will take effect everywhere else as I have flash submitions tied to the user_id and NOT the username. User id will NEVER change.

So, my flashes table would look like this after I submit a flash:
------------------------------------------
----------
flash_id|user_id|flash_name|views|num_vote
s|
------------------------------------------
----------
1|3|Hop To The Top|324553|2100|
------------------------------------------
----------

It's as simple as that. That is relational data. The user_id within the flashes table relates to the users table as you can do a lookup of a user in the users table just by using the user_id in the flashes table. This is a very important concept, so please make sure you understand it.

Structuring your database properly

A few days ago, someone comes to me with a PHP question. They were making a feature on their site where people could make a favorites list in their account of their favorite flashes.

This is how they had their database structured:

------------------------------------------
----------
user_id|user_name|email|favorite_1|favorit
e_2|favorite_3|favorite_4|favorite_5|
------------------------------------------
----------

IT MADE ME WANT TO PICK MY DOG OFF THE FLOOR AND THROW HIM OUT THE WINDOW. Augh, it pissed me off. What if you want to have the user be able to select unlimited favorites???

I'm going to say this once. YOU DO NOT PUT REPEATING DATA IN COLUMS, YOU PUT IT IN ROWS. Soooo many people make this error. The proper way to do this is:
- Have a table called favorites
- Have it uses a relational field called user_id to tie it to a specific user
- Each favorite gets one row.

It would look like this with some data:
------------------------------------------
----------
favorite_id (auto_increment)|user_id|favorite_movie_id
------------------------------------------
----------
1|3|6
2|3|300
3|3|246
4|3|69
5|6|27
6|6|32

Then, if I want to select all of users 3 favorites, all I have to do is select from favorites WHERE user_id = 3

So, that's basically it. I hoped this helped you learn how to structure your databases.

Be sure to check out PHP: Main to learn how to actually use MySQL with PHP.


None

JeremysFilms

Reply To Post Reply & Quote

Posted at: 9/2/06 05:17 PM

JeremysFilms NEUTRAL LEVEL 17

Sign-Up: 02/18/05

Posts: 1,511

Great work, this'll DEFINITLY help out both people who don't want to have to explain about this, and for people just getting into database usage.


None

Rellizate

Reply To Post Reply & Quote

Posted at: 9/2/06 05:23 PM

Rellizate EVIL LEVEL 08

Sign-Up: 02/27/06

Posts: 481

Great tutorial. 8/10


None

DFox

Reply To Post Reply & Quote

Posted at: 9/2/06 05:25 PM

DFox LIGHT LEVEL 30

Sign-Up: 08/09/03

Posts: 9,270

At 9/2/06 05:23 PM, Rellizate wrote: Great tutorial. 8/10

Thanks!

Anything specific you'd like to see be added, because I'm always looking to improve for second one?


None

elbekko

Reply To Post Reply & Quote

Posted at: 9/2/06 05:27 PM

elbekko EVIL LEVEL 16

Sign-Up: 07/23/04

Posts: 6,450

This looks like a great tutorial :)

You might want to include how to use JOIN statements ;)

"My software never has bugs. It just develops random features. " - Unknown

[ PHP: Main | Omigod, a blog ]

BBS Signature

None

DFox

Reply To Post Reply & Quote

Posted at: 9/2/06 06:22 PM

DFox LIGHT LEVEL 30

Sign-Up: 08/09/03

Posts: 9,270

At 9/2/06 05:27 PM, elbekko wrote: This looks like a great tutorial :)

You might want to include how to use JOIN statements ;)

Thanks.

I wanted to kind of keep away from showing actual syntax as this is basically just a concept tutorial rather than syntax tutorials.


None

WoogieNoogie

Reply To Post Reply & Quote

Posted at: 9/2/06 07:13 PM

WoogieNoogie LIGHT LEVEL 14

Sign-Up: 06/26/05

Posts: 3,195

Great tutorial.


None

DFox

Reply To Post Reply & Quote

Posted at: 9/2/06 07:20 PM

DFox LIGHT LEVEL 30

Sign-Up: 08/09/03

Posts: 9,270

At 9/2/06 07:13 PM, WoogieNoogie wrote: Great tutorial.

Thanks bud :)


None

NinoGrounds

Reply To Post Reply & Quote

Posted at: 9/3/06 08:22 AM

NinoGrounds LIGHT LEVEL 17

Sign-Up: 11/28/05

Posts: 3,529

Nice.

I'd also refer to this page since they use tables so it is more organized.


None

DFox

Reply To Post Reply & Quote

Posted at: 9/3/06 12:49 PM

DFox LIGHT LEVEL 30

Sign-Up: 08/09/03

Posts: 9,270

At 9/3/06 08:22 AM, Nino_JoJ wrote: Nice.

I'd also refer to this page since they use tables so it is more organized.

Thanks, and thanks for the link :)


None

xcrime-cyber

Reply To Post Reply & Quote

Posted at: 9/4/06 01:37 PM

xcrime-cyber NEUTRAL LEVEL 17

Sign-Up: 06/10/03

Posts: 532

SELECT MAX(user_id) AS user_id From table

SELECT * FROM WHERE user_id = '1'


None

SpamBurger

Reply To Post Reply & Quote

Posted at: 9/4/06 01:42 PM

SpamBurger NEUTRAL LEVEL 15

Sign-Up: 07/12/05

Posts: 4,620

Nice tutorial :)

"However, the game received only two orders, one of which Molyneux speculated was from his mother." -Peter Molyneux's first game The Entrepreneur


None

elbekko

Reply To Post Reply & Quote

Posted at: 9/4/06 01:43 PM

elbekko EVIL LEVEL 16

Sign-Up: 07/23/04

Posts: 6,450

At 9/4/06 01:37 PM, xcrime-cyber wrote: SELECT MAX(user_id) AS user_id From table

SELECT * FROM WHERE user_id = '1'

Why are you posting random bits of code everywhere? =/

"My software never has bugs. It just develops random features. " - Unknown

[ PHP: Main | Omigod, a blog ]

BBS Signature

None

DFox

Reply To Post Reply & Quote

Posted at: 9/4/06 01:44 PM

DFox LIGHT LEVEL 30

Sign-Up: 08/09/03

Posts: 9,270

At 9/4/06 01:37 PM, xcrime-cyber wrote: SELECT MAX(user_id) AS user_id From table

SELECT * FROM WHERE user_id = '1'

What?

At 9/4/06 01:42 PM, -Kurt- wrote: Nice tutorial :)

Thanks!


None

xcrime-cyber

Reply To Post Reply & Quote

Posted at: 9/4/06 01:49 PM

xcrime-cyber NEUTRAL LEVEL 17

Sign-Up: 06/10/03

Posts: 532

At 9/4/06 01:43 PM, elbekko wrote:
At 9/4/06 01:37 PM, xcrime-cyber wrote: SELECT MAX(user_id) AS user_id From table

SELECT * FROM WHERE user_id = '1'
Why are you posting random bits of code everywhere? =/

juts for fun...free to write


None

DFox

Reply To Post Reply & Quote

Posted at: 9/4/06 01:51 PM

DFox LIGHT LEVEL 30

Sign-Up: 08/09/03

Posts: 9,270

At 9/4/06 01:49 PM, xcrime-cyber wrote:
At 9/4/06 01:43 PM, elbekko wrote: Why are you posting random bits of code everywhere? =/
juts for fun...free to write

OK well it's pretty annoying and is just going to confuse people. What you posted above wasn't even correct.

If you like writing code, then write a nice tutorial instead of spamming random threads with it, that's much more productive.


None

Mister-Mind

Reply To Post Reply & Quote

Posted at: 9/4/06 02:09 PM

Mister-Mind EVIL LEVEL 07

Sign-Up: 07/01/06

Posts: 2,212


None

xcrime-cyber

Reply To Post Reply & Quote

Posted at: 9/4/06 02:14 PM

xcrime-cyber NEUTRAL LEVEL 17

Sign-Up: 06/10/03

Posts: 532

Is SQL code is spamming ??


None

WoogieNoogie

Reply To Post Reply & Quote

Posted at: 9/4/06 02:18 PM

WoogieNoogie LIGHT LEVEL 14

Sign-Up: 06/26/05

Posts: 3,195

At 9/4/06 02:14 PM, xcrime-cyber wrote: Is SQL code is spamming ??

It's not spamming at all...especially in the Programming forum, but you didn't really explain what you typed and why you typed it.


None

eWhabs

Reply To Post Reply & Quote

Posted at: 9/4/06 02:20 PM

eWhabs DARK LEVEL 13

Sign-Up: 05/04/06

Posts: 987

Yeah, it's annoying. And if you we consider code as english, it would be spam, since this was useless and totally random.+

yo... yeah.

BBS Signature

None

WoogieNoogie

Reply To Post Reply & Quote

Posted at: 9/4/06 02:34 PM

WoogieNoogie LIGHT LEVEL 14

Sign-Up: 06/26/05

Posts: 3,195

At 9/4/06 02:20 PM, Whabs wrote: Yeah, it's annoying. And if you we consider code as english, it would be spam, since this was useless and totally random.+

Not really. I can see something that would have to do with this tutorial, but I don't know exactly what it does.


None

eWhabs

Reply To Post Reply & Quote

Posted at: 9/4/06 02:38 PM

eWhabs DARK LEVEL 13

Sign-Up: 05/04/06

Posts: 987

Well then he should explain what it does...

yo... yeah.

BBS Signature

None

elbekko

Reply To Post Reply & Quote

Posted at: 9/4/06 02:40 PM

elbekko EVIL LEVEL 16

Sign-Up: 07/23/04

Posts: 6,450

At 9/4/06 02:34 PM, WoogieNoogie wrote:
At 9/4/06 02:20 PM, Whabs wrote: Yeah, it's annoying. And if you we consider code as english, it would be spam, since this was useless and totally random.+
Not really. I can see something that would have to do with this tutorial, but I don't know exactly what it does.

It throws errors. That's all :P

"My software never has bugs. It just develops random features. " - Unknown

[ PHP: Main | Omigod, a blog ]

BBS Signature

None

Alphabit

Reply To Post Reply & Quote

Posted at: 2/2/07 08:35 AM

Alphabit NEUTRAL LEVEL 09

Sign-Up: 02/14/06

Posts: 3,581

It's funny, at school, the teacher was always like: "You always need to have a primary key in every table."
That's rubbish, most of the tables I've made don't need one... In fact most of them only have a single field... I guess it's not very important for small data transfers.

For a search engine though...


None

Zendra

Reply To Post Reply & Quote

Posted at: 2/2/07 09:04 AM

Zendra NEUTRAL LEVEL 38

Sign-Up: 09/07/03

Posts: 12,131

At 2/2/07 08:35 AM, LolOutLoud wrote: It's funny, at school, the teacher was always like: "You always need to have a primary key in every table."
That's rubbish, most of the tables I've made don't need one... In fact most of them only have a single field... I guess it's not very important for small data transfers.

For a search engine though...

Listen to your teacher. Since if you want to have multiple data stored in a table, you need to have a primary key.

NG Review & BBS Moderator // PM Review & BBS Abuse to someone else


None

henke37

Reply To Post Reply & Quote

Posted at: 2/2/07 09:22 AM

henke37 NEUTRAL LEVEL 16

Sign-Up: 09/10/04

Posts: 2,627

I want to add that a primary key does not need to be one automativcaly made up number. It can be the result of multiple fields.
If you got a bridging table where each row is allways related to a row in two other tables and it is exactly one row for each possible combination of the relations, you may not need a special id, you alredy got one.
Like for example a table over which topics in aforum each user have read. You got a topic id and an user id. No need for a special row id.

Move on to ActionScript 3! And please, drop the mysql PHP extension, it's so stale that it lacks features that is no longer considered new! Go mysqli or pdo instead.


All times are Eastern Standard Time (GMT -5) | Current Time: 09:32 AM

<< Back

This topic is 1 page long.

<< < > >>
You need a Grounds Gold Account to post on the NG BBS! If you don't have one, click here to sign up now! It's fast, free, and easy — and opens up tons of great NG features!