My blog has moved!

You should be automatically redirected in 4 seconds. If not, visit
http://alexkaminski.org
and update your bookmarks.

Wednesday, March 26, 2008

Quick Overview of Database Design for the Web

So you're just getting started building your website and need features that require a database (such as user login and profiles). If you've never used a database before, this might seem daunting and complex. Well, it's not, it just takes practice and basic programming skills.

What's a Database and why do I need one?

Before we go further, let's explain what a database does and why its necessary. Essentially, a database is used to store information. The kind of information that can be stored varies from user comments on a blog post to credit card details of an online purchase. If you want to create a website that saves or accesses information in anyway, you will need to use some type of database. 

What does it look like?

A database is composed of tables. These tables have rows that store different information in each column. So a database will look something like this:
- Database
- Table users
- Table comments

Now, let's see how these tables would look like:



You'll notice that we don't have one table, but instead split the information among two tables: users and comments. 

So why can't I just have all my information in one table?

Most programmers do exactly that when they design their first table in a database. Let's look at a design like that:


Notice that in the above table design the user John Smith had two comments. In order to create the second comment, we had to create another row repeating his user_id, name, and email information. This type of design is crude and inefficient. Imagine if the user decided to change his email and you need to update your database. In this type of one-table design you would have to iterate through each row that belongs to the user and update their email. Seems inefficient right?

So what do we do? We split the information up into two tables (as shown in the images above). This reduces any data redundancy and makes the whole database design more efficient. 

How to scale your database

One of the best ways to scale your database is to split your data up among various tables. This allows you to split your tables up and place them in other databases. These databases can be located on other database servers, thus reducing the strain of having one database server run all the requests. 

What do I do next?

Next, you should decide on a type of database. There are free ones available such as MySql or more robust but expensive ones such as Microsoft Sql Server. You'll need to learn the SQL programming language, which is how you interact with the database and create, edit information. I'm not going to try and teach you the SQL language, but I'll direct you to a website that has great tutorials on this subject. 

blog comments powered by Disqus