177 Web Database Architecture Figure 7.7 To add reviews, we can either add a Reviews column to the Books table, or add a table specifically for reviews. Having many null values in your database is a bad idea. It wastes storage space and causes problems when working out totals and other functions on numerical columns. When a user sees a null in a table, they don’t know if it’s because this attribute is irrele- vant, whether there’s a mistake in the database, or whether the data just hasn’t been entered yet. You can generally avoid problems with many nulls by using an alternate design. In this case, we can use the second design proposed in Figure 7.7. Here, only books with a review are listed in the Book_Reviews table, along with their review. Note that this design is based on the idea of having a single in-house reviewer.We could just as easily let customers author reviews. If we wanted to do this, we could add the CustomerID to the Book_Reviews table. Summary of Table Types Yo u will usually find that your database design ends up consisting of two kinds of tables: n Simple tables that describe a real world object.These might also contain keys to other simple objects where there is a one-to-one or one-to-many relationship. For example, one customer might have many orders, but an order is placed by a single customer.Thus, we put a reference to the customer in the order. n Linking tables that describe a many-to-many relationship between two real objects such as the relationship between Orders and Books.These tables are often associat- ed with some kind of real-world transaction. Web Database Architecture Now that we’ve discussed the internal architecture of your database, we’ll look at the external architecture of a Web database system, and discuss the methodology for develop- ing a Web database system. ISBN BOOKS Author ISBN BOOK_REVIEWS Review Title Price 0-672-31687-8 Michael Morgan Java 2 for Professional Developers 34.99 0-672-31745-1 Thomas Down Installing Debian GNU/Linux 24.99 0-672-31509-2 Pruitt, et al. Teach Yourself GIMP in 24 Hours 24.99 Review 10 525x ch07 1/24/03 3:35 PM Page 177 178 Chapter 7 Designing Your Web Database Architecture The basic operation of a Web server is shown in Figure 7.8.This system consists of two objects: a Web browser and a Web server.A communication link is required between them. A Web browser makes a request of the server.The server sends back a response. This architecture suits a server delivering static pages well.The architecture that delivers a database backed Web site is a little more complex. Browser Response Request Web Server Figure 7.8 The client/server relationship between a Web browser and Web server requires communication. The Web database applications we will build in this book follow a general Web database structure that is shown in Figure 7.9. Most of this structure should already be familiar to you. Browser 6 1 Web Server 5 2 PHP Engine 4 3 MySQL Server Figure 7.9 The basic Web database architecture consists of the Web browser,Web server, scripting engine, and database server. A typical Web database transaction consists of the following stages, which are numbered in Figure 7.9.We will examine the stages in the context of the Book-O-Rama example. 1. A user’s Web browser issues an HTTP request for a particular Web page. For exam- ple, she might have requested a search for all the books at Book-O-Rama written by Laura Thomson, using an HTML form.The search results page is called results.php. 2. The Web server receives the request for results.php, retrieves the file, and passes it to the PHP engine for processing. 3. The PHP engine begins parsing the script. Inside the script is a command to con- nect to the database and execute a query (perform the search for books). PHP opens a connection to the MySQL server and sends on the appropriate query. 4. The MySQL server receives the database query and processes it, and sends the results—a list of books—back to the PHP engine. 10 525x ch07 1/24/03 3:35 PM Page 178 179 Next 5. The PHP engine finishes running the script, which will usually involve formatting the query results nicely in HTML. It then returns the resulting HTML to the Web server. 6. The Web server passes the HTML back to the browser, where the user can see the list of books she requested. The process is basically the same regardless of which scripting engine or database server you use. Often the Web server software, the PHP engine, and the database server all run on the same machine. However, it is also quite common for the database server to run on a different machine.You might do this for reasons of security, increased capacity, or load spreading. From a development perspective, this will be much the same to work with, but it might offer some significant advantages in performance. Further Reading In this chapter, we covered some guidelines for relational database design. If you want to delve into the theory behind relational databases, you can try reading books by some of the relational gurus like C.J. Date. Be warned, however, that the material can get pretty theoretical and might not be immediately relevant to a commercial Web developer.Your average Web database tends not to be that complicated. Next In the next chapter, we’ll start setting up your MySQL database. First you’ll learn how to set up a MySQL database for the Web, how to query it, and then how to query it from PHP. 10 525x ch07 1/24/03 3:35 PM Page 179 10 525x ch07 1/24/03 3:35 PM Page 180 8 Creating Your Web Database IN THIS CHAPTER WE ’LL TALK ABOUT how to set up a MySQL database for use on a Web site. We’ll cover n Creating a database n Users and Privileges n Introduction to the privilege system n Creating database tables n Column types in MySQL In this chapter, we’ll follow through with the Book-O-Rama online bookstore applica- tion discussed in the last chapter. As a reminder, here is the schema for the Book-O- Rama application: Customers(CustomerID, Name,Address, City) Orders(OrderID, CustomerID, Amount, Date) Books(ISBN, Author,Title, Price) Order_Items(OrderID, ISBN, Quantity) Book_Reviews(ISBN, Reviews) Remember that primary keys are underlined and foreign keys have a dotted under- line. In order to use the material in this section, you must have access to MySQL.This usually means that you n Have completed the basic install of MySQL on your Web server.This includes n Installing the files n Setting up a user for MySQL to run as 11 525x ch08 1/24/03 3:39 PM Page 181 . you. Browser 6 1 Web Server 5 2 PHP Engine 4 3 MySQL Server Figure 7.9 The basic Web database architecture consists of the Web browser ,Web server, scripting engine, and database server. A typical Web database. architecture of a Web database system, and discuss the methodology for develop- ing a Web database system. ISBN BOOKS Author ISBN BOOK_REVIEWS Review Title Price 0-6 7 2-3 168 7-8 Michael Morgan. context of the Book-O-Rama example. 1. A user’s Web browser issues an HTTP request for a particular Web page. For exam- ple, she might have requested a search for all the books at Book-O-Rama written by