Is it possible to import my database on another server? - mysql

I am working on a database which is placed on an online server (Linux). I had to do big changes to this database so I moved (by export then import) this database to a local one I created (Windows Server).
After working these last days on the local database I decided that the work was already done so I exported the local database and imported it to the live one but I could not. It gave me error related to foreign-key:
#1215 - Cannot add foreign key constraint
The table phpmyadmin said that was the one with some errors was printprices and these are the create tables I have for printprices and the other tables which could have some relation to the error:
CREATE TABLE `printprices` (
`STYLE` int(11) NOT NULL DEFAULT '0',
`DIFICULTAD` int(11) NOT NULL DEFAULT '0',
`NCOLORES` int(11) NOT NULL DEFAULT '0',
`PROVEEDOR` int(11) NOT NULL DEFAULT '0',
`SIZECM2MAX` int(11) NOT NULL DEFAULT '0',
`MINQ` int(11) NOT NULL,
`MAXQ` int(11) NOT NULL,
`PRECIO` decimal(5,2) DEFAULT '0.00',
`PRECIOPRV` decimal(5,2) DEFAULT '0.00',
`MINWORK` decimal(5,2) DEFAULT '0.00',
`MINWORKPRV` decimal(5,2) DEFAULT '0.00',
PRIMARY KEY (`STYLE`,`DIFICULTAD`,`NCOLORES`,`PROVEEDOR`,`SIZECM2MAX`,`MINQ`),
KEY `FK_PRINT` (`STYLE`,`DIFICULTAD`,`NCOLORES`,`PROVEEDOR`),
CONSTRAINT `FK_PRINT` FOREIGN KEY (`STYLE`, `DIFICULTAD`, `NCOLORES`, `PROVEEDOR`) REFERENCES `print` (`STYLE`, `DIFICULTAD`, `NCOLORES`, `PROVEEDOR`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8
CREATE TABLE `print` (
`STYLE` int(11) NOT NULL DEFAULT '0',
`DIFICULTAD` int(11) NOT NULL DEFAULT '0',
`NCOLORES` int(11) NOT NULL DEFAULT '0',
`PROVEEDOR` int(11) NOT NULL DEFAULT '0',
`CFC` decimal(5,2) DEFAULT '0.00',
PRIMARY KEY (`STYLE`,`DIFICULTAD`,`NCOLORES`,`PROVEEDOR`),
KEY `FK_PRINTPROV` (`PROVEEDOR`),
KEY `FK_PRINTSTYLE` (`STYLE`),
CONSTRAINT `FK_PRINTPROV` FOREIGN KEY (`PROVEEDOR`) REFERENCES `proveedores` (`CODIPRV`) ON DELETE CASCADE,
CONSTRAINT `FK_PRINTSTYLE` FOREIGN KEY (`STYLE`) REFERENCES `printstyle` (`CODISTY`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8
CREATE TABLE `printstyle` (
`CODISTY` int(11) NOT NULL AUTO_INCREMENT,
`STYLE` varchar(30) DEFAULT NULL,
PRIMARY KEY (`CODISTY`)
) ENGINE=InnoDB AUTO_INCREMENT=22 DEFAULT CHARSET=latin1
CREATE TABLE `proveedores` (
`CODIPRV` int(11) NOT NULL AUTO_INCREMENT,
`PROVEEDOR` varchar(20) DEFAULT NULL,
`ENVIOPRICE` decimal(4,2) NOT NULL DEFAULT '0.00',
`CARTONPRICE` decimal(4,2) NOT NULL DEFAULT '0.00',
`LIMITPRICE` decimal(6,2) NOT NULL,
PRIMARY KEY (`CODIPRV`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=latin1

I've finally found out why it wasn't working:
The problem was I was trying to upload my database on a Mysql, hosted with Linux, which is case sensitive. My foreign keys were stored on capital letter but my tables were not.
When Mysql from Linux read the file and tried to delete a table before creating it, the table wasn't deleted because the name wasn't exactly the same as it had been already stored in capital before.
The solution was to edit the export file and change all table names from lower case to capital.

Related

ERROR: ERROR 1215 (HY000): Cannot add foreign key constraint

I was helping a friend migrate his WordPress to AWS. The whole WordPress was exported by him using WPClone and I'm going to import it to RDS (Aurora). I'm not a pro DB guy and I only knew basic MySQL commands. I was trying to restore a database but during the restoration I encountered ERROR: ERROR 1215 (HY000): Cannot add foreign key constraint.
I executed show engine innodb status and checked the LATEST FOREIGN KEY ERROR and this is what I got but I have no idea how to resolve this.
------------------------
LATEST FOREIGN KEY ERROR
------------------------
2017-03-10 11:22:06 2b87437cb700 Error in foreign key constraint of table clientdb/wp_supsystic_ss_project_networks:
FOREIGN KEY (`project_id`) REFERENCES `wp_supsystic_ss_projects` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8:
Cannot resolve table name close to:
(`id`) ON DELETE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8
Help would be greatly appreciated!
Edit: Here's the create table for wp_supsystic_ss_networks, wp_supsystic_ss_project_networks and wp_supsystic_ss_projects:
CREATE TABLE `wp_supsystic_ss_networks` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`url` varchar(255) NOT NULL,
`class` varchar(255) NOT NULL,
`brand_primary` varchar(7) NOT NULL DEFAULT '#000000',
`brand_secondary` varchar(7) NOT NULL DEFAULT '#ffffff',
`total_shares` int(11) unsigned DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=20 DEFAULT CHARSET=utf8;
CREATE TABLE `wp_supsystic_ss_project_networks` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`project_id` int(11) unsigned DEFAULT NULL,
`network_id` int(11) unsigned DEFAULT NULL,
`position` int(11) unsigned DEFAULT '0',
`title` varchar(255) DEFAULT NULL,
`text` varchar(255) DEFAULT NULL,
`tooltip` varchar(255) DEFAULT NULL,
`text_format` varchar(255) DEFAULT NULL,
`use_short_url` bit(1) DEFAULT NULL,
`icon_image` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `FK__wp_supsystic_ss_projects` (`project_id`),
KEY `FK__wp_supsystic_ss_networks` (`network_id`),
CONSTRAINT `FK__wp_supsystic_ss_networks` FOREIGN KEY (`network_id`) REFERENCES `wp_supsystic_ss_networks` (`id`),
CONSTRAINT `FK__wp_supsystic_ss_projects` FOREIGN KEY (`project_id`) REFERENCES `wp_supsystic_ss_projects` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
CREATE TABLE `wp_supsystic_ss_projects` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`title` varchar(255) NOT NULL,
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`settings` text NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
Two problems:
Create tables in this order: child tables, parent table (with foreign keys). Replace CREATE TABLE statements:
wp_supsystic_ss_projects
wp_supsystic_ss_networks
wp_supsystic_ss_project_networks.
Child table wp_supsystic_ss_networks does not exist in the script.

"Foreign key contraint" error message although there are no foreign keys

I am trying to change the engine for my table "adverts". It does not let me and I get the message that "a foreign key contraint fails". But I removed all foreign keys and SHOW CREATE TABLE gives me this:
CREATE TABLE `adverts` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`id_users` int(11) NOT NULL,
`id_categories_adverts` int(11) NOT NULL,
`type` int(11) NOT NULL,
`status` int(11) NOT NULL,
`duration` int(11) NOT NULL,
`headline` varchar(200) NOT NULL,
`description` text NOT NULL,
`show_contact` int(11) NOT NULL,
`stamp_created` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`stamp_updated` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=19 DEFAULT CHARSET=utf8
Why can't I change the engine of my table? I am using
ALTER TABLE adverts ENGINE=MyISAM
The problem is that there is at least another table with foreign key to your adverts table. You need to tackle with those foreign key constraints, possibly, by temporarily/permanently removing them.

Foreign key constraint fails even if parent row is present

I have following 2 tables.
CREATE TABLE `RuleEntity` (
`rule_id` bigint(20) NOT NULL,
`action_word` varchar(255) DEFAULT NULL,
`condition_word` varchar(255) DEFAULT NULL,
`description` varchar(255) DEFAULT NULL,
`name` varchar(255) DEFAULT NULL,
`ruleGUID` varchar(255) NOT NULL,
`rule_type` varchar(255) DEFAULT NULL,
`caseflag` tinyint(1) NOT NULL DEFAULT '0',
`globalflag` tinyint(1) NOT NULL DEFAULT '1',
`multilineflag` tinyint(1) NOT NULL DEFAULT '1',
`isDefault` tinyint(1) DEFAULT '0',
PRIMARY KEY (`rule_id`),
UNIQUE KEY `ruleGUID` (`ruleGUID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
CREATE TABLE `PreProcessorRule` (
`id` bigint(20) NOT NULL,
`rank` int(11) DEFAULT NULL,
`rule_rule_id` bigint(20) DEFAULT NULL,
`defaultRuleGUID` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `FK4BD420EB4273A655` (`rule_rule_id`),
CONSTRAINT `FK4BD420EB4273A655` FOREIGN KEY (`rule_rule_id`) REFERENCES `RuleEntity` (`rule_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
I am trying to insert 1 row in RuleEntiy and another in PreprocessorRule. But it fails with foreign key constraint.
INSERT INTO `RuleEntity` (`rule_id`,`action_word`,`condition_word`,`description`,`caseflag`,`globalflag`,`multilineflag`,`name`,`ruleGUID`,`rule_type`, `isDefault`) VALUES ( '1',' ','[^\\x00-\\x7F]','HF Default Rule: Removes Non-ASCII Character.',1,1,1,'HF: Non-ASCII Remover','1157B568665E08','CUSTOM', 1);
Query OK, 1 row affected (0.02 sec)
INSERT INTO `HFP`.`PreProcessorRule` (`id`, `rank`, `rule_rule_id`) VALUES ('1', '0', '1');
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`HFP`.`PreProcessorRule`, CONSTRAINT `FK4BD420EB4273A655` FOREIGN KEY (`rule_rule_id`) REFERENCES `RuleEntity` (`rule_id`))
I ran same sql on local MySQL 5.1 on MAC OS X 10.9. It worked. Now I am trying to run it on the Amazon RDS MySQL where I get this error.
Kindly let me know what is going wrong?

#1005 - Can't create table errno: 150 Magento

I am trying to create a new table for magento and I am trying to reference existing magento tables. From what I googled, the problem that I am getting can be 1 of the 2 issues.
The FK must have a index
The PK must exist before the FK can reference
In both cases, I believe I did both of these correctly. Below is existing table schemas
ALREADY EXISTING TABLES BEING REFERENCED
CREATE TABLE IF NOT EXISTS `core_store` (
`store_id` smallint(5) unsigned NOT NULL AUTO_INCREMENT,
`code` varchar(32) NOT NULL DEFAULT '',
`website_id` smallint(5) unsigned DEFAULT '0',
`group_id` smallint(5) unsigned NOT NULL DEFAULT '0',
`name` varchar(255) NOT NULL,
`sort_order` smallint(5) unsigned NOT NULL DEFAULT '0',
`is_active` tinyint(1) unsigned NOT NULL DEFAULT '0',
PRIMARY KEY (`store_id`),
UNIQUE KEY `code` (`code`),
KEY `FK_STORE_WEBSITE` (`website_id`),
KEY `is_active` (`is_active`,`sort_order`),
KEY `FK_STORE_GROUP` (`group_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='Stores' AUTO_INCREMENT=9 ;
CREATE TABLE IF NOT EXISTS `admin_user` (
`user_id` mediumint(9) unsigned NOT NULL AUTO_INCREMENT,
`firstname` varchar(32) NOT NULL DEFAULT '',
`lastname` varchar(32) NOT NULL DEFAULT '',
`email` varchar(128) NOT NULL DEFAULT '',
`username` varchar(40) NOT NULL DEFAULT '',
`password` varchar(100) NOT NULL DEFAULT '',
`created` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`modified` datetime DEFAULT NULL,
`logdate` datetime DEFAULT NULL,
`lognum` smallint(5) unsigned NOT NULL DEFAULT '0',
`reload_acl_flag` tinyint(1) NOT NULL DEFAULT '0',
`is_active` tinyint(1) NOT NULL DEFAULT '1',
`extra` text,
`failures_num` smallint(6) NOT NULL DEFAULT '0',
`first_failure` datetime DEFAULT NULL,
`lock_expires` datetime DEFAULT NULL,
PRIMARY KEY (`user_id`),
UNIQUE KEY `UNQ_ADMIN_USER_USERNAME` (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='Users' AUTO_INCREMENT=25 ;
Table I am trying to create
CREATE TABLE `oro_dashboard`
( `id` int unsigned NOT NULL,
`name` varchar(255) NOT NULL default '',
`description` varchar(64) NOT NULL default '',
`created_by` int unsigned NOT NULL default '0',
`created_at` date,
`layout` varchar(255) NOT NULL default '',
`default_store_id` int,
PRIMARY KEY (`id`),
KEY `IDX_ORO_DASHBOARD_CREATED_BY` (`created_by`),
CONSTRAINT `FK_ORO_DASHBOARD_CREATED_BY_ADMIN_USER_USER_ID` FOREIGN KEY (`created_by`) REFERENCES `admin_user` (`user_id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `FK_ORO_DASHBOARD_DEFAULT_STORE_ID_CORE_STORE_STORE_ID` FOREIGN KEY (`default_store_id`) REFERENCES `core_store` (`store_id`) ON DELETE SET NULL ON UPDATE CASCADE )
ENGINE=INNODB charset=utf8 COLLATE=utf8_unicode_ci
In magento, I found there is in deed a table called admin_user that does have a column called user_id. There is a core_store table that does have a column called store_id. and both my columns have INDEXES made.
Does anyone have any clue what the issue maybe ? Below is my error message
#1005 - Can't create table 'db.oro_dashboard' (errno: 150)
It is necessary to have data type to be same for the foreign key and its corresponding parent key. The data type is different of the foreign key and its referencing parent key in your table that is why it is giving an error.
created by is int unsigned while userid is medium int in parent table
same is problem in the second foreign key
reference http://dev.mysql.com/doc/refman/5.1/en/innodb-foreign-key-constraints.html
When defining foreign keys, the data type must be the same.
You are defining core_store.store_id as smallint(5) unsigned and so the referencing column must be the same: oro_dashboard.default_store_id.
Do also with oro_dashboard.created_by
Final oro_dashboard CREATE TABLE query,
CREATE TABLE `oro_dashboard`
( `id` int unsigned NOT NULL,
`name` varchar(255) NOT NULL default '',
`description` varchar(64) NOT NULL default '',
`created_by` mediumint(9) unsigned NOT NULL default '0',
`created_at` date,
`layout` varchar(255) NOT NULL default '',
`default_store_id` smallint(5) unsigned,
PRIMARY KEY (`id`),
KEY `IDX_ORO_DASHBOARD_CREATED_BY` (`created_by`) ,
CONSTRAINT `FK_ORO_DASHBOARD_CREATED_BY_ADMIN_USER_USER_ID`
FOREIGN KEY (`created_by`)
REFERENCES `admin_user` (`user_id`)
ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `FK_ORO_DASHBOARD_DEFAULT_STORE_ID_CORE_STORE_STORE_ID`
FOREIGN KEY (`default_store_id`)
REFERENCES `core_store` (`store_id`)
ON DELETE SET NULL ON UPDATE CASCADE
)
SQLFIddle Demo
It appears that the column definition for default_store_id does not match core_store.store_id. it should be smallint(5) unsigned in your table. created_by has the same problem, although it did not prevent the table from being created

MySQL Foreign keys - Error 150 while creating tables

I have written some SQL code to create five tables with several relations(foreign keys).
The first foreign key relation works fine, the tables are created without any errors, but when I try to create gasten_url_toegangscodes I get the following error:
#1005 - Can't create table 'dbname.gasten_url_toegangscodes' (errno: 150).
I've read the docs about foreign keys and I still can't find the problem.. The most strange thing about this is that it works in the first three tables..
Can anyone help me please?
My full SQL code is:
CREATE TABLE IF NOT EXISTS `groepen` (
`id` tinyint(3) NOT NULL AUTO_INCREMENT,
`naam` varchar(50) NOT NULL DEFAULT '',
`status` tinyint(1) NOT NULL DEFAULT '1',
PRIMARY KEY (`id`)
) ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS `gasten` (
`id` int(5) NOT NULL AUTO_INCREMENT,
`bedrijf` varchar(100) NOT NULL DEFAULT '',
`uniek` varchar(64) NOT NULL,
`groepid` tinyint(3) NOT NULL,
PRIMARY KEY (`id`),
FOREIGN KEY (groepid) REFERENCES `groepen` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION
) ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS `passen` (
`id` tinyint(3) NOT NULL AUTO_INCREMENT,
`naam` varchar(30) NOT NULL DEFAULT '',
`color` varchar(7) NOT NULL DEFAULT '#FFFFFF',
`bekend` tinyint(1) NOT NULL DEFAULT '0',
`welkom` tinyint(1) NOT NULL DEFAULT '0',
`aantal` tinyint(1) NOT NULL DEFAULT '0',
`nummer` int(11) NOT NULL DEFAULT '0',
`begrens` tinyint(1) NOT NULL DEFAULT '0',
`status` tinyint(1) NOT NULL DEFAULT '1',
`priority` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
KEY `naam` (`naam`)
) ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS `gasten_url_toegangscodes` (
`uniek` varchar(64) NOT NULL,
`unieke_url_code` char(64) NOT NULL,
`salt` char(16) NOT NULL,
FOREIGN KEY (uniek) REFERENCES `gasten` (`uniek`) ON DELETE CASCADE ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE IF NOT EXISTS `gasten_tickets` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`pas_id` tinyint(3) NOT NULL,
`uniek` varchar(64) NOT NULL,
`barcode` char(16) NOT NULL,
`secret_key` char(32) NOT NULL,
`download_count` int(11) NOT NULL DEFAULT '0',
`last_download_time` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`scanned` enum('N','Y') NOT NULL,
`scanned_datetime` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (`id`),
FOREIGN KEY (uniek) REFERENCES `gasten` (`uniek`) ON DELETE CASCADE ON UPDATE NO ACTION,
FOREIGN KEY (pas_id) REFERENCES `passen` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION
) ENGINE=InnoDB;
Thanks in advance!
Create an index on gasten.uniek
CREATE TABLE IF NOT EXISTS `gasten` (
`id` int(5) NOT NULL AUTO_INCREMENT,
`bedrijf` varchar(100) NOT NULL DEFAULT '',
`uniek` varchar(64) NOT NULL,
`groepid` tinyint(3) NOT NULL,
PRIMARY KEY (`id`),
INDEX `idx_uniek` (`uniek`),
FOREIGN KEY (groepid) REFERENCES `groepen` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION
) ENGINE=InnoDB;
And set the charset of the FK in gasten_url_toegangscodes the same as that of the related column in gasten.
Assuming gasten is UTF-8:
CREATE TABLE IF NOT EXISTS `gasten_url_toegangscodes` (
/* use same charset for this column */
`uniek` varchar(64) NOT NULL CHARSET UTF-8,
`unieke_url_code` char(64) NOT NULL,
`salt` char(16) NOT NULL,
FOREIGN KEY (uniek) REFERENCES `gasten` (`uniek`) ON DELETE CASCADE ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=latin1;