1. Trang chủ >
  2. Công Nghệ Thông Tin >
  3. Kỹ thuật lập trình >

Hour 14. Creating a Simple Discussion Forum

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (7.37 MB, 561 trang )


This document is created with the unregistered version of CHM2PDF Pilot



[ Team LiB ]



This document is created with the unregistered version of CHM2PDF Pilot



Types of Table Relationships

Table relationships come in several forms:



One-to-one relationships



One-to-many relationships



Many-to-many relationships



For example, suppose you have a table called employees that contains each person's Social

Security number, name, and the department in which he or she works. Suppose you also have

a separate table called departments, containing the list of all available departments, made up

of a Department ID and a name. In the employees table, the Department ID field matches an

ID found in the departments table. You can see this type of relationship in Figure 14.1. The

"PK" next to the field named stands for primary key, which you'll learn about during this hour.



Figure 14.1. The employees and departments tables are related through the DeptID.



In the following sections, you will take a closer look at each of the relationship types.



One-to-One Relationships

In a one-to-one relationship, a key appears only once in a related table. The employees and departments tables do



This document is created with the unregistered version of CHM2PDF Pilot



[ Team LiB ]



This document is created with the unregistered version of CHM2PDF Pilot



[ Team LiB ]



This document is created with the unregistered version of CHM2PDF Pilot



Understanding Normalization

Normalization is simply a set of rules that will ultimately make your life easier when you're acting as a database

administrator. It's the art of organizing your database in such a way that your tables are related where appropriate

and flexible for future growth.



The sets of rules used in normalization are called normal forms. If your database design follows the first set of rules,

it's considered in the first normal form. If the first three sets of rules of normalization are followed, your database is

said to be in the third normal form.



Throughout this hour, you'll learn about each rule in the first, second, and third normal forms and, we hope, will

follow them as you create your own applications. You'll be using a sample set of tables for a students and courses

database and taking it to the third normal form.



Problems with the Flat Table

Before launching into the first normal form, you have to start with something that needs to be fixed. In the case of a

database, it's the flat table. A flat table is like a spreadsheet—it has many, many columns. There are no relationships

between multiple tables; all the data you could possibly want is right there in that flat table. This scenario is inefficient

and consumes more physical space on your hard drive than a normalized database.



In your students and courses database, assume you have the following fields in your flat table:



StudentName— The name of the student.



CourseID1— The ID of the first course taken by the student.



CourseDescription1— The description of the first course taken by the student.



CourseInstructor1— The instructor of the first course taken by the student.



CourseID2— The ID of the second course taken by the student.



CourseDescription2— The description of the second course taken by the student.



CourseInstructor2— The instructor of the second course taken by the student.



This document is created with the unregistered version of CHM2PDF Pilot



[ Team LiB ]



This document is created with the unregistered version of CHM2PDF Pilot



[ Team LiB ]



This document is created with the unregistered version of CHM2PDF Pilot



Following the Design Process

The greatest problem in application design is a lack of forethought. As it applies to database-driven applications, the

design process must include a thorough evaluation of your database—what it should hold, how data relates to each

other, and most importantly, whether it is scalable.



The general steps in the design process are



Define the objective.



Design the data structures (tables, fields).



Discern relationships.



Define and implement business rules.



Create the application.



Creating the application is the last step—not the first! Many developers take an idea for an application, build it, and

then go back and try to make a set of database tables fit into it. This approach is completely backward, inefficient,

and will cost a lot of time and money.



Before you start any application design process, sit down and talk it out. If you can't describe your

application—including the objectives, audience, and target market—then you're not ready to build it, let alone model

the database.



After you can describe the actions and nuances of your application to other people and have it make sense to them,

you can start thinking about the tables you want to create. Start with big flat tables because, after you write them

down, your newfound normalization skills will take over. You will be able to find your redundancies and visualize

your relationships.



The next step is to do the normalization. Go from flat table to first normal form and so on up to the third normal form

if possible. Use paper, pencils, sticky notes, or whatever helps you to visualize the tables and relationships. There's

no shame in data modeling on sticky notes until you're ready to create the tables themselves. Plus, using them is a lot

cheaper than buying software to do it for you; software ranges from one hundred to several thousands of dollars!



After you have a preliminary data model, look at it from the application's point of view. Or look at it from the point of

view of the person using the application you're building. This is the point where you define business rules and see

whether your data model will break. An example of a business rule for an online registration application is, "Each user

must have one e-mail address, and it must not belong to any other user." If EmailAddress wasn't a unique field in your

data model, your model would be broken based on the business rule.



This document is created with the unregistered version of CHM2PDF Pilot



[ Team LiB ]



This document is created with the unregistered version of CHM2PDF Pilot



[ Team LiB ]



This document is created with the unregistered version of CHM2PDF Pilot



Creating a Discussion Forum

In the following sections, you'll learn the design process behind a simple discussion forum. This includes developing

the database tables, user input forms, and display of the results. When broken into pieces like this, such a task seems

quite simple—and it is!



Designing the Database Tables

Think of the basic components of a forum: topics and posts. A forum—if properly used by its patrons—should have

several topics, and each of those topics will have one or more posts 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 want 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)



In this forum example, we will identify users by their e-mail addresses and not require any

sort of login sequence. In the activity at the end of this hour, you'll be given some hints on

extending this forum example to fit within an environment of registered users.



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.



Creating the Input Forms and Scripts

Before you can add any posts, you must add a topic to the forum. It is common practice in forum creation to add the

topic and the first post in that topic at the same time. From a user's point of view, it doesn't make much sense to add

a topic and then go back, select the topic, and add a reply. You want the process to be as smooth as possible.



Xem Thêm
Tải bản đầy đủ (.pdf) (561 trang)

×