Digital Web Magazine

The web professional's online magazine of choice.

Introduction to Databases

Got something to say?

Share your comments on this topic with other web professionals

In: Articles

By Paul Tero

Published on October 13, 2004

Once upon a time, the mark of a savvy company was having a Web site. Sites had lots of pages and if something needed to be changed, you'd call someone who would promise to look into it right away.

Then, a couple years ago, your competitors started bragging that their Web sites were dynamic and used databases. This didn’t bother or affect you until your favorite IT guy moved to the Caribbean and was replaced by an automated answering system.

Now that you have a budget and a lot of changes to make, you want to know how a database-driven Web site can help keep costs down and save time. We’ll start at the beginning.

What is a database?

A database is simply a bunch of information (data) stored on a computer. This could be a list of all your clients, a list of the products you sell, the results of a chess tournament or everyone in your family tree.

The most common type of database is a relational database. Relational databases consist of tables of data with clearly defined columns.

If you run a design agency, somewhere on your computer you probably have a spreadsheet with all your clients’ names and addresses. Along the top of the spreadsheet are the column headings: Name, Address, Telephone Number, Email Address, etc. Down the left side you might have customer identification numbers, and in the middle all the data. This is the same thing as a table in a relational database. It’s your “clients” table.

And tucked away in your filing cabinet, you might have a separate folder for every project you’ve ever worked on. This paper contains the project number, the name (or ID number) of the client, a description of the project, the current status, the budget and a completion date. In database terms, this is your “projects” table.

If one day a couple months from now you realize that a check for project number 3068 bounced, you can dig out your “projects” folder and find out the name of the client. Then you can use this name to look up the client’s telephone number in your “clients” file.

In this way, your “projects” file is related to your “clients” table, and you essentially have the beginnings of a relational database. Next, you’ll need to migrate your data to some sort of database software.

How do I use database software?

Another popular answer to the question “What is a database?” could be “A piece of software that you use to store your data.” This is because the word “database” can refer to both the software and the actual data.

The above section gave more of a dictionary definition of a database. This section talks about the actual software, which makes storing all that data much easier.

Database software is purchased and installed just like any other type of software. Once installed, you can create a database and then start setting it up.

First you create the tables, specifying the name of each column and the type of data it will contain (a number, a piece of text or a date).

Then you relate bits of data in one table (like your client identification number in your “clients” table) to bits of data in another table (the client identification number in your “projects” table).

Finally, you type in or import all that valuable data. Now you can start searching through it to extract all sorts of information.

For instance, it would be pretty time consuming to find clients who have spent more than $1,000 on projects with you if you had to rummage through the filing cabinet adding things up, but it would be almost instantaneous with database software.

Who makes this database software?

There are a lot of database software manufacturers out there and a wide range of prices, sizes, speeds and functionalities.

At the lower end of the scale are personal database software products like Microsoft Access, which is designed to be used by individuals or small companies relatively little data. User friendliness and ease of use are the priority rather than speed and scalability (in other words, it works well when you have 100 projects but not when you have 100,000).

At the higher end are full-fledged enterprise solutions, such as Oracle Enterprise Edition. These database software products can handle millions of data entries and are fast and efficient. They have many optimization and performance tools and generally require a database administrator (DBA) to look after them. Products in this range can also be very expensive.

In the middle are products like Microsoft SQL Server, which is a logical upgrade from Microsoft Access for Windows users.

There are also several very good free database software products, such as MySQL and PostgreSQL. These are lacking on the user interface side, but can certainly compete on speed and scalability.

What is SQL?

You probably noticed that many of these products include the acronym SQL (often pronounced “sequel”). This is a term which arises very often when discussing databases. It stands for Structured Query Language and is a language that all relational databases understand. A “database query” is something that a user asks the database, and SQL is the language that query is written in.

You don’t actually need to know much about SQL to operate a database (especially when using something like Microsoft Access) because it hides the SQL behind menus and toolbars and nice little pop-ups. However, SQL is a fairly simple language, so this section provides a brief introduction.

This is how you would tell an SQL-friendly database to create your “clients” table:

CREATE TABLE clients ( idnumber INT PRIMARY KEY, name TEXT, city TEXT, telephone TEXT );

This creates a table with five columns. The idnumber column is the table’s PRIMARY KEY, which means that it has to be unique. In other words, this database will not let you assign two clients the same ID number. INT means that the idnumber is an integer (a number) and TEXT means that the name column contains text (you’d probably use something else in a real situation as TEXT is relatively inefficient).

You can then use an SQL command like this to insert data into your new table:

INSERT INTO clients VALUES (100, 'Smiths Records', 'London', '020 8888 2222');

This adds a client with a client ID number of 100 and some contact details. Once you have added plenty of data to your table, you can run searches using SQL commands like this:

SELECT idnumber, name, city FROM clients WHERE city='London';

This will retrieve all clients located in London.

As you can see, SQL is a straightforward language and simple queries like the ones above are easy to understand. However, the real power of databases comes when you form more complex queries.

To show all clients who have spent over $1,000 on projects, you would use a query like:

SELECT name, SUM(budget) AS total FROM clients INNER JOIN projects ON clients.idnumber=projects.clientidnumber GROUP BY clientidnumber HAVING total > 1000;

It’s a slightly more complex query than the previous examples, but still easy to understand.

What is a database-driven Web site?

Now that you know the database basics (what they are, how to use them, who makes them and how to query them) it’s time to return to the initial problem—how to keep up with your competitors and their fancy new Web sites.

Individual Web pages on any Web site are either static or dynamic. A static Web page is one that can only be changed by logging into the server and replacing the Web page with a new version.

A dynamic Web page is one that knows how to read its information from a database. For example, your Web site might display a list of all your the clients from your clients table. When you change the data in your database to reflect that Smith Records has just moved to Manchester, this change is automatically reflected on your Web site. The page is no longer static because it dynamically displays the contents of your database.

Since the data in your database drives the content on your Web pages, it is considered “database driven.”

How can my Web site read from my database?

This is the hard part. It is easy enough to have a Web site, and databases aren’t too difficult either, but how on earth do you get the former to talk to the latter?

The answer is twofold.

First, your Web site and your database will probably need to be situated on the same computer, or at least in the same local network. This may seem inconvenient because your Web site is probably on a computer in a big warehouse in a city hundreds of miles away.

However, your Web server may already have some database software installed (usually SQL Server on Windows servers and MySQL on Linux servers), or you can ask your host to install it for you. Once the software is up and running, there are plenty of tools to allow you to administer the database remotely.

Second, you need some glue to connect your Web site to your database. For this you will need more software and probably a programmer.

Again, the software is probably already on your server. On a Windows server it is usually .NET, and PHP on a Linux server (there are alternatives, such as Java Beans, Java Struts and ColdFusion). These bits of software all know how to communicate with databases and turn the results into dynamic Web pages.

Then you just need to call up your favorite IT guy and say “Make it work!” He’ll write various scripts which refer to the database and output HTML.

Here is an example of such a script in PHP:

$connection = mysql_connect ("localhost:3066", "paul", "8asdfk3");
$query = mysql_db_query ("test", "SELECT * FROM clients", $connection);
while ($row = mysql_fetch_array ($query)) {
print $row["name"] . " is located in " . $row["city"] . "<br />";
}

First, the script connects to a MySQL database (specifying the server location, port, user name and password). Then it runs a query using that connection (passing in the database, the query on the clients table, and the $connection variable). Then it loops through each of the results and prints out the clients’ names and cities.

This is one of the most expensive parts of the process, but once it is working you will be able to make all changes on your Web site very quickly.

Conclusion

Hopefully this article has served to demystify databases and how to start using them.

You now know that a database is just a handy and structured place to put all your data. You know what database software is for, who makes it and why there can be such huge price differences.

Your grasp of SQL is tenuous, but at least you know what it stands for.

And finally, the phrase “database-driven Web site” no longer scares you, because it’s just about people putting stuff in a database and having it automatically appear on a Web site.

And when you have commissioned and received your brand new, database-driven, fully dynamic Web site, you can feel very proud that you are keeping up with your competitors. You can also be happy that you’ll still have a reason to call your IT guy, who is working for you again from his cottage in Barbados.

Got something to say?

Share your comments  with other professionals (14 comments)

Related Topics: Technology, Databases, Basics

 

Paul Tero is a web programmer based in Brighton, England. After graduating from UC Berkeley, he worked for several years programming content management systems for DOT new media. He now works in a corner of his living room as a freelance programmer.

Media Temple

via Ad Packs