mysqldiff tool drops indexes and adds duplicates and causes errors - mysql

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

Related

Transferring table from MySQL 5.7 to 8.0, showing wrong data on a blob column

I am transferring a single table from MySQL 5.7 to MySQL 8.
First, I used mysqldump db_name table_name > table_name.sql in my current server(MYSQL 5.7); taken from here
Then, in my VM with MySQL 8, I used mysql -u username -p db_name < /path/to/table_name.sql.
The columns with the 'blob' data type are not correct, the other columns are okay.
the 'blob' columns went from "International Master (1973); Grandmaster (1975); FIDE Senior Trainer (2004).Alexander Genrikhovich Beliavsky was..." to "0x3C703E496E7465726E6174696F6E616C204D6173746572202831393733293B204772616E646D617374657220...".
Both databases have character-set = latin1 and collation = latin1_swedish_ci.
How can I properly transfer the 'blob' columns from MySQL 5.7 to MySQL 8?
Output when running SHOW CREATE TABLE table_name:
OLD:
CREATE TABLE `Player` (
`pid` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(34) DEFAULT NULL,
`longname` varchar(64) DEFAULT NULL,
`rating` int(10) unsigned NOT NULL DEFAULT '0',
`born` varchar(10) DEFAULT NULL,
`died` varchar(10) DEFAULT NULL,
`country` varchar(10) DEFAULT NULL,
`nationality` varchar(10) DEFAULT NULL,
`gender` enum('M','F') DEFAULT NULL,
`bio` blob,
`fiderating` int(10) unsigned NOT NULL DEFAULT '0',
`rawbio` blob,
`fidenumber` int(10) unsigned DEFAULT NULL,
`fideblitz` int(10) unsigned DEFAULT '0',
`fiderapid` int(10) unsigned DEFAULT '0',
PRIMARY KEY (`pid`),
KEY `name` (`name`),
KEY `rating` (`rating`),
KEY `country` (`country`),
KEY `nationality` (`nationality`),
KEY `gender` (`gender`),
KEY `fidenumber` (`fidenumber`),
FULLTEXT KEY `longname` (`longname`)
) ENGINE=MyISAM AUTO_INCREMENT=168045 DEFAULT CHARSET=latin1
NEW:
| Player | CREATE TABLE `Player` (
`pid` int unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(34) DEFAULT NULL,
`longname` varchar(64) DEFAULT NULL,
`rating` int unsigned NOT NULL DEFAULT '0',
`born` varchar(10) DEFAULT NULL,
`died` varchar(10) DEFAULT NULL,
`country` varchar(10) DEFAULT NULL,
`nationality` varchar(10) DEFAULT NULL,
`gender` enum('M','F') DEFAULT NULL,
`bio` blob,
`fiderating` int unsigned NOT NULL DEFAULT '0',
`rawbio` blob,
`fidenumber` int unsigned DEFAULT NULL,
`fideblitz` int unsigned DEFAULT '0',
`fiderapid` int unsigned DEFAULT '0',
PRIMARY KEY (`pid`),
KEY `name` (`name`),
KEY `rating` (`rating`),
KEY `country` (`country`),
KEY `nationality` (`nationality`),
KEY `gender` (`gender`),
KEY `fidenumber` (`fidenumber`),
FULLTEXT KEY `longname` (`longname`)
) ENGINE=MyISAM AUTO_INCREMENT=168045 DEFAULT CHARSET=latin1 |

Recognizing foreign key in MySQL script

I am analyzing a SQL script to find all the primary and foreign keys. I don't know whether the last line of the script "KEY ID_STAFF (id_staff)" is a primary key or a foreign key. The full script is
CREATE TABLE IF NOT EXISTS `conge` (
`id_conge` int(10) NOT NULL AUTO_INCREMENT,
`id_staff` int(10) NOT NULL DEFAULT '0',
`annee` varchar(4) DEFAULT NULL,
`total_conge` double DEFAULT NULL,
`report_conge` double DEFAULT NULL,
`conge_pris` double DEFAULT NULL,
`debut` date DEFAULT NULL,
`fin` date DEFAULT NULL,
`conge_reste` double DEFAULT NULL,
`codeterrain` varchar(14) NOT NULL DEFAULT '',
`typeconge` varchar(6) DEFAULT NULL,
`commentaire` text,
`id_contrat` int(10) DEFAULT NULL,
`typeautre` varchar(6) DEFAULT NULL,
`lienbulletin` tinyint(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`codeterrain`,`id_conge`),
KEY `ID_STAFF` (`id_staff`)
)

Not getting expected Output while trying to alter a column default value

Problem:
I have created a table with a given structure:
CREATE TABLE `X` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`MSISDN` varchar(25) DEFAULT NULL,
`subservice_id` varchar(25) DEFAULT NULL,
`SUB_START_DATE` datetime DEFAULT NULL,
`START_DATE` datetime DEFAULT NULL,
`END_DATE` datetime DEFAULT NULL,
`scheduler_renewal_status` varchar(25) NOT NULL DEFAULT 'scheduled',
`STATUS` varchar(20) DEFAULT NULL,
`LAST_RENEW_DATE` datetime DEFAULT NULL,
`LAST_CALL_DATE` datetime DEFAULT NULL,
`TRANSACTION_STATUS` varchar(15) DEFAULT NULL,
`CIRCLE` varchar(3) DEFAULT NULL,
`COUNTRY` varchar(3) DEFAULT NULL,
`LANGUAGE` varchar(3) DEFAULT NULL,
`price` float DEFAULT '0',
`CALL_ATTEMPTS` int(3) DEFAULT NULL,
`PRIMARY_ACT_MODE` varchar(25) DEFAULT NULL,
`SECONDARY_ACT_MODE` varchar(25) DEFAULT NULL,
`ERROR_MSG` varchar(25) DEFAULT NULL,
`SUB_TYPE` varchar(10) DEFAULT NULL,
`SUB_TIME_LEFT` int(3) DEFAULT '0',
`SUB_SERVICE_NAME` varchar(25) DEFAULT NULL,
`OPERATOR` varchar(25) DEFAULT NULL,
`GIFT_ID` varchar(25) DEFAULT NULL,
`service_id` varchar(25) DEFAULT NULL,
`RETRY_COUNT` int(2) NOT NULL DEFAULT '0',
`NEXT_RETRY_DATE` datetime DEFAULT NULL,
`CPID` int(11) DEFAULT NULL,
`SCHEDULER_RETRY_STATUS` varchar(25) DEFAULT 'scheduled',
`scheduler_statechanger_status` varchar(25) DEFAULT 'scheduled',
`scheduler_subs_msgs_status` varchar(25) DEFAULT 'scheduled',
`serviceName` varchar(25) DEFAULT NULL,
`transactionId` varchar(25) DEFAULT NULL,
`nextAction` varchar(25) DEFAULT NULL,
`topUpPrice` int(3) DEFAULT NULL,
`topUp_time_left` int(5) NOT NULL DEFAULT '0',
`autoRenew` varchar(10) DEFAULT NULL,
`option1` varchar(25) DEFAULT NULL,
`callBackRecvFlag` varchar(10) DEFAULT NULL,
`last_msg_date` date DEFAULT NULL,
`accountType` varchar(10) DEFAULT 'prepaid',
`optkey` varchar(20) DEFAULT NULL,
`successCallCount` int(3) DEFAULT '0',
PRIMARY KEY (`ID`),
UNIQUE KEY `UK_3` (`MSISDN`,`subservice_id`)
) ENGINE=InnoDB AUTO_INCREMENT=27793 DEFAULT CHARSET=utf8
I want to change the column SUB_START_DATE default type from NULL to TIMESTAMP.
Can anyone guide me the proper syntax for this
Solution:
I am running the following Command:
ALTER TABLE `X` MODIFY COLUMN SUB_START_DATE TIMESTAMP NOT NULL DEFAULT current_timestamp;
It is affecting the rows of the table.
But,if I want to have column datatype datetime only and change the default value to current_timestamp.
I ran the following command:
ALTER TABLE `X` MODIFY COLUMN SUB_START_DATE datetime DEFAULT current_timestamp;
OutputShown:
Error Code : 1067
Invalid default value for 'SUB_START_DATE'
In this case my SQL server does not allow to modify existing default constraint value. So to change the default value we need to delete the existing system generated or user generated default constraint. And after that default value can be set for a particular column.
Follow some steps :
List all existing default value constraints for columns.
Execute this system database procedure, it takes table name as a parameter. It returns list of all constrains for all columns within table.
execute [dbo].[sp_helpconstraint] 'table_name'
Drop existing default constraint for a column.
Syntax:
alter table 'table_name' drop constraint 'constraint_name'
Add new default value constraint for that column:
Syntax:
alter table 'table_name' add default 'current_timestamp' for 'SUB_START_DATE '

mysql join query is slow compared with mssql

I have the following simple join query
SELECT
count(*)
FROM
DBx.caseview p2015
INNER JOIN DBy.caseview p2014 ON p2015.casenumber=p2014.casenumber;
For some reason it just leaves MySQL hanging there for a lot of time until I get tired and cancel it. On the contrary, if run exactly the same code on MSSQL with the same data set the query takes a few seconds at most.
Is there a parameter that needs to be changed on MySQL to speed up this type of queries?
Here's my table in MySQL
CREATE TABLE `caseview` (
`ID` bigint(20) NOT NULL AUTO_INCREMENT,
`CASEID` varchar(18) DEFAULT NULL,
`CASENUMBER` int(10) DEFAULT NULL,
`ACCOUNTID` varchar(18) DEFAULT NULL,
`ACCOUNT` varchar(256) DEFAULT NULL,
`ASSETID` varchar(18) DEFAULT NULL,
`SAPPRODUCTGROUP` varchar(10) DEFAULT NULL,
`PRODUCT` varchar(128) DEFAULT NULL,
`FAMILY` varchar(128) DEFAULT NULL,
`CONTACTID` varchar(18) DEFAULT NULL,
`OWNERID` varchar(18) DEFAULT NULL,
`TYPE` varchar(128) DEFAULT NULL,
`PRIORITY` varchar(24) DEFAULT NULL,
`ORIGIN` varchar(24) DEFAULT NULL,
`SUBJECT` varchar(256) DEFAULT NULL,
`STATUS` varchar(24) DEFAULT NULL,
`LASTACTIVITY` varchar(1024) DEFAULT NULL,
`INITALDESCRIPTION` varchar(1024) DEFAULT NULL,
`CLOSEDDATE` datetime DEFAULT NULL,
`CREATEDDATE` datetime DEFAULT NULL,
`LASTMODIFIEDDATE` datetime DEFAULT NULL,
PRIMARY KEY (`ID`),
UNIQUE KEY `ID_UNIQUE` (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=122393 DEFAULT CHARSET=utf8;
There's two tables with exactly the same configuration as above, just different data
DBx.caseview p2015 has 197647 rows
DBy.caseview p2014 has 122392 rows
Create an Index for CASENUMBER field.
ALTER TABLE `caseview` ADD INDEX ( `CASENUMBER` ) ;
Give it a few seconds to create the index and run the query again.

why Mysql is giving me error 1280 "Wrong Index"

Can anyone explain why Mysql is giving me error 1280 ("wrong index for 'fk_chart_aid_aid' ") error whend I try to create the "CHART OF ACCOUNTS" table. I'm completly confused here. How can I fix this so I can create the table? The "ACCOUNT" table already exists in the database and has data in it.
Thanks for the help.
MYSQL Server version: 5.1.54
CHART OF ACCOUNTS:
DROP TABLE IF EXISTS `rst`.`acctg_chart_of_accounts` ;
CREATE TABLE IF NOT EXISTS `rst`.`acctg_chart_of_accounts` (
`acctg_chart_of_accounts_id` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
`account_id` INT UNSIGNED NOT NULL ,
`account_nbr` VARCHAR(45) NULL ,
`description` VARCHAR(45) NULL ,
`account_type` INT UNSIGNED NULL ,
`commissionable` TINYINT UNSIGNED NULL ,
`hidden` TINYINT UNSIGNED NULL ,
`deduct_balance_from_owner_check` TINYINT UNSIGNED NULL ,
PRIMARY KEY (`acctg_chart_of_accounts_id`) ,
CONSTRAINT `fk_chart_aid_aid`
FOREIGN KEY (`account_id` )
REFERENCES `rst`.`account` (`account_id` )
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB;
CREATE INDEX `fk_chart_aid_aid` ON `rst`.`acctg_chart_of_accounts` (`account_id` ASC) ;
ACCOUNTS TABLE THAT IS BEING REFERENCED:
CREATE TABLE IF NOT EXISTS `account` (
`account_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`account_status_id` int(10) unsigned NOT NULL,
`company_name` varchar(155) DEFAULT NULL,
`address1` varchar(155) DEFAULT NULL,
`address2` varchar(155) DEFAULT NULL,
`city` varchar(155) DEFAULT NULL,
`state` varchar(155) DEFAULT NULL,
`zip` varchar(45) DEFAULT NULL,
`country` varchar(255) DEFAULT NULL,
`work_phone` varchar(45) DEFAULT NULL,
`mobile_phone` varchar(45) DEFAULT NULL,
`time_zone` varchar(45) DEFAULT NULL,
`subdomain` varchar(155) DEFAULT NULL,
`cname_URL` varchar(255) DEFAULT NULL,
`promotion_code` varchar(45) DEFAULT NULL,
`can_we_contact_you` tinyint(4) DEFAULT NULL COMMENT '0=false, 1=true',
`units_managed_nbr` varchar(10) DEFAULT NULL,
`a_hear_about_us_list_id` tinyint(3) unsigned DEFAULT NULL COMMENT 'populated from dropdown list.',
`receive_special_offers` tinyint(4) DEFAULT NULL,
`receive_announcements` tinyint(4) DEFAULT NULL,
`receive_newsletter` tinyint(4) DEFAULT NULL,
`create_ts` timestamp NULL DEFAULT NULL,
`expires` timestamp NULL DEFAULT NULL,
`storage_capacity` varchar(255) DEFAULT NULL COMMENT '1073741824 = 1GB',
`logo` varchar(455) DEFAULT NULL,
`max_active_connections` int(11) DEFAULT '3',
`_product_id` int(11) DEFAULT NULL,
`report_footer` varchar(455) DEFAULT NULL,
`welcome_dialog` tinyint(4) DEFAULT '1',
`ARB_subscription_id` int(11) DEFAULT NULL,
`trashbin` tinyint(4) NOT NULL DEFAULT '1',
PRIMARY KEY (`account_id`),
KEY `fk_account_account_status_id` (`account_status_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=58 ;
Are you getting the error after the CREATE TABLE statement, or after the subsequent CREATE INDEX?
Looks like you are attempting to name both a FOREIGN KEY constraint and an INDEX fk_chart_aid_aid. Try choosing a different name for either one of them.
Also, in the accounts table, account_id is INT(10). Try also to change the column definition in acctg_chart_of_accounts to:
`account_id` INT(10) UNSIGNED NOT NULL ,
Though, I think that mysql defaults type INT to INT(10) anyway...
I met the same issue; tried manually rename the index to a different name but don't like the idea of 'manually' and neither I don't really understand why we need to generate the index separately. so I decide to generate it within the create statement by unchecking the option of 'generate separate index statement' in 'forwarding engineer', and this fix the issue.