Designing the Database Tables
Think of the basic components of a forum: topics and posts. A forumif properly used by its patronsshould have several topics, and each of those topics will have one or more posts submitted by users. Knowing that, you should realize that the posts are tied to the topics through a key field. This key forms the relationship between the two tables.
Think about the requirements for the topics themselves. You definitely need a field for the title, and subsequently you may want fields to hold the creation time and the identification of the user who created the topic. Similarly, think of the requirements for the posts: You'll want to store the text of the post, the time it was created, and the person creating it. Most importantly, you need that key to tie the post to the topic.
The following two table creation statements create these tables, called forum_topics and forum_posts:
mysql> create table forum_topics (
-> topic_id int not null primary key auto_increment,
-> topic_title varchar (150),
-> topic_create_time datetime, -> topic_owner varchar (150)
-> );
Query OK, 0 rows affected (0.03 sec)
mysql> create table forum_posts (
-> post_id int not null primary key auto_increment,
-> topic_id int not null,
-> post_text text,
-> post_create_time datetime,
-> post_owner varchar (150)
-> );
Query OK, 0 rows affected (0.00 sec)
By the Way
In this simple forum example, we will identify users by their email addresses and not require any sort of login sequence.
You should now have two empty tables, waiting for some input. In the next section, you'll create the input forms for adding a topic and a post.
|