NYCPHP Meetup

NYPHP.org

[nycphp-talk] primary key alphanumerical

Tim Gales tgales at tgaconnect.com
Fri Dec 10 07:21:38 EST 2004


jaime bermudez writes

> Is there any way to make a Primary Key that is a
> combination of letters and numbers. I have an
> application form that collects teachers information,
> then every teacher should submit their school
> principal and their school supervisor, the problem is
> that each (PK) is just a consecutive number.
> Therefore, each application contains 3 diferent
> numbers like 001 (for the teacher) 002 (for the
> principal) and 003 (for the supervisor) this gets
> confusing... 

The short answer is yes.

(John Lacey already pointed out that you 
can create a segmented key.)

I would suggest that you do some reading on  
when to use a natural key versus when to use 
a surrogate key.

(Auto increment fields are, of course, generated 
surrogate keys) 
 
My feeling is that you should always look for 
a natural key first, before falling back on a 
surrogate. Generally speaking, a segmented key 
is needed when no field or column (i.e. natural 
attribute) is suitable by itself. 

But if you have to generate a key, I would say 
you should at least generate a (self-contained) 
unique key -- and not create a 'unique key if it 
is glued onto another column from the table' key.

Maybe you could just use a (universal) timestamp 
instead (as a surrogate key).

T. Gales & Associates
'Helping People Connect with Technology'

http://www.tgaconnect.com 

P.S.

It may be that: if you have more surrogate keys 
than natural keys in your database, you may have 
a bad design

Sort of like: if you have more guns than teeth, 
you may be a 'red neck' 





More information about the talk mailing list