# Build It! Databases

Planning Your Database Tables

How Relational Databases Work

As a news site, you might want to keep a database of past stories for your readers. You could keep that information in a table that looks like this:


Table name: Articles

Headline Byline Date Body
Dog Bites Dave Barry Dec 12, 2005 Body of the story
Record Cold Travis Smith Dec 12, 2005 Body of the story
Mets Win Travis Smith Dec 12, 2005 Body of the story

This Articles table contains all the basic information you need. The “Headline,” “Byline” and “Body” fields are text fields, and the “Date” is, of course, a date field. Because of the way database programs read information, names for database fields should be a single word with no spaces. If you need to give a field a name with more than one word, like “Final Score,” either run the words together (“FinalScore”) or replace the space with an underscore (“Final_Score”).

Even though you want to keep a table small, you would want to add a field to the Articles table named “ArticleID” that assigns each article a unique identification number. There are two reasons for this:

  • You might want to have two articles on the same date with the same title and the same byline. For example, "Correction," "Web staff" and "June 1."
  • It's simpler to write a program that just looks for entry number 6 from the Articles table than it is to request an entry from Articles with the headline “Dog Bites,” the byline “Travis Smith” and the date “Dec 12, 2005.”

So with that change, you'd have a table like this:

ArticleID Headline Byline Date Body
101 Dog Bites Dave Barry Dec 12, 2005 Body of the story
102 Record Cold Travis Smith Dec 12, 2005 Body of the story
103 Mets Win Travis Smith Dec 12, 2005 Body of the story

As we’ve discussed, multiple small, linked tables are more efficient and easier to update than a single large table. So instead of storing the name "Travis Smith" hundreds of times, a proper relational database would have a second table with the possible list of authors. You'd then do something like this:

Table name: Bylines

AuthorID Name
1 Travis Smith
2 Edward R. Murrow
3 Dave Barry

Now, the Articles table could be changed to look like this:

ArticleID Headline Byline Date Body
101 Dog Bites 3 Dec 12, 2005 Body of the story
102 Record Cold 1 Dec 12, 2005 Body of the story
103 Mets Win 1 Dec 12, 2005 Body of the story

There are a number of advantages to this:

  • It avoids misspelling someone's byline.
  • A single number linked to the Bylines table replaces a chunk of text that would otherwise be repeated over and over. This is faster for both the computer and the person entering the data.
  • Changing a byline for every article in the database only requires changing one entry in the Bylines table.

Technically, this type of data linking is called a one-to-many relationship. Each byline (one) links to several articles (many). But (in this example) each article only links to one byline.

What if you want to add e-mail addresses for each writer? Simply add that information to the Bylines table:

AuthorID Name E-mail
1 Travis Smith nep@hopstudios.com
2 Edward R. Murrow ed@cbs.com
3 Dave Barry dave@davebarry.com

You don't need to change the Articles table at all, and yet now you can add the writer's e-mail address to every story on your site. That's the power of relational databases.

What if there's more than one byline on a story? Well, that gets complex, because the “Byline” field in the Articles table, as currently set up, can only handle one number. It's not a text field – we changed it to handle a single number only because that takes up less space and lets us easily sort the bylines in interesting ways.

So, to change it so that each article can have multiple bylines, don't store the byline in the article table. You need to create another table that will link the byilines to the articles. It would look something like this:

Table name: Articles_Bylines

ArticleID AuthorID
101 3
102 1
102 1
103 3

(This table needs no primary key because it's only used for linking, and each row would be unique because each author only writes each article once anyway.)

Now, this table can have several entries, linking the Bylines table to the Articles table. When your Web page displays an article from the database, it can now also grab all the bylines linked to that article.

Relational databases can get tricky fast. For example, you might have multiple tables that link the relationship between an article and:

  • Author(s).
  • Categories or keywords.
  • Location(s).
  • Related stories.
  • Photo(s).
  • Editing status (for workflow purposes).
  • Editing history.

You might also want to have several dates associated with each article: When it was first entered in your system, when it was originally published in another medium, when it was last revised, when it should appear on a website, or when it should come off the front page.

If you are involved in planning or reporting on a lot of events, you might want to have a table that lists the events themselves, a table for the venues in which they’re held, a table for participants to rate or comment on the events, a table for photos taken at specific events or a table for recommended ages.

For a restaurant database, you'd need a table of the restaurants with additional tables for cuisine, addresses, price categories, ratings, or online menus.

Things get complex quickly, as you can see. To read in more detail about planning tables, take a look at these articles:

If you're really interested, there's a book called “Philip and Alex's Guide to Web Publishing” that offers a platform-agnostic overview of the process. It’s not only well written, but also filled with beautiful photography. (ISBN 1558605347)

Now that you understand how databases work, let's look at some software options available to you.

> READ NEXT ARTICLE: Database Applications

 

recent comments

    Be the first to comment on this page. You must register before you can leave a comment.


post a comment

You must be registered (free) in order to post a comment.

 

More on This Topic

Evaluate This Page

How useful was this article?
(5 is extremely useful)

rate this a 1 rate this a 2 rate this a 3 rate this a 4 rate this a 5

Others have rated
this article: 2.1 / 5

discuss this topic

Post a comment

on Advertising Your Site

After you create a site that job ended. Instead, you realize that you have nothing and you must again start working. Sites are like living organisms grow and gain popularity over time depending on what you do for him. It’s like a child to be raised with great care. If you give too much food is overweight.
Work is continuing and uninterrupted if you want to get results.
Masini from vand miere naturala

on RSS Feeds

Buy or sell timeshare with Bay Tree Solutions; browse timeshare resales; timeshares for sale and rent by owner; Learn about buying or selling timeshare on the resale market and how to Sell My Timeshare!

on RSS Feeds

Thanks for the great article. I really enjoyed it.

STD Testing
STD Test

on Maintaining Ethics and Standards

The iPad helped facilitate the wedding of Aaron and Katie, as it made its rounds to the priest Blue Wedding Dress and then the bride and groom. Not only did the bride, Katie, read her vows to her soon-to-be husband, Aaron, from the iPad, While the couple’s wedding was held outdoors, a gazebo provided shade to help make reading easier; the iPad’s glass screen and the sunlight usually don’t make a good combination for readability.Offbeat Bride reports on creative wedding ideas; Aaron and Katie’s iPad-enabled nuptials Bodice Wedding Dresses certainly qualify.If you plan on using your iPad in your wedding, you might as well plan your wedding using an iPad. You can use 100 Wedding Tips and the Wedding Guests app, which aims to help you make a final Bolero Wedding Dress decision on who to invite to your wedding and who to cut from the list.

on Legal Issues for Online Publishers

Your article is extremely impressive. I never considered that it was feasible to accomplish something like that until after I looked over your post. You certainly gave a great perception on exactly how this whole process works. I will make sure to return for more advice. Thanks

College Term Papers