Why use an auto-incrementing primary key when other unique fields exist?

前端 未结 12 2190
悲&欢浪女
悲&欢浪女 2021-01-30 10:36

I\'m taking a course called \"database systems\" and for our class project I have to design a website.

Here\'s an example of a table I created:

CREATE TA         


        
相关标签:
12条回答
  • 2021-01-30 11:08

    I'll need someone with more database knowledge to back me up on this one, but i believe you get a faster response in foreign key lookup time.

    Additionally, you may decide later that you want usernames to change, or that the requirements for usernames may change (maybe a longer string?). Using an ID prevents having to change all foreign keys.

    Lets face it, most projects aren't going to expand that much, but do you really want to risk the headache 12 months down the road, when you could conform to good programming standards now?

    0 讨论(0)
  • 2021-01-30 11:11

    Auto-incrementing primary keys are useful for several reasons:

    • They allow duplicate user names as on Stack Overflow
    • They allow the user name (or email address, if that's used to login) to be changed (easily)
    • Selects, joins and inserts are faster than varchar primary keys as its much faster to maintain a numeric index
    • As you mentioned, validation becomes very simple: if ((int)$id > 0) { ... }
    • Sanitation of input is trivial: $id = (int)$_GET['id']
    • There is far less overhead as foreign keys don't have to duplicate potentially large string values

    I would say trying to use any piece of string information as a unique identifier for a record is a bad idea when an auto-incrementing numeric key is so readily available.

    Systems with unique user names are fine for very small numbers of users, but the Internet has rendered them fundamentally broken. When you consider the sheer number of people named "john" that might have to interact with a website, it's ridiculous to require each of them to use a unique display name. It leads to the awful system we see so frequently with random digits and letters decorating a username.

    However, even in a system where you enforced unique usernames, it's still a poor choice for a primary key. Imagine a user with 500 posts: The foreign key in the posts table is going to contain the username, duplicated 500 times. The overhead is prohibitive even before you consider that somebody might eventually need to change their username.

    0 讨论(0)
  • 2021-01-30 11:15

    Your professor is doing the right thing by pointing out that you should have made username unique and not nullable if it was a requirement that user names should be unique. The uid could be a key as well but unless you are actually using it somewhere then it isn't needed. The more important aspect of the design ought to be to implement the natural key. So I agree with your professor's comment.

    0 讨论(0)
  • 2021-01-30 11:19

    If the username is the primary key and a user changes his/her username, you will need to update all the tables which have foreign key references to the users table.

    0 讨论(0)
  • 2021-01-30 11:21

    Because userid is supposed to be unique (cannot be duplicated) & sometimes is index.

    0 讨论(0)
  • 2021-01-30 11:22

    And do you want to store your usernames in clear text for any one to steal? I would never consider using a natural key that I might want to encrypt someday (or want to encrypt now).

    0 讨论(0)
提交回复
热议问题