Results 1 to 5 of 5

Thread: Facing problem with primary keys

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Oct 2010
    Posts
    195

    Facing problem with primary keys

    Using php i created a database called "my_db". Then i added a table called "users" kai 3 columns "username", "password", "email_address". The columns "username" and "email_address" are the primary keys. So they can accept only unique records. But this is what is happening...

    username : george
    email : [email protected]

    (this combination is accepted - it's also the first row of records so there is no problem. lets continue adding one more).

    username : george
    email : [email protected]

    (Duplicate entry '[email protected]' for key 1. Ok let's try another one)

    username : george
    email : george2@hotmail.com

    (and it accepts it. so i have two users with exactly the same name. I also get the same result if i add a different name and a mail that already exists)

    any ideas?

  2. #2
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: Facing problem with primary keys

    The key is a combination of two columns according to what you said. that means as long as 1 of the two are different the data is different.

    Gary [email protected] is not the same as Gary [email protected] the two fields for the email are different so the records are different.
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Oct 2010
    Posts
    195

    Re: Facing problem with primary keys

    and how is it possible to make each value unique in the whole column?

  4. #4
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: Facing problem with primary keys

    What is the database? All of them allow for multiple columns to be used for an index , that is basicly what a PK is a Unique index on the columns.
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  5. #5
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Facing problem with primary keys

    it's customary to use a numeric primary key for users (and other things like that) -- this provides an easy way to reference the user throughout the database with foreign keys. your username and email fields should both just be unique indexes in your table.

    I'm going to assume you're using MySQL, and that you're managing your database in phpMyAdmin: if this is the case, you can drop your current primary keys on the structure tab of your table, then create a new column (userId) which is the primary key, as an integer with auto_increment. then, you can create a new unique index for the username and email (again on the structure tab of your table).
    Like Archer? Check out some Sterling Archer quotes.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width