2 Replies - 338 Views - Last Post: 20 October 2009 - 05:07 AM Rate Topic: -----

#1 SIC_stu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 17-October 09

ENCOUNTERED ERROR : DUPLICATE KEYS

Post icon  Posted 19 October 2009 - 12:23 AM

Hai. I wanted to insert new Record to the same primary key, hence it says error: duplicate keys. I know that primary key cant be duplicate.

How am i going to handle this error?

For my problem : students will have different units and marks , but one Roll No.
In my result table : ROLLNO, ASSESSMENTID,UNIT,MARKS.
Is This A Good Question/Topic? 0
  • +

Replies To: ENCOUNTERED ERROR : DUPLICATE KEYS

#2 AdaHacker  Icon User is offline

  • Resident Curmudgeon

Reputation: 452
  • View blog
  • Posts: 811
  • Joined: 17-June 08

Re: ENCOUNTERED ERROR : DUPLICATE KEYS

Posted 19 October 2009 - 06:53 AM

View PostSIC_stu, on 19 Oct, 2009 - 01:23 AM, said:

How am i going to handle this error?

Umm...don't do that?

I really don't understand what you're asking. You know that you can't have duplicate primary keys, but you're trying to insert one anyway. Well, that's obviously not going to work. All I can say is that you either need to not do that or you need to rethink what you're using as your primary key.
Was This Post Helpful? 0
  • +
  • -

#3 jaql  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 73
  • Joined: 19-October 09

Re: ENCOUNTERED ERROR : DUPLICATE KEYS

Posted 20 October 2009 - 05:07 AM

View PostSIC_stu, on 18 Oct, 2009 - 11:23 PM, said:

Hai. I wanted to insert new Record to the same primary key, hence it says error: duplicate keys. I know that primary key cant be duplicate.


Well, you can't insert a record into a primary key because a primary key is a single attribute/field and a record is an entire row comprised of all of the fields in your table (I guess in your case ROLLNO, ASSESSMENTID, UNIT, and MARKS).

Do you mean you're trying to insert a new row with a ROLLNO value that already exists? Yeah, you won't be able to do that since primary keys are unique.

You can do...

UPDATE your_table SET ASSESSMENTID=value, UNIT=value, MARKS=value WHERE ROLLNO=value;

or

INSERT INTO your_table VALUES ('', value, value, value);

The insert statement above assumes your first field in your table is ROLLNO and that it is auto-incrementing. If you don't know what to do from here, I'd suggest picking up a basic SQL book.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1