How is the data stored?

<< Click to Display Table of Contents >>

Navigation:  Additional References > Database Primer >

How is the data stored?

Previous pageReturn to chapter overviewNext page

 

How the data is stored in a database is probably much simpler than you might think. Databases use a series of Tables to store the data. A table simply refers to a two dimensional representation of your data using columns and rows. For example:

 

 

LastName

FirstName

Address

City

Smith

John

24 West Ave

Los Angeles

Wayman

Laurie

16 Davidson St

Los Angeles

Johnson

Paul

1243 Georgetown Dr

Los Angeles

 

 

Each database table is given a unique name. The above example might be a table named Persons. Next, each column in the table is given a unique name. In our example above the column names are LastName, FirstName, Address, and City. Column names only has to be unique within a table, however, the same column name may be used in other tables.

 

The next thing to understand about your table is the Primary Key. The Primary Key simply refers to a column in your table that is guaranteed to be unique. The Primary Key is then used for the purposes of indexing your table which makes it much more efficient to search, sort, link, etc. So what is the Primary Key in our example? There is none. In our example, there is nothing that is going to be guaranteed unique. If we extend the table above to include a Primary Key, it might look like the following:

 

 

LastName

FirstName

Address

City

Smith

John

24 West Ave

Los Angeles

Wayman

Laurie

16 Davidson St

Los Angeles

Johnson

Paul

1243 Georgetown Dr

Los Angeles

 

 

To avoid the uncertainty of using a data column as a primary key, many developers will create their own column which contains a computer generated unique number, an ID number of sorts. This way you don't ever have to worry about its uniqueness since the database knows not to ever use the same number twice.

 

How many tables are used? That depends on how the data can be logically broken down. There is no limit to the number of tables, or columns for that matter, you can create. Keep in mind, though, that one huge table will be very inefficient while a bunch of little tables can be nearly impossible to keep straight. The best solution usually lies somewhere in the middle.

 

Here's an example. Let's say our Persons table stores contact information for a database of subscription magazines. Now we need to store what magazine(s) each person wants to subscribe to. We could simply add another column in our contact table that would store the name of the magazine. This would allow us to save the information we need but cause names and addresses to be duplicated, once for each different newsletter a person subscribes to. That would be highly inefficient.

 

What about making a second table for the names of the magazines? This way each magazine name and description would be stored only once.

 

 

LastName

FirstName

Address

City

Smith

John

24 West Ave

Los Angeles

Wayman

Laurie

16 Davidson St

Los Angeles

Johnson

Paul

1243 Georgetown Dr

Los Angeles