I have two tables tblcity and tblcityx:
--
-- Table structure for table `tblcity`
--
CREATE TABLE IF NOT EXISTS `tblcity` (
`pkcityid` bigint(20) NOT NULL AUTO_INCREMENT,
`cityname` varchar(255) CHARACTER SET latin1 DEFAULT NULL,
`fkstateid` bigint(20) NOT NULL,
PRIMARY KEY (`pkcityid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;
-- --------------------------------------------------------
--
-- Table structure for table `tblcityx`
--
CREATE TABLE IF NOT EXISTS `tblcityx` (
`pkcityid` bigint(20) NOT NULL AUTO_INCREMENT,
`cityname` varchar(255) CHARACTER SET latin1 DEFAULT NULL,
`fkstateid` bigint(20) NOT NULL,
PRIMARY KEY (`pkcityid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1;
I am trying to add a new record in tblcity from tblcityx. Currently, I have tried:
$cityid = 10; // could be any city id
INSERT INTO `tblcity` (SELECT * FROM `tblcityx` WHERE pkcityid = '$cityid')
Both tables have same number of fields and pkcityid is auto-incremented.
It adds a new DUPLICATE record in tblcity as it has already a city with pkcityid=10, so after running the above query there are two records with pkcityid=10.
Isn't there a way that even if the tblcity already has a city with that pkcityid, it should always add a NEW record?
Might be straight forward for an expert but I'm unable to find a way through.
pkcityidshould be primary key, or at least have aUNIQUEconstraint. Can you change that? Otherwise, there is no simple way to do that with a simple SQL statement (you could still do what you want with a convoluted stored procedure, or with an external script).pkcityidis primary key in both tables but the problem stands still.tblcity"?! Or did you mean it raises a DUPLICATE error ?UNIQUEconstraint was not been set.