mysql 1215 - Cannot add foreign key constraint - mysql

I have created the schema with Workbench 6.1 (and MySQL 5.6) and it outputs the code below. But this produces a foreign key constraint. Last time it worked but produced a composite primary key made of the other foreign keys which is overkill I think.
Does anyone have an idea what is wrong here?
SET #OLD_UNIQUE_CHECKS=##UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET #OLD_FOREIGN_KEY_CHECKS=##FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET #OLD_SQL_MODE=##SQL_MODE, SQL_MODE='TRADITIONAL';
CREATE SCHEMA IF NOT EXISTS `yii_jobmedisearch` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ;
CREATE TABLE IF NOT EXISTS `yii_jobmedisearch`.`Person` (
`PersId` BIGINT(19) UNSIGNED NOT NULL AUTO_INCREMENT,
`Vorname` VARCHAR(25) NOT NULL,
`MiddleName` VARCHAR(25) NOT NULL,
`Nachname` VARCHAR(25) NOT NULL,
`Anrede` ENUM('Herr','Frau','Herr Dr.','Frau Dr.','Herr Prof.','Frau Prof.') NOT NULL,
`Titel` ENUM('Dr.','Prof.') NULL DEFAULT NULL,
`Geschlecht` ENUM('Frau','Mann','Transident') NOT NULL,
`Geburtsdatum` DATE NOT NULL,
`Nationalitaet` VARCHAR(2) NOT NULL,`enter code here`
`Nationalitaet2` VARCHAR(2) NULL DEFAULT NULL,
`Heimatort` VARCHAR(60) NULL DEFAULT NULL,
`Strasse` VARCHAR(60) NULL DEFAULT NULL,
`PLZ` VARCHAR(10) NOT NULL,
`Wohnort` VARCHAR(60) NOT NULL,
`Kanton` VARCHAR(4) NULL DEFAULT NULL,
`TelHome` VARCHAR(15) NULL DEFAULT NULL,
`TelMobil` VARCHAR(15) NULL DEFAULT NULL,
`Zivilstand` ENUM('ledig','verheiratet','geschieden','verwittwet') NULL DEFAULT NULL,
`Email` VARCHAR(80) NOT NULL,
`SkypeId` VARCHAR(30) NULL DEFAULT NULL,
`FacebookId` VARCHAR(30) NULL DEFAULT NULL,
`GooglePlusId` VARCHAR(30) NULL DEFAULT NULL,
`XingId` VARCHAR(30) NULL DEFAULT NULL,
`LinkedInId` VARCHAR(30) NULL DEFAULT NULL,
`TwitterId` VARCHAR(30) NULL DEFAULT NULL,
`IstSuchend` ENUM('Sucht','Sucht nicht') NULL DEFAULT NULL,
`AbsenzVon` DATE NULL DEFAULT NULL,
`AbsenzBis` DATE NULL DEFAULT NULL,
`PersonCreateDatum` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`PersonModifDatum` TIMESTAMP NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`PersId`))
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_general_ci;
CREATE TABLE IF NOT EXISTS `yii_jobmedisearch`.`User` (
`UserId` BIGINT(19) UNSIGNED NOT NULL AUTO_INCREMENT,
`UserName` VARCHAR(25) NOT NULL,
`UserPass` VARCHAR(255) NOT NULL,
`UserRolle` ENUM('guest','AGeber','ANehmer','ProAGeber','ProAGeberAdmin','ProANehmer','ProVermittler','ProVermittlerAdmin','Maintainer','Admin') NOT NULL,
`Person_PersId` BIGINT(19) UNSIGNED NOT NULL,
`JobFirma_JobId` BIGINT(19) UNSIGNED NOT NULL,
`JobFirma_HQFirma_HQFirmaId` BIGINT(19) UNSIGNED NOT NULL,
`JobFirma_Filiale_FilialeId` BIGINT(19) UNSIGNED NOT NULL,
`JobFirma_Filiale_HQFirma_HQFirmaId` BIGINT(19) UNSIGNED NOT NULL,
`Filiale_FilialeId` BIGINT(19) UNSIGNED NOT NULL,
`Filiale_HQFirma_HQFirmaId` BIGINT(19) UNSIGNED NOT NULL,
`UserCreateDatum` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`UserModifDatum` TIMESTAMP NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
`UserVerified` TINYINT(3) UNSIGNED NOT NULL DEFAULT 0,
PRIMARY KEY (`UserId`),
INDEX `fk_User_Person_idx` (`Person_PersId` ASC),
INDEX `fk_User_Filiale1_idx` (`Filiale_FilialeId` ASC, `Filiale_HQFirma_HQFirmaId` ASC),
UNIQUE INDEX `UserName_UNIQUE` (`UserName` ASC),
CONSTRAINT `fk_User_Person`
FOREIGN KEY (`Person_PersId`)
REFERENCES `yii_jobmedisearch`.`Person` (`PersId`)
ON DELETE CASCADE
ON UPDATE NO ACTION,
CONSTRAINT `fk_User_Filiale1`
FOREIGN KEY (`Filiale_FilialeId` , `Filiale_HQFirma_HQFirmaId`)
REFERENCES `yii_jobmedisearch`.`Filiale` (`FilialeId` , `HQFirma_HQFirmaId`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_general_ci;
CREATE TABLE IF NOT EXISTS `yii_jobmedisearch`.`HQFirma` (
`HQFirmaId` BIGINT(19) UNSIGNED NOT NULL AUTO_INCREMENT,
`HQFirmenname` VARCHAR(80) NOT NULL,
`HQAnzFilialen` SMALLINT(5) UNSIGNED NULL DEFAULT NULL,
`HQGesellschaftsform` ENUM('AG','GmbH','Einzelfirma') NOT NULL,
`HQBranche` ENUM('Spital','Klinik','Stellenvermittler','Versicherung') NOT NULL,
`HQFirmenbeschrieb` VARCHAR(512) NOT NULL,
`HQStrasse` VARCHAR(60) NOT NULL,
`HQPLZ` VARCHAR(10) NOT NULL,
`HQPostfach` VARCHAR(20) NULL DEFAULT NULL,
`HQOrt` VARCHAR(60) NOT NULL,
`HQFax` VARCHAR(15) NULL DEFAULT NULL,
`HQTelefon` VARCHAR(15) NOT NULL,
`HQTelefon2` VARCHAR(15) NULL DEFAULT NULL,
`HQEmail` VARCHAR(80) NOT NULL,
`HQEmail2` VARCHAR(80) NULL DEFAULT NULL,
`HQWebURL` VARCHAR(255) NULL DEFAULT NULL,
`HQWebURL2` VARCHAR(255) NULL DEFAULT NULL,
`HQAnzLikes` MEDIUMINT(8) UNSIGNED NULL DEFAULT NULL,
`HQSkypeId` VARCHAR(30) NULL DEFAULT NULL,
`HQFacebookId` VARCHAR(30) NULL DEFAULT NULL,
`HQGooglePlusId` VARCHAR(60) NULL DEFAULT NULL,
`HQXingId` VARCHAR(60) NULL DEFAULT NULL,
`HQLinkedInId` VARCHAR(60) NULL DEFAULT NULL,
`HQTwitterId` VARCHAR(60) NULL DEFAULT NULL,
`HQFirmaCreateDatum` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`HQFirmaModifDatum` TIMESTAMP NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`HQFirmaId`),
UNIQUE INDEX `HQFirmenname_UNIQUE` (`HQFirmenname` ASC))
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_general_ci;
CREATE TABLE IF NOT EXISTS `yii_jobmedisearch`.`Filiale` (
`FilialeId` BIGINT(19) UNSIGNED NOT NULL AUTO_INCREMENT,
`Strasse` VARCHAR(60) NOT NULL,
`Postfach` VARCHAR(20) NULL DEFAULT NULL,
`PLZ` VARCHAR(10) NOT NULL,
`Ort` VARCHAR(60) NOT NULL,
`Fax` VARCHAR(15) NULL DEFAULT NULL,
`Telefon` VARCHAR(15) NOT NULL,
`Telefon2` VARCHAR(15) NULL DEFAULT NULL,
`Email` VARCHAR(80) NOT NULL,
`Email2` VARCHAR(80) NULL DEFAULT NULL,
`WebURL` VARCHAR(255) NULL DEFAULT NULL,
`WebURL2` VARCHAR(255) NULL DEFAULT NULL,
`SkypeId` VARCHAR(30) NULL DEFAULT NULL,
`FacebookId` VARCHAR(60) NULL DEFAULT NULL,
`GooglePlusId` VARCHAR(60) NULL DEFAULT NULL,
`XingId` VARCHAR(60) NULL DEFAULT NULL,
`LinkedInId` VARCHAR(60) NULL DEFAULT NULL,
`TwitterId` VARCHAR(60) NULL DEFAULT NULL,
`AnzLikes` MEDIUMINT(9) NULL DEFAULT NULL,
`HQFirma_HQFirmaId` BIGINT(19) UNSIGNED NOT NULL,
`FilialeName` VARCHAR(80) NOT NULL,
`FilialeCreateDatum` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`FilialeModifDatum` TIMESTAMP NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`FilialeId`),
INDEX `fk_Filiale_HQFirma1_idx` (`HQFirma_HQFirmaId` ASC),
UNIQUE INDEX `FilialenName_UNIQUE` (`FilialeName` ASC),
CONSTRAINT `fk_Filiale_HQFirma1`
FOREIGN KEY (`HQFirma_HQFirmaId`)
REFERENCES `yii_jobmedisearch`.`HQFirma` (`HQFirmaId`)
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_general_ci;
CREATE TABLE IF NOT EXISTS `yii_jobmedisearch`.`Promotions` (
`PromoId` BIGINT(19) UNSIGNED NOT NULL AUTO_INCREMENT,
`PromoTyp` ENUM('TopList','Highlighting') NOT NULL,
`PromoBeginn` TIMESTAMP NULL DEFAULT NULL,
`PromoEnd` TIMESTAMP NULL DEFAULT NULL,
`JobFirma_JobId` BIGINT(19) UNSIGNED NOT NULL,
`User_UserId` BIGINT(19) UNSIGNED NOT NULL,
`User_Person_PersId` BIGINT(19) UNSIGNED NOT NULL,
`User_JobFirma_JobId` BIGINT(19) UNSIGNED NOT NULL,
`User_JobFirma_HQFirma_HQFirmaId` BIGINT(19) UNSIGNED NOT NULL,
`User_JobFirma_Filiale_FilialeId` BIGINT(19) UNSIGNED NOT NULL,
`User_JobFirma_Filiale_HQFirma_HQFirmaId` BIGINT(19) UNSIGNED NOT NULL,
`PromoCreationDatum` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`PromoModifDatum` TIMESTAMP NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`PromoId`),
INDEX `fk_Promotions_JobFirma1_idx` (`JobFirma_JobId` ASC),
INDEX `fk_Promotions_User1_idx` (`User_UserId` ASC, `User_Person_PersId` ASC, `User_JobFirma_JobId` ASC, `User_JobFirma_HQFirma_HQFirmaId` ASC, `User_JobFirma_Filiale_FilialeId` ASC, `User_JobFirma_Filiale_HQFirma_HQFirmaId` ASC),
CONSTRAINT `fk_Promotions_JobFirma1`
FOREIGN KEY (`JobFirma_JobId`)
REFERENCES `yii_jobmedisearch`.`JobFirma` (`JobId`)
ON DELETE CASCADE
ON UPDATE NO ACTION,
CONSTRAINT `fk_Promotions_User1`
FOREIGN KEY (`User_UserId` , `User_Person_PersId` , `User_JobFirma_JobId` , `User_JobFirma_HQFirma_HQFirmaId` , `User_JobFirma_Filiale_FilialeId` , `User_JobFirma_Filiale_HQFirma_HQFirmaId`)
REFERENCES `yii_jobmedisearch`.`User` (`UserId` , `Person_PersId` , `JobFirma_JobId` , `JobFirma_HQFirma_HQFirmaId` , `JobFirma_Filiale_FilialeId` , `JobFirma_Filiale_HQFirma_HQFirmaId`)
ON DELETE CASCADE
ON UPDATE NO ACTION)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_general_ci;
CREATE TABLE IF NOT EXISTS `yii_jobmedisearch`.`JobFirma` (
`JobId` BIGINT(19) UNSIGNED NOT NULL,
`JobCreateDatum` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`JobModifDatum` TIMESTAMP NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
`JobPostDatum` TIMESTAMP NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
`JobBeginnDatum` DATE NOT NULL,
`JobExpireDatum` TIMESTAMP NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
`JobTitel` VARCHAR(60) NOT NULL,
`JobBranche` ENUM('Spital','Stellenvermittler','Klinik','Versicherung') NOT NULL,
`JobSynopsis` VARCHAR(255) NULL DEFAULT NULL,
`JobAnforderungen` VARCHAR(2048) NOT NULL,
`JobDescription` VARCHAR(2048) NOT NULL,
`JobAngebot` VARCHAR(2048) NULL DEFAULT NULL,
`JobEduLevel` ENUM('PhD','Master','Lehre','Berufserfahrung') NOT NULL,
`JobEduLevel2` ENUM('PhD','Master','Lehre','Berufserfahrung') NULL DEFAULT NULL,
`JobEduLevel3` ENUM('PhD','Master','Lehre','Berufserfahrung') NULL DEFAULT NULL,
`JobAgeRange` ENUM('18-25','25-35','35-45','45-65','egal') NOT NULL,
`JobNationalitaet` VARCHAR(2) NOT NULL,
`JobPlace` VARCHAR(60) NOT NULL,
`JobTimeModel` ENUM('Teilzeit','Vollzeit','auf Abruf') NOT NULL,
`JobTimePercent` FLOAT(10) UNSIGNED NOT NULL,
`JobContractModel` ENUM('Permanent','Temporär','Freelancer') NOT NULL,
`JobAnzViews` MEDIUMINT(8) UNSIGNED NULL DEFAULT NULL,
`JobAnzBewerbungen` MEDIUMINT(8) UNSIGNED NULL DEFAULT NULL,
`JobAnzStars` MEDIUMINT(8) UNSIGNED NULL DEFAULT NULL,
`JobAnz1Star` FLOAT(10) UNSIGNED NULL DEFAULT NULL,
`JobAnz2Star` FLOAT(10) UNSIGNED NULL DEFAULT NULL,
`JobAnz3Star` FLOAT(10) UNSIGNED NULL DEFAULT NULL,
`JobAnz4Star` FLOAT(10) UNSIGNED NULL DEFAULT NULL,
`JobAnz5Star` FLOAT(10) UNSIGNED NULL DEFAULT NULL,
`JobAnz6Star` FLOAT(10) UNSIGNED NULL DEFAULT NULL,
`JobStars` FLOAT(10) UNSIGNED NULL DEFAULT NULL,
`JobWebURL` VARCHAR(255) NULL DEFAULT NULL,
`JobWebPDF` VARCHAR(255) NULL DEFAULT NULL,
`Filiale_FilialeId` BIGINT(19) UNSIGNED NOT NULL,
`Filiale_HQFirma_HQFirmaId` BIGINT(19) UNSIGNED NOT NULL,
`User_UserId` BIGINT(19) UNSIGNED NOT NULL,
`User_Person_PersId` BIGINT(19) UNSIGNED NOT NULL,
`User_JobFirma_JobId` BIGINT(19) UNSIGNED NOT NULL,
`User_JobFirma_HQFirma_HQFirmaId` BIGINT(19) UNSIGNED NOT NULL,
`User_JobFirma_Filiale_FilialeId` BIGINT(19) UNSIGNED NOT NULL,
`User_JobFirma_Filiale_HQFirma_HQFirmaId` BIGINT(19) UNSIGNED NOT NULL,
PRIMARY KEY (`JobId`),
INDEX `fk_JobFirma_Filiale1_idx` (`Filiale_FilialeId` ASC, `Filiale_HQFirma_HQFirmaId` ASC),
INDEX `fk_JobFirma_User1_idx` (`User_UserId` ASC, `User_Person_PersId` ASC, `User_JobFirma_JobId` ASC, `User_JobFirma_HQFirma_HQFirmaId` ASC, `User_JobFirma_Filiale_FilialeId` ASC, `User_JobFirma_Filiale_HQFirma_HQFirmaId` ASC),
CONSTRAINT `fk_JobFirma_Filiale1`
FOREIGN KEY (`Filiale_FilialeId` , `Filiale_HQFirma_HQFirmaId`)
REFERENCES `yii_jobmedisearch`.`Filiale` (`FilialeId` , `HQFirma_HQFirmaId`)
ON DELETE CASCADE
ON UPDATE NO ACTION,
CONSTRAINT `fk_JobFirma_User1`
FOREIGN KEY (`User_UserId` , `User_Person_PersId` , `User_JobFirma_JobId` , `User_JobFirma_HQFirma_HQFirmaId` , `User_JobFirma_Filiale_FilialeId` , `User_JobFirma_Filiale_HQFirma_HQFirmaId`)
REFERENCES `yii_jobmedisearch`.`User` (`UserId` , `Person_PersId` , `JobFirma_JobId` , `JobFirma_HQFirma_HQFirmaId` , `JobFirma_Filiale_FilialeId` , `JobFirma_Filiale_HQFirma_HQFirmaId`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_general_ci
COMMENT = /* comment truncated */ /* */;
SET SQL_MODE=#OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=#OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=#OLD_UNIQUE_CHECKS;
LATEST FOREIGN KEY ERROR
2014-04-07 15:16:31 ce0 Error in foreign key constraint of table yii_jobmedisearch/user:
there is no index in referenced table which would contain
the columns as the first columns, or the data types in the
referenced table do not match the ones in table. Constraint:
,
CONSTRAINT "fk_User_Filiale1" FOREIGN KEY ("Filiale_FilialeId", "Filiale_HQFirma_HQFirmaId") REFERENCES "filiale" ("FilialeId", "HQFirma_HQFirmaId") ON DELETE NO ACTION ON UPDATE NO ACTION
The index in the foreign key in table is "fk_User_Filiale1_idx"
See http://dev.mysql.com/doc/refman/5.6/en/innodb-foreign-key-constraints.html for correct foreign key definition.

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 |

Error Code: 1005. Can't create table `v2_financieel`.`facturen_azie` (errno: 150 "Foreign key constraint is incorrectly formed")

I am using workbench and I am trying to create a database with a table:
I just put this create database in the sql editor and execute it.
Of course I googled first for the error. But I cant find anything for this specific case.
-- Dumping database structure for v2_financieel
CREATE DATABASE IF NOT EXISTS `v2_financieel` /*!40100 DEFAULT CHARACTER SET utf8 */;
USE `v2_financieel`;
-- Dumping structure for table v2_financieel.facturen_azie
CREATE TABLE IF NOT EXISTS `facturen_azie` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`kantoor_id` int(10) unsigned NOT NULL,
`factuurnummer` varchar(10) COLLATE utf8_unicode_ci NOT NULL,
`factuurdatum` date NOT NULL,
`merknaam_id` int(10) unsigned NOT NULL,
`klant_id` int(10) unsigned NOT NULL,
`omschrijving` varchar(191) COLLATE utf8_unicode_ci NOT NULL,
`aantal_afslagen` decimal(10,1) unsigned NOT NULL,
`bonnummers` varchar(191) COLLATE utf8_unicode_ci DEFAULT NULL,
`valuta` char(3) COLLATE utf8_unicode_ci NOT NULL DEFAULT 'EUR',
`koers` decimal(8,4) unsigned NOT NULL,
`koersdatum` date NOT NULL,
`bedrag_euro` decimal(10,2) NOT NULL,
`bedrag_valuta` decimal(10,2) NOT NULL,
`btw` decimal(10,2) NOT NULL,
`factuurbedrag` decimal(10,2) NOT NULL,
`betaald` tinyint(1) NOT NULL DEFAULT '0',
`extra_bedrag` decimal(10,2) NOT NULL,
`extra_omschrijving` varchar(191) COLLATE utf8_unicode_ci DEFAULT NULL,
`creditfactuur` tinyint(1) NOT NULL DEFAULT '0',
`credit_id` int(11) NOT NULL DEFAULT '0',
`betalingsdatum` date NOT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `facturen_azie_factuurnummer_unique` (`factuurnummer`),
KEY `facturen_azie_merknaam_id_foreign` (`merknaam_id`),
KEY `facturen_azie_klant_id_foreign` (`klant_id`),
KEY `facturen_azie_kantoor_id_foreign` (`kantoor_id`),
FULLTEXT KEY `search_azie` (`factuurnummer`,`omschrijving`),
CONSTRAINT `facturen_azie_kantoor_id_foreign` FOREIGN KEY (`kantoor_id`) REFERENCES `kantoren` (`id`),
CONSTRAINT `facturen_azie_klant_id_foreign` FOREIGN KEY (`klant_id`) REFERENCES `klanten` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=496 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
But I get this errror:
12:02:40 CREATE TABLE IF NOT EXISTS `facturen_azie` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `kantoor_id` int(10) unsigned NOT NULL, `factuurnummer` varchar(10) COLLATE utf8_unicode_ci NOT NULL, `factuurdatum` date NOT NULL, `merknaam_id` int(10) unsigned NOT NULL, `klant_id` int(10) unsigned NOT NULL, `omschrijving` varchar(191) COLLATE utf8_unicode_ci NOT NULL, `aantal_afslagen` decimal(10,1) unsigned NOT NULL, `bonnummers` varchar(191) COLLATE utf8_unicode_ci DEFAULT NULL, `valuta` char(3) COLLATE utf8_unicode_ci NOT NULL DEFAULT 'EUR', `koers` decimal(8,4) unsigned NOT NULL, `koersdatum` date NOT NULL, `bedrag_euro` decimal(10,2) NOT NULL, `bedrag_valuta` decimal(10,2) NOT NULL, `btw` decimal(10,2) NOT NULL, `factuurbedrag` decimal(10,2) NOT NULL, `betaald` tinyint(1) NOT NULL DEFAULT '0', `extra_bedrag` decimal(10,2) NOT NULL, `extra_omschrijving` varchar(191) COLLATE utf8_unicode_ci DEFAULT NULL, `creditfactuur` tinyint(1) NOT NULL DEFAULT '0', `credit_id` int(11) NOT NULL DEFAULT '0', `betalingsdatum` date NOT NULL, `created_at` timestamp NULL DEFAULT NULL, `updated_at` timestamp NULL DEFAULT NULL, PRIMARY KEY (`id`), UNIQUE KEY `facturen_azie_factuurnummer_unique` (`factuurnummer`), KEY `facturen_azie_merknaam_id_foreign` (`merknaam_id`), KEY `facturen_azie_klant_id_foreign` (`klant_id`), KEY `facturen_azie_kantoor_id_foreign` (`kantoor_id`), FULLTEXT KEY `search_azie` (`factuurnummer`,`omschrijving`), CONSTRAINT `facturen_azie_kantoor_id_foreign` FOREIGN KEY (`kantoor_id`) REFERENCES `kantoren` (`id`), CONSTRAINT `facturen_azie_klant_id_foreign` FOREIGN KEY (`klant_id`) REFERENCES `klanten` (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=496 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci Error Code: 1005. Can't create table `v2_financieel`.`facturen_azie` (errno: 150 "Foreign key constraint is incorrectly formed") 0.359 sec

mysql 'slow queries log' return a huge number of rows examined but explain seems ok

The sql query
# Query_time: 16.536276 Lock_time: 0.000159 Rows_sent: 756 Rows_examined: 8392194
SET timestamp=1555422526;
SELECT c.id AS c__id, c.company_id AS c__company_id,
c.ordinary_price AS c__ordinary_price, c.nights AS c__nights,
c.food_type AS c__food_type, c.period AS c__period,
c.period_desc AS c__period_desc, c.extra AS c__extra,
c.coupons_bought AS c__coupons_bought, c.coupon_price AS c__coupon_price,
c.coordinates AS c__coordinates, c.best_price AS c__best_price,
c.from_price AS c__from_price, c.end_datetime AS c__end_datetime,
c.hide_clock AS c__hide_clock, c.hide_discount AS c__hide_discount,
c.booking_hotel_id AS c__booking_hotel_id, c.title AS c__title,
c.option_people AS c__option_people, c.option_room AS c__option_room,
c.option_period AS c__option_period, c.city AS c__city,
c2.id AS c2__id, c2.people AS c2__people, c2.room AS c2__room,
( SELECT c8.url AS c8__url
FROM campaign_images c8
WHERE (c8.campaign_id = c.id
AND c8.photo_type = 'list')
ORDER BY c8.ordering ASC LIMIT 1
) AS c__0
FROM campaign c
LEFT JOIN campaign_options c2 ON c.id = c2.campaign_id
AND (c2.active = 1)
LEFT JOIN city_in_campaign c3 ON c.id = c3.campaign_id
LEFT JOIN city c4 ON c3.city_id = c4.id
LEFT JOIN company c5 ON c.company_id = c5.id
LEFT JOIN campaign_in_category c6 ON c.id = c6.campaign_id
LEFT JOIN campaign_in_group c7 ON c.id = c7.campaign_id
WHERE c.id IN ('13308', '13281', '13265')
AND (c.status IN ('published')
AND c.start_datetime <= NOW()
AND c.end_datetime >= NOW()
AND c5.id = '2111'
AND c.id != '14624'
AND (c7.group_id in (1)
OR c7.group_id is NULL
)
)
ORDER BY c.coupon_expire_datetime ASC;
create table campaign
CREATE TABLE `campaign` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL,
`company_id` int(11) DEFAULT NULL,
`source_id` int(11) DEFAULT NULL,
`secondary_source_id` int(11) DEFAULT NULL,
`source_hotel_id` int(11) DEFAULT NULL,
`booking_hotel_id` int(11) DEFAULT NULL,
`booking_board_id` int(11) DEFAULT NULL,
`booking_rate` varchar(100) DEFAULT NULL,
`ordinary_price` decimal(9,2) NOT NULL,
`best_price` tinyint(1) NOT NULL DEFAULT '0',
`from_price` tinyint(1) NOT NULL DEFAULT '0',
`status` varchar(10) DEFAULT 'draft',
`type` varchar(20) DEFAULT 'other',
`deal_status` varchar(20) DEFAULT 'in_progress',
`coupon_price` decimal(9,2) NOT NULL,
`cosmote_discount` decimal(5,2) DEFAULT NULL,
`nights` int(11) NOT NULL DEFAULT '0',
`period` varchar(255) DEFAULT NULL,
`period_desc` varchar(1000) DEFAULT NULL,
`hide_period` tinyint(1) NOT NULL DEFAULT '0',
`food_type` varchar(100) DEFAULT NULL,
`stars` varchar(100) DEFAULT NULL,
`adults` tinyint(1) DEFAULT NULL,
`childs` tinyint(1) DEFAULT NULL,
`extra` varchar(255) DEFAULT NULL,
`best_point` varchar(511) DEFAULT NULL,
`extra_night_price` decimal(9,2) NOT NULL DEFAULT '0.00',
`high_season_price` decimal(9,2) NOT NULL DEFAULT '0.00',
`high_season_desc` varchar(500) DEFAULT NULL,
`high_season_extra_night_price` decimal(9,2) NOT NULL DEFAULT '0.00',
`family_packages_desc` varchar(1000) DEFAULT NULL,
`coordinates` varchar(70) DEFAULT NULL,
`start_datetime` datetime DEFAULT NULL,
`end_datetime` datetime DEFAULT NULL,
`coupon_expire_datetime` datetime DEFAULT NULL,
`active` tinyint(4) NOT NULL DEFAULT '1',
`city` varchar(255) DEFAULT NULL,
`min_coupons` int(11) NOT NULL DEFAULT '0',
`is_global` tinyint(4) NOT NULL DEFAULT '0',
`hide_clock` tinyint(1) NOT NULL DEFAULT '0',
`hide_discount` tinyint(1) NOT NULL DEFAULT '0',
`hide_purchases` tinyint(1) NOT NULL DEFAULT '0',
`booking_enabled` tinyint(1) NOT NULL DEFAULT '0',
`booking_phone` varchar(50) DEFAULT NULL,
`refresh` tinyint(1) NOT NULL DEFAULT '0',
`installments` tinyint(1) NOT NULL DEFAULT '1',
`receipt` enum('0','1') NOT NULL DEFAULT '0',
`newsletters_sent` tinyint(4) NOT NULL DEFAULT '0',
`max_coupons` int(11) NOT NULL DEFAULT '0',
`max_coupons_per_user` int(11) DEFAULT NULL,
`coupons_bought` int(11) unsigned NOT NULL DEFAULT '0',
`fake_orders` int(11) DEFAULT '0',
`title` varchar(255) DEFAULT NULL,
`newsletter_title` varchar(500) DEFAULT NULL,
`linkwise_title` varchar(500) DEFAULT NULL,
`option_title` varchar(255) DEFAULT NULL,
`option_title_en` varchar(255) DEFAULT NULL,
`option_people` varchar(150) DEFAULT NULL,
`option_room` varchar(150) DEFAULT NULL,
`option_period` varchar(150) DEFAULT NULL,
`name` varchar(1200) DEFAULT NULL,
`description` text,
`highlights` text,
`coupon_instructions` text,
`show_in_recent_deals` tinyint(4) NOT NULL DEFAULT '1',
`youtube_video_id` varchar(100) DEFAULT NULL,
`in_side` tinyint(4) NOT NULL DEFAULT '0',
`family` tinyint(1) NOT NULL DEFAULT '0',
`send_newsletter` tinyint(4) NOT NULL DEFAULT '1',
`resend_newsletter` tinyint(4) NOT NULL DEFAULT '0',
`modified_datetime` timestamp NULL DEFAULT NULL,
`created_datetime` datetime NOT NULL,
`ordering` int(11) unsigned DEFAULT '0',
PRIMARY KEY (`id`),
KEY `company_id_idx` (`company_id`),
KEY `user_id_idx` (`user_id`),
KEY `status_indx` (`status`),
KEY `str_dt_indx` (`start_datetime`),
KEY `end_dt_indx` (`end_datetime`),
KEY `side_indx` (`in_side`),
KEY `ord_indx` (`ordering`),
KEY `global_indx` (`is_global`),
KEY `act_indx` (`active`),
KEY `coup_expr_index` (`coupon_expire_datetime`)
) ENGINE=InnoDB AUTO_INCREMENT=14788 DEFAULT CHARSET=utf8
create table campaign_options
CREATE TABLE `campaign_options` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`campaign_id` int(11) NOT NULL,
`coupons_bought` int(11) unsigned NOT NULL DEFAULT '0',
`name` varchar(255) NOT NULL,
`name_en` varchar(255) DEFAULT NULL,
`people` varchar(100) DEFAULT NULL,
`room` varchar(100) DEFAULT NULL,
`food` varchar(100) DEFAULT NULL,
`period` varchar(100) DEFAULT NULL,
`coupon_price` decimal(9,2) NOT NULL,
`extra_night_price` decimal(9,2) NOT NULL DEFAULT '0.00',
`high_season_price` decimal(9,2) DEFAULT '0.00',
`high_season_extra_night_price` decimal(9,2) NOT NULL DEFAULT '0.00',
`modified_datetime` timestamp NULL DEFAULT NULL,
`created_datetime` datetime NOT NULL,
`main` tinyint(1) NOT NULL DEFAULT '0',
`family` tinyint(1) NOT NULL DEFAULT '0',
`active` tinyint(4) NOT NULL DEFAULT '1',
PRIMARY KEY (`id`),
KEY `campaign_id_idx` (`campaign_id`),
KEY `active_indx` (`active`),
KEY `family_indx` (`family`),
KEY `main_indx` (`main`)
) ENGINE=InnoDB AUTO_INCREMENT=48990 DEFAULT CHARSET=utf8
create table city_in_campaign
CREATE TABLE `city_in_campaign` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`campaign_id` int(11) DEFAULT NULL,
`city_id` int(11) DEFAULT NULL,
`ordering` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
KEY `campaign_id_idx` (`campaign_id`),
KEY `city_id_idx` (`city_id`),
KEY `order_indx` (`ordering`)
) ENGINE=InnoDB AUTO_INCREMENT=227176 DEFAULT CHARSET=utf8
create table city
CREATE TABLE `city` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`country_id` int(11) DEFAULT NULL,
`name` varchar(100) DEFAULT NULL,
`is_active` tinyint(3) unsigned DEFAULT NULL,
`modified_datetime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
KEY `country_id_idx` (`country_id`),
KEY `is_active_indx` (`is_active`)
) ENGINE=InnoDB AUTO_INCREMENT=254 DEFAULT CHARSET=utf8
create table company
CREATE TABLE `company` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`ref_id` int(11) DEFAULT NULL,
`name` varchar(100) DEFAULT NULL,
`description` text,
`logo` varchar(255) DEFAULT NULL,
`phone_number` varchar(100) DEFAULT NULL,
`address` varchar(200) DEFAULT NULL,
`coordinates` varchar(70) DEFAULT NULL,
`email` varchar(100) DEFAULT NULL,
`website` varchar(255) DEFAULT NULL,
`skype_name` varchar(50) DEFAULT NULL,
`icq_number` varchar(255) DEFAULT NULL,
`payment_information` text,
`extra1` text,
`extra2` text,
`extra3` text,
`video` varchar(500) DEFAULT NULL,
`checked` tinyint(1) NOT NULL DEFAULT '0',
`ordering` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
KEY `checked_indx` (`checked`),
KEY `ordering_indx` (`ordering`)
) ENGINE=InnoDB AUTO_INCREMENT=2519 DEFAULT CHARSET=utf8
create table campaign_in_category
CREATE TABLE `campaign_in_category` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`campaign_id` int(11) NOT NULL,
`category_id` int(11) DEFAULT NULL,
`ordering` int(11) NOT NULL DEFAULT '0',
`created_datetime` datetime NOT NULL,
PRIMARY KEY (`id`),
KEY `campaign_id_idx` (`campaign_id`),
KEY `category_id_idx` (`category_id`),
KEY `order_indx` (`ordering`)
) ENGINE=InnoDB AUTO_INCREMENT=457080 DEFAULT CHARSET=utf8
create table campaign_in_group
CREATE TABLE `campaign_in_group` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`campaign_id` int(11) DEFAULT NULL,
`group_id` int(11) DEFAULT NULL,
`created_datetime` datetime DEFAULT NULL,
`modified_datetime` datetime DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `camp_group_indx` (`campaign_id`,`group_id`)
) ENGINE=InnoDB AUTO_INCREMENT=1175 DEFAULT CHARSET=utf8
campaign total rows: 12,657
campaign_options total rows: 43,714
city_in_campaign total rows: 15,162
city total rows: 215
company total rows: 1,756
campaign_in_category total rows: 38,817
campaign_in_group total rows: 395
explain of the query
which ones have index
it looks you need composite index because file sorting.
usage:
CREATE INDEX index_name
ON table_name(c2,c3,c4);
You are getting above result because some time o/p of explain gives different result as response of query of prod.
For optimization of query you can't have a fix answer. it varies from case to case. As for this situation I think you need to perform performance testing of this query. You can do this by inserting some records in all referenced table and then checking it's performance using explain.
LEFT JOIN campaign_in_category c6 ON c.id = c6.campaign_id
seems to be totally useless. But the Optimizer may not realize it. Remove it and any other dead code.

#1215 - Cannot add foreign key constraint

I am trying to migrate my osclass installation to another server. I have copied all files and created a new database. When trying to import my database from backup, I get "#1215 - Cannot add foreign key constraint".
It shows that this bit is a problem:
--
-- Table structure for table `oc_t_user`
--
CREATE TABLE IF NOT EXISTS `oc_t_user` (
`pk_i_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`dt_reg_date` datetime NOT NULL,
`dt_mod_date` datetime DEFAULT NULL,
`s_name` varchar(100) NOT NULL,
`s_username` varchar(100) NOT NULL,
`s_password` char(60) NOT NULL,
`s_secret` varchar(40) DEFAULT NULL,
`s_email` varchar(100) NOT NULL,
`s_website` varchar(100) DEFAULT NULL,
`s_phone_land` varchar(45) DEFAULT NULL,
`s_phone_mobile` varchar(45) DEFAULT NULL,
`b_enabled` tinyint(1) NOT NULL DEFAULT '1',
`b_active` tinyint(1) NOT NULL DEFAULT '0',
`s_pass_code` varchar(100) DEFAULT NULL,
`s_pass_date` datetime DEFAULT NULL,
`s_pass_ip` varchar(15) DEFAULT NULL,
`fk_c_country_code` char(2) DEFAULT NULL,
`s_country` varchar(40) DEFAULT NULL,
`s_address` varchar(100) DEFAULT NULL,
`s_zip` varchar(15) DEFAULT NULL,
`fk_i_region_id` int(10) unsigned DEFAULT NULL,
`s_region` varchar(100) DEFAULT NULL,
`fk_i_city_id` int(10) unsigned DEFAULT NULL,
`s_city` varchar(100) DEFAULT NULL,
`fk_i_city_area_id` int(10) unsigned DEFAULT NULL,
`s_city_area` varchar(200) DEFAULT NULL,
`d_coord_lat` decimal(10,6) DEFAULT NULL,
`d_coord_long` decimal(10,6) DEFAULT NULL,
`b_company` tinyint(1) NOT NULL DEFAULT '0',
`i_items` int(10) unsigned DEFAULT '0',
`i_comments` int(10) unsigned DEFAULT '0',
`dt_access_date` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`s_access_ip` varchar(15) NOT NULL DEFAULT '',
PRIMARY KEY (`pk_i_id`),
UNIQUE KEY `s_email` (`s_email`),
KEY `idx_s_name` (`s_name`(6)),
KEY `idx_s_username` (`s_username`),
KEY `fk_c_country_code` (`fk_c_country_code`),
KEY `fk_i_region_id` (`fk_i_region_id`),
KEY `fk_i_city_id` (`fk_i_city_id`),
KEY `fk_i_city_area_id` (`fk_i_city_area_id`),
CONSTRAINT `oc_t_user_ibfk_1` FOREIGN KEY (`fk_c_country_code`) REFERENCES `oc_t_country` (`pk_c_code`),
CONSTRAINT `oc_t_user_ibfk_2` FOREIGN KEY (`fk_i_region_id`) REFERENCES `oc_t_region` (`pk_i_id`),
CONSTRAINT `oc_t_user_ibfk_3` FOREIGN KEY (`fk_i_city_id`) REFERENCES `oc_t_city` (`pk_i_id`),
CONSTRAINT `oc_t_user_ibfk_4` FOREIGN KEY (`fk_i_city_area_id`) REFERENCES `oc_t_city_area` (`pk_i_id`)
) ENGINE=InnoDB AUTO_INCREMENT=81 DEFAULT CHARSET=utf8;
Please help.
You must create first the tables you want to reference, for example, here is one foreing key that reference one table, also the type of the key must match, follow this example an add the rest of your tables, I will show a minimum example that works in the creation of the tables, you must add the correct data of course:
CREATE TABLE IF NOT EXISTS `oc_t_country` (
`pk_c_code` int(10) unsigned NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`pk_c_code`)
) ENGINE=InnoDB AUTO_INCREMENT=81 DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `oc_t_region` (
`pk_i_id_region` int(10) unsigned NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`pk_i_id_region`)
) ENGINE=InnoDB AUTO_INCREMENT=81 DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `oc_t_city` (
`pk_i_id_city` int(10) unsigned NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`pk_i_id_city`)
) ENGINE=InnoDB AUTO_INCREMENT=81 DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `oc_t_city_area` (
`pk_i_id_area` int(10) unsigned NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`pk_i_id_area`)
) ENGINE=InnoDB AUTO_INCREMENT=81 DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `oc_t_user` (
`pk_i_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`dt_reg_date` datetime NOT NULL,
`dt_mod_date` datetime DEFAULT NULL,
`s_name` varchar(100) NOT NULL,
`s_username` varchar(100) NOT NULL,
`s_password` char(60) NOT NULL,
`s_secret` varchar(40) DEFAULT NULL,
`s_email` varchar(100) NOT NULL,
`s_website` varchar(100) DEFAULT NULL,
`s_phone_land` varchar(45) DEFAULT NULL,
`s_phone_mobile` varchar(45) DEFAULT NULL,
`b_enabled` tinyint(1) NOT NULL DEFAULT '1',
`b_active` tinyint(1) NOT NULL DEFAULT '0',
`s_pass_code` varchar(100) DEFAULT NULL,
`s_pass_date` datetime DEFAULT NULL,
`s_pass_ip` varchar(15) DEFAULT NULL,
`fk_c_country_code` int(10) unsigned DEFAULT NULL,
`s_country` varchar(40) DEFAULT NULL,
`s_address` varchar(100) DEFAULT NULL,
`s_zip` varchar(15) DEFAULT NULL,
`fk_i_region_id` int(10) unsigned DEFAULT NULL,
`s_region` varchar(100) DEFAULT NULL,
`fk_i_city_id` int(10) unsigned DEFAULT NULL,
`s_city` varchar(100) DEFAULT NULL,
`fk_i_city_area_id` int(10) unsigned DEFAULT NULL,
`s_city_area` varchar(200) DEFAULT NULL,
`d_coord_lat` decimal(10,6) DEFAULT NULL,
`d_coord_long` decimal(10,6) DEFAULT NULL,
`b_company` tinyint(1) NOT NULL DEFAULT '0',
`i_items` int(10) unsigned DEFAULT '0',
`i_comments` int(10) unsigned DEFAULT '0',
`dt_access_date` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`s_access_ip` varchar(15) NOT NULL DEFAULT '',
PRIMARY KEY (`pk_i_id`),
UNIQUE KEY `s_email` (`s_email`),
KEY `idx_s_name` (`s_name`(6)),
KEY `idx_s_username` (`s_username`),
KEY `fk_c_country_code` (`fk_c_country_code`),
KEY `fk_i_region_id` (`fk_i_region_id`),
KEY `fk_i_city_id` (`fk_i_city_id`),
KEY `fk_i_city_area_id` (`fk_i_city_area_id`),
CONSTRAINT `oc_t_user_ibfk_1` FOREIGN KEY (`fk_c_country_code`) REFERENCES `oc_t_country` (`pk_c_code`),
CONSTRAINT `oc_t_user_ibfk_2` FOREIGN KEY (`fk_i_region_id`) REFERENCES `oc_t_region` (`pk_i_id_region`),
CONSTRAINT `oc_t_user_ibfk_3` FOREIGN KEY (`fk_i_city_id`) REFERENCES `oc_t_city` (`pk_i_id_city`),
CONSTRAINT `oc_t_user_ibfk_4` FOREIGN KEY (`fk_i_city_area_id`) REFERENCES `oc_t_city_area` (`pk_i_id_area`)
) ENGINE=InnoDB AUTO_INCREMENT=81 DEFAULT CHARSET=utf8;

Mysql error 1215 - Cannot add foreign key constraint

Created these two tables successfully
First table
CREATE TABLE IF NOT EXISTS `lawncare_user` (
`ID` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
`FirstName` varchar(255) NOT NULL,
`LastName` varchar(255) NOT NULL,
`Email` varchar(255) NOT NULL,
`UserType` varchar(30) NOT NULL,
`UserName` varchar(255) NOT NULL,
`Password` varchar(255) NOT NULL,
`AddedBy` int(11) NOT NULL,
`AddedOn` date NOT NULL,
`ModifiedOn` date DEFAULT NULL,
`Status` BOOLEAN NOT NULL DEFAULT '0',
`QuestionID` int(11) DEFAULT NULL,
`QuestionAnswer` text DEFAULT NULL,
`Params` text NOT NULL,
`Address` text NOT NULL,
`Country` varchar(300) NOT NULL,
`State` varchar(300) NOT NULL,
`City` varchar(300) NOT NULL,
`ContactNo` double DEFAULT NULL,
`Activation` BOOLEAN NOT NULL DEFAULT '0',
`ActivatedOn` date DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Second table
CREATE TABLE IF NOT EXISTS `lawncare_customer` (
`ID` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
`FirstName` varchar(255) NOT NULL,
`LastName` varchar(255) NOT NULL,
`Email` varchar(255) NOT NULL,
`Password` varchar(255) NOT NULL,
`ContactNo` varchar(20) NOT NULL,
`Address` varchar(255) NOT NULL,
`Params` text NOT NULL,
`Province` varchar(255) NOT NULL,
`ZipCode` varchar(255) NOT NULL,
`Status` Boolean NOT NULL DEFAULT '0',
`AddedBy` int(11) NOT NULL,
`AddedOn` date NOT NULL,
`ModifiedOn` date DEFAULT NULL
) ENGINE =InnoDB DEFAULT CHARSET=latin1;
But while creating third table as
CREATE TABLE IF NOT EXISTS `lawncare_message` (
`ID` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
`Reason` int(5) NOT NULL,
`Subject` text NOT NULL,
`Description` text NOT NULL,
`Customer` int(11) NOT NULL,
`CustomerUser` varchar(255) NOT NULL,
`CustomerEmail` varchar(255) NOT NULL,
`SendTo` int(11) NOT NULL,
`SendToUser` varchar(255) NOT NULL,
`SendToEmail` varchar(255) NOT NULL,
`Status` int(5) NOT NULL DEFAULT '0',
`AddedBy` int(11) NOT NULL,
`AddedOn` date NOT NULL
FOREIGN KEY (SendTo, SendToUser, SendToEmail)
REFERENCES lawncare_user(ID, UserName, Email)
ON UPDATE CASCADE ,
FOREIGN KEY (Customer, CustomerUser, CustomerEmail)
REFERENCES lawncare_customer(ID, FirstName,Email)
ON UPDATE CASCADE
) ENGINE =InnoDB DEFAULT CHARSET=latin1
I get #1215 - Cannot add foreign key constraint , error in mysql tried adding foreign keys after creating table but it still gives the same error. I don't know what I'm doing wrong here.
First of all Check whether you have applied indexes on the keys.
As per your code their is no point in referencing id,UserName and Email.
Only id is enough for referencing.
Check the following code
CREATE TABLE IF NOT EXISTS `lawncare_message` (
`ID` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
`Reason` int(5) NOT NULL,
`Subject` text NOT NULL,
`Description` text NOT NULL,
`Customer` int(11) NOT NULL,
`CustomerUser` varchar(255) NOT NULL,
`CustomerEmail` varchar(255) NOT NULL,
`SendTo` int(11) NOT NULL,
`SendToUser` varchar(255) NOT NULL,
`SendToEmail` varchar(255) NOT NULL,
`Status` int(5) NOT NULL DEFAULT '0',
`AddedBy` int(11) NOT NULL,
`AddedOn` date NOT NULL,
FOREIGN KEY (SendTo)
REFERENCES lawncare_user(ID)
ON UPDATE CASCADE ,
FOREIGN KEY (Customer)
REFERENCES lawncare_customer(ID)
ON UPDATE CASCADE
) ENGINE =InnoDB DEFAULT CHARSET=latin1