I’ve been messing around with XML and XSL transformations quite a bit lately, using my favorite scripting language, PHP, to create XML documents on-the-fly using data retrieved from a MySQL database. This is cool, because I can then use PHP 4.03’s new Sablotron extension to transform these XML documents into a format viewable within a PC, PDA or cellular phone WAP browser. Although discussing the details of how this is accomplished is certainly out of the scope of this article, a database structure used within one of my new XML-oriented applications is perfectly suited for the purposes of this article. The database that we’ll create stores information used within an online news application. Using a Web interface, a select group of administrators add and submit newsworthy items that will then be inserted into the database. This information can fall under one of a group of categories, the name of each category being predetermined by the site administrator. To make things really interesting, the news service offers news items in different languages, and therefore each news item must contain some information specifying which language it is written in. Note that in providing just this brief description, several questions already arise in terms of scaleability: 1. How many news items can be stored in the database? 2. How many administrators are able to add news items? 3. How many categories are there? 4. How many languages are there? The answer to these questions are simple: We don’t know. Therefore, it is in our best interests to build the most scaleable and manageable database table structure possible through proper database normalization. To begin, take note that four tables are needed to properly store the news data: news, administrators, categories, and languages. I’ll round out this project introduction with a synopsis of the columns used within each table found within the database. Table: news - Contains the information relative to each news item.
Table: administrators - Contains the information specific to each administrator.
Table: categories - Contains the categories under which each news item can belong.
Table: languages - Contains the languages under which each news item can be written in.
Note that each table contains a unique identification number (primary key). This makes it possible to reference one specific row with a simple query. Furthermore, take note that the news table contains several foreign keys, which relate to another table in which that foreign key is the primary key of the respective table. Perhaps most importantly, however, is the fact that the grouping of data into well-defined tables allows for the subsequent expansion and editing of administrators, languages, categories without the need to perform massive updates to existing data. In the next section, I’ll show how these tables are created using MySQL syntax. {mospagebreak title=Creating the news tables in the MySQL database} Since its public release in October 1996, MySQL (http://www.mysql.com) has enjoyed a rapid gain in popularity. This is not surprising, considering the speed and power it offers to its users. Recently released under the GNU General Public License (GPL), MySQL is poised to become the 800-LB. gorilla in the realm of Open Source databasing technologies. Let’s begin by creating the three least complex tables: languages, categories and administrators: mysql>create table languages ( ->language_id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT, ->name CHAR(20) NOT NULL, ->PRIMARY KEY (language_id) ); mysql>create table categories ( ->category_id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT, ->name CHAR(20) NOT NULL, ->PRIMARY KEY (category_id) ); mysql>create table administrators ( ->admin_id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT, ->name CHAR(20) NOT NULL, ->email CHAR(40) NOT NULL, ->url CHAR(50) NULL, ->PRIMARY KEY (admin_id) ); As you can see, several attributes accompany the column definitions. I’ll introduce each here: CHAR(N): A CHAR specifies a string of 1 or more characters. The maximum number of characters is specified by N. mysql>create table news ( ->news_id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT, ->category_id SMALLINT UNSIGNED NOT NULL REFERENCES categories, ->admin_id SMALLINT UNSIGNED NOT NULL REFERENCES administrators, ->language_id SMALLINT UNSIGNED NOT NULL REFERENCES languages, ->title char(100) NOT NULL, ->submit_time TIMESTAMP(12) NOT NULL, ->summary TINYTEXT NOT NULL, ->PRIMARY KEY (news_id) ); You’ve already been introduced to most of the column attributes used to create the news table. However, there are a few used within the news table creation worth noting: While you may find the MySQL developer’s logic questionable in their choice to not implement Foreign Key support, their decision is not without reason; Continuous verification of value integrity between the Primary Key and Foreign Key values would result in a potentially substantial performance degradation. Next section, I’ll demonstrate how database normalization can vastly improve performance when querying tables.
blog comments powered by Disqus |