Solved

ON DUPLICATE KEY UPDATE question

Posted on 2010-09-09
1
319 Views
Last Modified: 2012-06-27
Got the tables defined below. I need to only update values when a record is trying to be inserted if it is still in the table.
However, I am not sure how to build the indexes for the ON DUPLICATE KEY UPDATE because the keys are mostly autoinc.
Can anyone tell me how to configure these tables to allow this?

Does every column need to be in the key? Does every column need to be updated?

CREATE TABLE IF NOT EXISTS `niin` (
  `NIIN_ID` int(10) NOT NULL AUTO_INCREMENT,
  `NIINS_ID` int(10) NOT NULL DEFAULT '0',
  `NSN_NUMBER` varchar(255) NOT NULL DEFAULT '0',
  `ITEMNAME` varchar(255) NOT NULL DEFAULT '0',
  `SCHEDULEB` varchar(255) NOT NULL DEFAULT '0',
  `ENAC` varchar(255) NOT NULL DEFAULT '0',
  `IMAGE` varchar(255) NOT NULL DEFAULT '',
  KEY `Index 1` (`NIIN_ID`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

CREATE TABLE IF NOT EXISTS `niinid` (
  `NIINID_ID` int(10) NOT NULL AUTO_INCREMENT,
  `NIIN_ID` int(10) NOT NULL DEFAULT '0',
  `FIIG` varchar(20) NOT NULL DEFAULT '0',
  `INC` varchar(20) NOT NULL DEFAULT '0',
  `CRITCD` varchar(20) NOT NULL DEFAULT '0',
  `II` varchar(20) NOT NULL DEFAULT '0',
  `RPDMRC` varchar(20) NOT NULL DEFAULT '0',
  `DMIL` varchar(20) NOT NULL DEFAULT '0',
  `DMILINTCD` varchar(20) NOT NULL DEFAULT '0',
  `NIINASGMT` varchar(20) NOT NULL DEFAULT '0',
  `PMIC` varchar(20) NOT NULL DEFAULT '0',
  `ADP` varchar(20) NOT NULL DEFAULT '0',
  `ESDEMI` varchar(20) NOT NULL DEFAULT '0',
  `HMIC` varchar(20) NOT NULL DEFAULT '0',
  `HCC` varchar(20) NOT NULL DEFAULT '0',
  KEY `Index 1` (`NIINID_ID`),
  KEY `Index 2` (`NIIN_ID`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

CREATE TABLE IF NOT EXISTS `niinprocurement` (
  `NIINPROCUREMENT_ID` int(10) NOT NULL AUTO_INCREMENT,
  `NIIN_ID` int(10) NOT NULL DEFAULT '0',
  `EFF_DT` varchar(20) NOT NULL DEFAULT '0',
  `MOE` varchar(20) NOT NULL DEFAULT '0',
  `AAC` varchar(20) NOT NULL DEFAULT '0',
  `SOS` varchar(20) NOT NULL DEFAULT '0',
  `UI` varchar(20) NOT NULL DEFAULT '0',
  `UI_PRICE` decimal(10,2) NOT NULL DEFAULT '0.00',
  `QUP` varchar(20) NOT NULL DEFAULT '0',
  `CIIC` varchar(20) NOT NULL DEFAULT '0',
  `SLC` varchar(20) NOT NULL DEFAULT '0',
  `REP` varchar(20) NOT NULL DEFAULT '0',
  `USC` varchar(20) NOT NULL DEFAULT '0',
  KEY `Index 1` (`NIINPROCUREMENT_ID`),
  KEY `Index 2` (`NIIN_ID`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

CREATE TABLE IF NOT EXISTS `niins` (
  `NIINS_ID` int(10) NOT NULL AUTO_INCREMENT,
  `PARTNO` varchar(250) NOT NULL DEFAULT '',
  `DESCRIPTION` varchar(255) NOT NULL,
  `NSN` varchar(255) NOT NULL,
  KEY `Index 1` (`NIINS_ID`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

CREATE TABLE IF NOT EXISTS `niinxref` (
  `NIINXREF_ID` int(10) NOT NULL AUTO_INCREMENT,
  `NIIN_ID` int(10) NOT NULL DEFAULT '0',
  `REFPN` varchar(20) NOT NULL DEFAULT '0',
  `CAGECD` varchar(20) NOT NULL DEFAULT '0',
  `STAT` varchar(20) NOT NULL DEFAULT '0',
  `RNCC` varchar(20) NOT NULL DEFAULT '0',
  `RNVC` varchar(20) NOT NULL DEFAULT '0',
  `DAC` varchar(20) NOT NULL DEFAULT '0',
  `RNAAC` varchar(20) NOT NULL DEFAULT '0',
  `RNFC` varchar(20) NOT NULL DEFAULT '0',
  `RNSC` varchar(20) NOT NULL DEFAULT '0',
  `RNJC` varchar(20) NOT NULL DEFAULT '0',
  `SADC` varchar(20) NOT NULL DEFAULT '0',
  `HCC` varchar(20) NOT NULL DEFAULT '0',
  `MSDS` varchar(20) NOT NULL DEFAULT '0',
  KEY `Index 1` (`NIINXREF_ID`),
  KEY `Index 2` (`NIIN_ID`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

Open in new window

0
Comment
Question by:EddieShipman
1 Comment
 
LVL 3

Accepted Solution

by:
SuchetaL earned 500 total points
ID: 33643703
Hi Eddie,
I need to only update values when a record is trying to be inserted if it is still in the table.
How do you decide that this is a repeated record? Other than the auto inc ID's there must be something that you don't want repeated. For the first table in your example "niin" - assume you don't want the NSN_NUMBER & ITEMNAME repeated. So add it as a Unique Index,
If you specify ON DUPLICATE KEY UPDATE, and a row is inserted that would cause a duplicate value in a UNIQUE index or PRIMARY KEY, an UPDATE of the old row is performed.
 In case you want to check with the last inserted record for the auto incremented ID - you can do the following
// Your table has cols - id, a, b, c - then
INSERT INTO table (a,b,c) VALUES (1,2,3)
  ON DUPLICATE KEY UPDATE id=LAST_INSERT_ID(id), c=3;

Hope that helps
Sucheta
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Suggested Solutions

This guide whil teach how to setup live replication (database mirroring) on 2 servers for backup or other purposes. In our example situation we have this network schema (see atachment). We need to replicate EVERY executed SQL query on server 1 to…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

825 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