insert record on duplicate update is not updating - mysql

I am using this insert query which is supposed to update on duplicate situation. It does not update and does not result in error. What is wrong here?
Primary key are the columns res_id, lud
INSERT INTO sv_sa (res_id,resort,resort_us,weather,templo,temphi,alert_val,alert,ski_id,lud,tweet)
VALUES (1561,'Aachen','aachen','PM Rain/Snow',-1,3,2,4,NULL,'2014-01-25',0)
ON DUPLICATE KEY UPDATE templo=-1, temphi=3, alert_val=2, alert=4, ski_id=NULL
CREATE TABLE `sv_sa` (
`res_id` int(6) NOT NULL,
`resort` varchar(30) DEFAULT NULL,
`resort_us` varchar(30) DEFAULT NULL,
`ski_id` int(4) DEFAULT NULL,
`templo` decimal(4,2) DEFAULT NULL,
`temphi` decimal(4,2) DEFAULT NULL,
`weather` varchar(50) DEFAULT NULL,
`alert_val` int(3) DEFAULT NULL,
`alert` int(3) DEFAULT NULL,
`snow_valley_min` int(4) DEFAULT NULL,
`snow_valley_max` int(4) DEFAULT NULL,
`snow_mountain` int(4) DEFAULT NULL,
`lifts_open` varchar(8) DEFAULT NULL,
`tweet` tinyint(4) DEFAULT NULL,
`lud` date NOT NULL,
PRIMARY KEY (`res_id`,`lud`),
KEY `alert` (`alert`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

On duplicate values are the same as the record inserted so there is no change to the record, change one of the duplicate values, say
temphi=5
try it and it will set
temphi=5
on subsequent inserts.

Related

phpMyAdmin Error - Says my table does not have a unique column when it has two

phpMyAdmin is displaying:
Current selection does not contain a unique column. Grid edit, checkbox, Edit, Copy and
Delete features are not available.
It displays this even when I have a unique column. In fact, I have two:
CREATE TABLE `Rest_Details` (
`Resturant_ID` bigint(255) NOT NULL AUTO_INCREMENT,
`Resturant_name` varchar(100) NOT NULL,
`Resturant_des` varchar(200) NOT NULL,
`Res_Address_Line_1` varchar(200) NOT NULL,
`Res_Address_Line_2` varchar(200) DEFAULT NULL,
`City_name` varchar(100) NOT NULL,
`Resturant_Postcode` varchar(8) DEFAULT NULL,
`Cat_ID` tinyint(11) NOT NULL,
`Avg_Del` tinyint(11) NOT NULL,
`Est_Del` tinyint(11) NOT NULL,
`Email1` varchar(200) NOT NULL,
`Email2` varchar(200) DEFAULT NULL,
`Min_ord` tinyint(11) NOT NULL,
PRIMARY KEY (`Resturant_ID`),
UNIQUE KEY `Resturant_name` (`Resturant_name`),
UNIQUE KEY `Resturant_ID` (`Resturant_ID`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8
I believe this is causing countless problems with inserting data and selecting data from the table in question. How can I resolve this?

mysqldiff tool drops indexes and adds duplicates and causes errors

I'm using the mysqldiff tool (v1.5.3) from MySQL to synchronize the schema between two databases. The databases should have the same schema, but would like to produce a file that only has the ALTER TABLE differences between them so it can (after thorough testing) be run in production.
The problem is that the mysqldiff tool produces some weirdness. For example:
ALTER TABLE `database1`.`ACCOUNT_TRANSACTIONS`
DROP INDEX MATCHING_TRANSACTION_ID,
DROP INDEX PAYEE_ID,
ADD INDEX CHECK_NUMBER (ACCOUNT_ID),
ADD INDEX PAYEE_ID (PAYEE_ID),
ADD INDEX CHECK_NUMBER (PAYEE_ID,CHECK_NUMBER);
It seems to not only drop the indexes, but tries to recreate the new index through two statements that produce an error. The final schema should have CHECK_NUMBER (ACCOUNT_ID, PAYEE_ID, CHECK_NUMBER) as the index.
What might be causing this?
Final version:
CREATE TABLE `ACCOUNT_TRANSACTIONS` (
`TRANSACTION_ID` int(12) NOT NULL AUTO_INCREMENT,
`ACCOUNT_ID` int(1) DEFAULT NULL,
`REF_NUMBER` varchar(15) DEFAULT NULL,
`PAYEE_ID` int(6) DEFAULT NULL,
`ACCOUNT_CHART_ID` int(12) DEFAULT NULL,
`TRANSACTION_AMOUNT` decimal(10,2) DEFAULT NULL,
`TRANSACTION_DATE` date DEFAULT NULL,
`MEMO` varchar(100) DEFAULT NULL,
`ACCOUNT_BALANCE` decimal(10,2) DEFAULT NULL,
`DEBIT_OR_CREDIT` char(1) DEFAULT 'D',
`MATCHING_TRANSACTION_ID` int(11) DEFAULT '0',
`RECONCILED_DATE` date DEFAULT NULL,
`RECONCILED_EMPLOYEE_ID` int(12) DEFAULT NULL,
`TIME_UPDATED` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`CHECK_NUMBER` bigint(20) unsigned DEFAULT NULL,
`STATEMENT_ID` int(12) DEFAULT NULL,
PRIMARY KEY (`TRANSACTION_ID`),
KEY `PAYEE_ID` (`PAYEE_ID`),
KEY `CHECK_NUMBER` (`CHECK_NUMBER`,`ACCOUNT_ID`,`PAYEE_ID`)
) ENGINE=InnoDB AUTO_INCREMENT=xxx DEFAULT CHARSET=latin1
Original version (needs to be altered to be final version)
CREATE TABLE `ACCOUNT_TRANSACTIONS` (
`TRANSACTION_ID` int(12) NOT NULL AUTO_INCREMENT,
`ACCOUNT_ID` int(1) DEFAULT NULL,
`REF_NUMBER` varchar(15) DEFAULT NULL,
`PAYEE_ID` int(6) DEFAULT NULL,
`ACCOUNT_CHART_ID` int(12) DEFAULT NULL,
`TRANSACTION_AMOUNT` decimal(10,2) DEFAULT NULL,
`TRANSACTION_DATE` date DEFAULT NULL,
`MEMO` varchar(100) DEFAULT NULL,
`ACCOUNT_BALANCE` decimal(10,2) DEFAULT NULL,
`DEBIT_OR_CREDIT` char(1) DEFAULT 'D',
`MATCHING_TRANSACTION_ID` int(11) DEFAULT '0',
`RECONCILED_DATE` date DEFAULT NULL,
`RECONCILED_EMPLOYEE_ID` int(12) DEFAULT NULL,
`TIME_UPDATED` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`CHECK_NUMBER` bigint(20) unsigned DEFAULT NULL,
`STATEMENT_ID` int(12) DEFAULT NULL,
PRIMARY KEY (`TRANSACTION_ID`),
KEY `PAYEE_ID` (`PAYEE_ID`),
KEY `MATCHING_TRANSACTION_ID` (`MATCHING_TRANSACTION_ID`)
) ENGINE=MyISAM AUTO_INCREMENT=xxx DEFAULT CHARSET=latin1

How to add foreign key in table using existing column, without losing data?

I have a table in which I need to add foreign key on an existing column. Following is create table:
CREATE TABLE `itemtx` (
`itemTxid` int(11) NOT NULL AUTO_INCREMENT,
`itemcode` varchar(1) NOT NULL,
`weight` decimal(7,3) DEFAULT NULL,
`txtype` varchar(10) DEFAULT 'Pickup',
`tripstopid` int(11) NOT NULL,
`barcode` varchar(25) DEFAULT NULL,
`bagcount` int(11) DEFAULT '1',
PRIMARY KEY (`itemTxid`)
) ENGINE=InnoDB AUTO_INCREMENT=33524 DEFAULT CHARSET=latin1
I need to add foreign key on tripstopid column. I cannot drop or empty table as it contains data. Following is the referenced table:
CREATE TABLE `tripstop` (
`tripstopid` int(11) NOT NULL AUTO_INCREMENT,
`tripid` int(11) DEFAULT NULL,
`locationName` varchar(100) DEFAULT NULL,
`userName` varchar(100) DEFAULT NULL,
`locationid` int(11) DEFAULT NULL,
`datetime` varchar(20) DEFAULT NULL,
`createts` varchar(20) DEFAULT NULL,
`latitude` double DEFAULT NULL,
`longitude` double DEFAULT NULL,
`userid` int(11) DEFAULT NULL,
`tid` int(11) DEFAULT NULL,
PRIMARY KEY (`tripstopid`)
) ENGINE=InnoDB AUTO_INCREMENT=4691 DEFAULT CHARSET=latin1
How can I do this without losing my data?
You can achieve this by following:
ALTER TABLE itemtx
ADD FOREIGN KEY (tripstopid) REFERENCES tripstop(tripstopid);
Verified by creating tables, inserting data in them and then updating table for foreign key, previously entered data is not lost.

Duplicate entry mysql

I have a table which has does not have a primary key.Table has some indexes. Whenever I try to insert record for the first time ,record gets inserted successfully but when i try to insert record with same value again it gives Duplicate entry '125249-1'.
Create table :-
CREATE TABLE `schedules` (
`entity` bigint(20) DEFAULT NULL,
`id` bigint(20) DEFAULT NULL,
`applicationid` int(16) DEFAULT NULL,
`apptnid` int(16) DEFAULT NULL,
`apptorgunitid` bigint(20) DEFAULT NULL,
`orgid` int(16) DEFAULT NULL,
`nid` int(16) DEFAULT NULL,
`logged_date` datetime DEFAULT NULL,
`logged_by` int(16) DEFAULT NULL,
`last_updated_date` datetime DEFAULT NULL,
`last_updated_by` int(16) DEFAULT NULL,
`rowstate` int(8) DEFAULT '1',
`status` int(16) DEFAULT '1',
KEY `applicationid` (`applicationid`),
KEY `apptnid` (`apptnid`),
KEY `id` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
insert into schedules (entity, id, applicationid, apptnid, apptorgunitid, orgid, nid, logged_date, logged_by, rowstate, status) values (125249,454,85246,2,2,124,1,current_timestamp,9999,1,1)
Error : - Duplicate entry '125249-1' for key 'PRIMARY'

Field not inserting or updating , int type in sql

I am working on magento platform.I face a problem regarding values insertion to specific field: My query run perfect but one specific column not working for any query.I try my best but didn't find why .When i change the column type from int to varchar type it works.This is my table structure.
CREATE TABLE `followupemails_emaillogs` (
`id` int(8) NOT NULL AUTO_INCREMENT,
`schedule_time` datetime DEFAULT NULL,
`sent_time` datetime DEFAULT NULL,
`email_status` varchar(100) DEFAULT NULL,
`client_name` varchar(250) DEFAULT NULL,
`client_email` varchar(250) DEFAULT NULL,
`followupemails_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `id` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=30 DEFAULT CHARSET=latin1.
the "followupemails_id" column not working in insert and update query.This is one update query where record exist that id(29). UPDATE followupemails_emaillogs SET followupemails_id=5 WHERE id =29.
This is insertion query INSERT INTO followupemails_emaillogs SET followupemails_id=4, schedule_time='2013-10-23 08:10:00', email_status='pending', client_name='ayaz ali'.this works fine on fiddle but not on my sqlyog ? what could be the issue.At last i find query that work perfect
.INSERT INTO followupemails_emaillogs (followupemails_id,schedule_time,email_status,client_name,client_email) VALUES (26,'2013-10-23 08:10:00','pending','ayaz ali','mamhmood#yahoo.com');
Can anyone tell me why set query not working but second query works perfect.so that i can accept his answer.Thanks for all your help
Try like this
To Create,
CREATE TABLE followupemails_emaillogs (
id int(8) NOT NULL AUTO_INCREMENT PRIMARY KEY,
schedule_time datetime DEFAULT NULL,
sent_time datetime DEFAULT NULL,
email_status varchar(100) DEFAULT NULL,
client_name varchar(250) DEFAULT NULL,
client_email varchar(250) DEFAULT NULL,
followupemails_i int(11) DEFAULT NULL,
UNIQUE (id)
)
To Insert,
INSERT INTO followupemails_emaillogs (schedule_time,sent_time,email_status,client_name,client_email,followupemails_i)
VALUES
('2012-05-05','2012-05-06',"sent","sagar","sagar#xxxx.com",2)
the whole query is ok
CREATE TABLE `followupemails_emaillogs` (
`id` int NOT NULL AUTO_INCREMENT,
`schedule_time` datetime DEFAULT NULL,
`sent_time` datetime DEFAULT NULL,
`email_status` varchar(100) DEFAULT NULL,
`client_name` varchar(250) DEFAULT NULL,
`client_email` varchar(250) DEFAULT NULL,
`followupemails_id` int DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `id` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=30 DEFAULT CHARSET=latin1.
but at the last there is dot which is actually error so remove the dot and create the table
latin1.
so remove the dot sign and not null in id filed use this line by default fields are null so don't use default null
id int (8) AUTO_INCREMENT
CREATE TABLE `followupemails_emaillogs` (
`id` int (8) AUTO_INCREMENT,
`schedule_time` datetime DEFAULT NULL,
`sent_time` datetime DEFAULT NULL,
`email_status` varchar(100),
`client_name` varchar(250),
`client_email` varchar(250),
`followupemails_id` int,
PRIMARY KEY (`id`),
UNIQUE KEY `id` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=30 DEFAULT CHARSET=latin1
don't need (11) in the sql query for int operator , This get for length of the nvarchar,varchar datatype column only not a int datatype,So change and write int instead of int(11) and int(8)
Try this query instead of your query
CREATE TABLE `followupemails_emaillogs` (
`id` int NOT NULL AUTO_INCREMENT,
`schedule_time` datetime DEFAULT NULL,
`sent_time` datetime DEFAULT NULL,
`email_status` varchar(100) DEFAULT NULL,
`client_name` varchar(250) DEFAULT NULL,
`client_email` varchar(250) DEFAULT NULL,
`followupemails_id` int DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `id` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=30 DEFAULT CHARSET=latin1.