What will happen if user insert data with same primary key?
Will it thhrow error ?
how can i solve this?
insering data with existing primary key
Moderator: General Moderators
insering data with existing primary key
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.
Reason: You only need to post the topic in 1 forum, not 3.
Re: insering data with existing primary key
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.)
(Note: I can only vouch for MySQL and Oracle as they are the only database systems I am familiar with.)
Re: insering data with existing primary key
Users shouldn't be choosing primary keys.
Re: insering data with existing primary key
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.onion2k wrote:Users shouldn't be choosing primary keys.
Re: insering data with existing primary key
And would break everything if the user wanted to change their username.AlanG wrote: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.onion2k wrote:Users shouldn't be choosing primary keys.
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.
Re: insering data with existing primary key
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.onion2k wrote:And would break everything if the user wanted to change their username.
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.