Solved

add MySQL primary key

Posted on 2015-02-09
7
181 Views
Last Modified: 2015-02-11
dear all,

right now have structure like this of a table :

CREATE TABLE `tblInventory` (
  `ID` int(11) NOT NULL DEFAULT '0',
  `Parent` int(11) DEFAULT NULL,
  `SNMPName` varchar(30) DEFAULT NULL,
  `Make` varchar(100) DEFAULT NULL,
  `Model` varchar(100) DEFAULT NULL,
  `Part` varchar(30) DEFAULT NULL,
  `Serial` varchar(20) DEFAULT NULL,
  `VendorCircuitID` varchar(100) DEFAULT NULL,
  `VendorID` int(11) DEFAULT NULL,
  `DataRateSDH` char(1) NOT NULL DEFAULT '0',
  `DataRateType` varchar(10) DEFAULT NULL,
  `DataRate` decimal(11,3) DEFAULT NULL,
  `DataRateUnit` char(4) DEFAULT NULL,
  `DataRateVC12` bigint(20) unsigned DEFAULT '0',
  `RestorationUse` tinyint(1) DEFAULT '0',
  `UnderInstallation` tinyint(1) DEFAULT '0',
  `TriggerAction_Audit` varchar(10) DEFAULT NULL,
  `ActionDone_Audit` char(10) DEFAULT NULL,
  `ActionTime_Audit` datetime DEFAULT NULL,
  `ActionByHost_Audit` varchar(50) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Open in new window


and now we want to add one more column to this talbe:

RecordVersion_Audit BIGINT NOT NULL AUTO_INCREMENT; 

Open in new window


however, we need to define that column as a key, but when I do this:

ALTER TABLE xxx.yyy ADD RecordVersion_Audit BIGINT NOT NULL primary key;

it report:

Lookup Error - MySQL Database Error: Duplicate entry '0' for key 'PRIMARY'

Open in new window


any way to fix this ?
0
Comment
Question by:marrowyung
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
7 Comments
 
LVL 1

Author Comment

by:marrowyung
ID: 40597883
can I just do this :

ALTER TABLE xxx.yyy ADD RecordVersion_Audit BIGINT NOT NULL AUTO_INCREMENT UNIQUE;  

Open in new window

?
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 40597899
the error tells you that you already HAVE several records with the value of "0", adding the field with auto_increment will not populate the field as if you had inserted the records after having that field set;

to solve this, you have several ways, the most simple shall be to copy the data to another table, delete it from this table, and reinsert back from the backup table.
0
 
LVL 1

Author Comment

by:marrowyung
ID: 40600253
"HAVE several records with the value of "0", "

but why relate to the primary key/key ? that's why I don't understand.

" if you had inserted the records after having that field set;
"

you mean this field will not have the auto increment effect for old record?

"to solve this, you have several ways, the most simple shall be to copy the data to another table, delete it from this table, and reinsert back from the backup table. "

I solve that by one statement I post above, near that version but not exactly .
0
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 
LVL 1

Author Closing Comment

by:marrowyung
ID: 40600254
tks anyway even I fix it last night.

I fix it BASICALLY by trial and error. AHHAHAH
0
 
LVL 11

Expert Comment

by:Dany Balian
ID: 40600276
You can also add the field as non primary key.. Update the table using a function, and then set the pkey fields again!

Something like update xxx set recordversion_audit = getnextpkey() where recordversion_audit=0

And getnextpkey would be a function that returns the max+1 from same table xxx

Just saying!
0
 
LVL 1

Author Comment

by:marrowyung
ID: 40600376
I just remove the field and add it back using a statement VERY LIKE the one in my update, and it just work, confirmed by developer.

that's  simple enough.
0
 
LVL 1

Author Comment

by:marrowyung
ID: 40602574
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL several years ago, it seemed like now was a good time to update it for object-oriented PHP.  This article does that, replacing as much as possible the pr…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question