insering data with existing primary key

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
delhiris
Forum Commoner
Posts: 33
Joined: Sun Sep 06, 2009 3:57 pm

insering data with existing primary key

Post by delhiris »

What will happen if user insert data with same primary key?
Will it thhrow error ?
how can i solve this?
Last edited by onion2k on Tue Sep 08, 2009 8:51 am, edited 1 time in total.
Reason: You only need to post the topic in 1 forum, not 3.
AlanG
Forum Contributor
Posts: 136
Joined: Wed Jun 10, 2009 1:03 am

Re: insering data with existing primary key

Post by AlanG »

It will return an error. You solve it by assigning a new unique primary key to the new data. A primary key is a unique index used to identify a row. It CANNOT be duplicated and it cannot be set to NULL, and there is no way around this.

(Note: I can only vouch for MySQL and Oracle as they are the only database systems I am familiar with.)
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: insering data with existing primary key

Post by onion2k »

Users shouldn't be choosing primary keys.
AlanG
Forum Contributor
Posts: 136
Joined: Wed Jun 10, 2009 1:03 am

Re: insering data with existing primary key

Post by AlanG »

onion2k wrote:Users shouldn't be choosing primary keys.
Assuming your using an id, no harm in using a string as a primary key. For example, what about a username for a users table. The username would work perfectly as a primary key, and would remove the overhead of having an id.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: insering data with existing primary key

Post by onion2k »

AlanG wrote:
onion2k wrote:Users shouldn't be choosing primary keys.
Assuming your using an id, no harm in using a string as a primary key. For example, what about a username for a users table. The username would work perfectly as a primary key, and would remove the overhead of having an id.
And would break everything if the user wanted to change their username. :)

I understand what you mean though. Natural language primary keys are a good idea in some, limited circumstances. They should be restricted to cases where they won't change though (ISO language codes for example). You can't guarantee that to the case if the user is choosing something.
AlanG
Forum Contributor
Posts: 136
Joined: Wed Jun 10, 2009 1:03 am

Re: insering data with existing primary key

Post by AlanG »

onion2k wrote:And would break everything if the user wanted to change their username. :)
Hmm would it? If it did I would put that down to bad development or database design. Personally I use an id all the time. It makes it easier when passing it around such as in urls and such, no need to worry about url encoding it. :) I was just making a point though. :)

Actually, just for my own curiosity, can you give me one example where an id change would "break" the system? Other than hard coding id's into the code, which is just plain silly.
Post Reply