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
Solved

add MySQL primary key

Posted on 2015-02-09
7
179 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 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
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.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

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…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

828 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