reageer@yahoo.com wrote:[color=blue]
> Hi all,
>
> I have a design question:
>
> I have a bunch of users (name, address, zip, etc.). They are assigned a
> card with a specific id.
> The only thing unique is this card id, or probably the combination of
> all other user fields.
> So it's seductive to use the card id as the primary key.
> This card allows access to certain places and all access is logged.[/color]
If more than one user can live in a household you can't use the
card id as the primary key for the user since you state that the
card is issued to a household and not to a user.
[color=blue]
>
> The problem is, a user might lose a card and will be issued a new one
> with a different id. Perhaps it may even be desirable to assign
> mutliple cards with different ids to one person, but let's put that
> scenario aside for a moment.
>[/color]
A second reason why card id cannot be used as the primary key.
[color=blue]
> Issuing a new card to a user could mean updating the card id with a new
> one, e.g.:
> UPDATE Users SET CardId=124 WHERE CardId=123
>
> But same would have to be done with all 'logs' that refer to the lost
> card id.
> And some kind of logging of this change needs to occur to maintain some
> 'history'.
> Consistency could be handled with triggers or something..
> Doesn't seem like a good solution.[/color]
You can't avoid the problem of a change in primary key values
when dealing with people because names are not permanent. You
have a situation in which there aren't any truly good solutions,
but if you keep your logs in the database and use triggers or
separate procedures to update the logs as well as the active
tables maintenance shouldn't be a significant problem.
[color=blue]
>
> It seems obvious that I should seperate user from card id, where card
> id is no longer primary key, rather just a field within the users
> table, or if multiple cards can be assigned to the same user, a
> seperate table for cards mapped to user records.
>
> So then how do I uniquely identify a user? It seems easy enough to
> simply use an IDENTITY column for this, however, I do agree with
> 'CELKO's' (sp?) principle that best design is one where data in the
> database actually represents something in the real world. In particular
> when backing up, reinserting and other things could goof up
> (re-generate) these id's.[/color]
Why not use area code, phone number, first name, Suffix (Jr.,
Sr., etc). Phone numbers and first names tend to be stable and
the suffix is sufficient to discriminate between members of a
household with the same first name.[color=blue]
>
> And there's my dilemma. How do I uniquely identify each user record
> consisting of just name, address, zip, city and perhaps phone number?
> There are no other id's that represent the user, such as social
> security number or whatever.
>
> Actually, the card is going to be assigned to a household (home
> address), so the combination of house number, street and zip is
> probably unique but using this as a primary key would make all queries
> quite cumbersome, compared to some id.
>[/color]
Beware of using addresses, or any multiple word value, as a key
since you can't predict how the users will enter data. For
example "1st Street" or "First Street" or "1st ST" or "First ST"
[color=blue]
> Is this a case where an identity (aka auto_increment) is inevitable or
> even highly recommended?[/color]
auto_increment should only be used as a last resort because
there is no way to relate the auto_increment value to the data
to assure that the correct data is being operated on. When you
use values like phone numbers and names you can sight verify
that you are operating on the correct data.[color=blue]
>
> Another alternative I thought of was to use the card id as primary key,
> but have the same user record appear in the users table multiple times,
> but with a different card id, where each card id might actally be
> 'blocked' or not. This blocked cards can then be deleted once the
> records in other tables that refer to that id are also deleted, such as
> logs. Sounds ugly as this introduces some redundancy of user records,
> but might actually be acceptable I think, because logs are deleted
> every now and then and lost cards will be rare. Not sure if I should
> design the database with the 'rule' or 'exception' in mind.
>[/color]
You are right. This is ugly. Also by introducing the concept
of redundant data you are subverting one of the primary
strengths of a relational data base management system. Don't do it.
[color=blue]
> Yes, I'm a rookie.. and I don't mind being chewed out for it, as long
> as the lecture includes something useful I can work with and learn from
> :)
>
> Lisa
>[/color]