Solved

add MySQL primary key

Posted on 2015-02-09
7
175 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
  • 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 142

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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Foreword In the years since this article was written, numerous hacking attacks have targeted password-protected web sites.  The storage of client passwords has become a subject of much discussion, some of it useful and some of it misguided.  Of cou…
As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

743 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now