Not a problem, Chris...always happy to help 🙂
In your example, my opinion is that it would be best to use the id column as the primary key and define it when you create your table like this:
id INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY (id)
and make dna a unique field and index by it:
dna INT,
UNIQUE dna (dna ),
INDEX table_dna (dna)
Of course, you'd replace "table" with your table name...
so, let's say you want to create a table called DNAdata, you could do it like this:
CREATE TABLE DNAdata (
id INT NOT NULL AUTO_INCREMENT,
name varchar(20),
city varchar(20),
state varchar(2),
dna INT,
PRIMARY KEY (id),
UNIQUE dna (dna),
INDEX DNAdata_dna (dna)
);
This table would then accept up to 20 characters for each of name and city and 2 characters for state.
Your primary key would be id and dna is indexed and unique.
From my SAMS book, here are the constraints on primary keys:
1) No two rows may have the same primary key value
2) Every row much have a not-null primary key value
3) The column containing primary key values can never
be modified or updated
4) Primary key values can be be reused (If a row is
deleted from the table, its primary key must
not be assinged to any new rows)
Item #1 says that primary keys must be unique
Item #2 says that a value must be assigned to the primary key, which is why auto_increment is so nice 🙂
Item #3 is the reason why data should not be used as
the primary key since you cannot update that column... which is another reason why auto_increment is so nice... the value is strictly arbitrary and has no relavence to the rest of the data in the row. If you used dna as the primary key, and a typo was made while inserting the data for that person, you could not go in and change it again. You would have to delete and re-insert the data instead of updating the existing row.
Item #4 means that, instead of not taking the time to think your updates through and skimping on code to delete and re-insert data, you should take the time to think things through, and write good code which knows the difference between an insert and an update...at least that is what it means to me 🙂
I'm sorry if I'm being too long winded, but database design determines how easily you can code the rest of your application and how efficient your database and PHP code will be. Do not make things harder than they have to be, but don't cut corners.
A improperly designed database will cause you many hours of hair pulling and probably quite a few thoughts of "Why didn't I do it another way from the start?"
If you take the time to thoroughly think every through when designing your database, you will not have to go through your code making changes every time you make a change to the database to fix something you should have made allowances for from the beginning. Sometimes database changes can't be avoided.... unforeseeable problems do arise, but proper database design will minimize these and maximize your productivity.
Again, sorry for being soooooo long winded...but I've taught myself many of these lessons the hard way 🙂
-Rich