|
-
Jan 1st, 2011, 06:50 PM
#1
Thread Starter
Addicted Member
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?
-
Jan 1st, 2011, 08:06 PM
#2
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
-
Jan 1st, 2011, 08:08 PM
#3
Thread Starter
Addicted Member
Re: Facing problem with primary keys
and how is it possible to make each value unique in the whole column?
-
Jan 2nd, 2011, 02:13 PM
#4
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
-
Jan 2nd, 2011, 02:47 PM
#5
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).
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|