Computer Science Canada Database (SQL) design tips |
Author: | wtd [ Wed Jun 22, 2005 10:56 pm ] | ||||||||||||||||
Post subject: | Database (SQL) design tips | ||||||||||||||||
This first entry is inspired by some wackiness recently encountered by the author. The following targets MySQL, which isn't exactly SQL standards compliant. Let's say we want a database which keeps track of various bits of information on a particular person. What kind of info?
So, let's look at how we'd organize this. This is a first try.
Now, there are a lot of things wrong with this, but let's start with the most basic. Every table should have some column that's guaranteed to be unique. A name is not necessarily guaranteed to be unique. Instead we generally use an auto-incrementing integer. we also tell the database that column will be the primary key for the table.
Now, onto the next mistake. We have only a single field for the name. What if we just want the last or first name? Well, we have to do some expensive processing to get them apart, and there's no guarantee there will be more than one name. Let's just have two columns instead.
Now, what else are we doing wrong? The phone number is a string with space for 14 character, which just accomodates something like:
But again, what if I want to rearrange that or get at parts of it independently? How about I store the components separately...
Oh, and I also want the option to not have any data for the phone number.
Now, that's a lot of data, and we have a problem. What if home_phone_exchange has a value, but home_phone_number is null? How about we separate those into another table?
Now we can easily have phone numbers as optional pieces of data, but without allowing incomplete information. We also avoid making our people table overly complicated. Of course, two get back to something that looks like our ideal initial table, we might use a select statement like:
|