#1215 - Cannot add foreign key constraint - mysql

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;

Related

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

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`)
)

MySQL insert using transaction

I have following structure on mysql database:
sqlfiddle
What I want to do is:
To select DISTINCT industry from Company table
To insert into Industry table first and get auto incremented ID
With this ID to insert again into IndustryTranslation table and set "language"="en"
To insert Company's id and newly generated Industry's id into MapCompanyIndustry table
I know that it's not possible with one statement. But definitely it's possible with transaction. Can't figure out how to achieve this result with one transaction.
Any suggestions?
Schema
CREATE TABLE `Industry` (
`id` int(4) unsigned NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `IndustryTranslation` (
`industryID` int(4) unsigned NOT NULL,
`language` varchar(5) NOT NULL,
`name` varchar(255) NOT NULL,
`confirmed` tinyint(1) DEFAULT '0',
PRIMARY KEY (`industryID`,`language`),
KEY `language` (`language`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `Company` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`imageUri` varchar(255) DEFAULT NULL,
`countryID` int(3) unsigned DEFAULT NULL,
`phone` varchar(255) DEFAULT NULL,
`email` varchar(255) DEFAULT NULL,
`verified` tinyint(1) DEFAULT NULL,
`industry` varchar(255) DEFAULT NULL,
`headquarters` varchar(255) DEFAULT NULL,
`uri` varchar(255) DEFAULT NULL,
`createdAt` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
`updatedAt` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
KEY `countryID` (`countryID`)
) ENGINE=InnoDB AUTO_INCREMENT=4004 DEFAULT CHARSET=utf8;
CREATE TABLE `MapCompanyIndustry` (
`companyID` int(10) unsigned NOT NULL,
`industryID` int(4) unsigned NOT NULL,
PRIMARY KEY (`companyID`,`industryID`),
KEY `industryID` (`industryID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

How to check if a given data exists in onther table MySql?

I have two tables in MySql Database:
Captain(captain.email)
Members(member.email)
I want when captain table insert data in captain.email then check If members table data in members.email are already exit then data in captain.email not insert in captain table.
How it is possible ?
1.Captain :
CREATE TABLE `captain` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(255) NOT NULL,
`username_canonical` varchar(255) NOT NULL,
`email` varchar(255) NOT NULL,
`email_canonical` varchar(255) NOT NULL,
`enabled` tinyint(1) NOT NULL,
`salt` varchar(255) NOT NULL,
`password` varchar(255) NOT NULL,
`last_login` datetime DEFAULT NULL,
`locked` tinyint(1) NOT NULL,
`expired` tinyint(1) NOT NULL,
`expires_at` datetime DEFAULT NULL,
`confirmation_token` varchar(255) DEFAULT NULL,
`password_requested_at` datetime DEFAULT NULL,
`roles` longtext NOT NULL COMMENT '(DC2Type:array)',
`credentials_expired` tinyint(1) NOT NULL,
`credentials_expire_at` datetime DEFAULT NULL,
`name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `UNIQ_957A647992FC23A8` (`username_canonical`),
UNIQUE KEY `UNIQ_957A6479A0D96FBF` (`email_canonical`)
)
2.Members :
CREATE TABLE `members` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`team_id` int(11) DEFAULT NULL,
`fos_user_id` int(11) DEFAULT NULL,
`name` varchar(45) DEFAULT NULL,
`email` varchar(45) DEFAULT NULL,
`mobile` varchar(45) DEFAULT NULL,
`role` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `email` (`email`),
UNIQUE KEY `email_2` (`email`),
KEY `IDX_45A0D2FF296CD8AE` (`team_id`),
KEY `IDX_45A0D2FF8C20A0FB` (`fos_user_id`),
CONSTRAINT `FK_45A0D2FF296CD8AE` FOREIGN KEY (`team_id`) REFERENCES `team` (`id`),
CONSTRAINT `FK_45A0D2FF8C20A0FB` FOREIGN KEY (`fos_user_id`) REFERENCES `fos_user` (`id`)
)
There is no way to enforce such constraint.
Using declarative referential integrity (DRI) you could create a table that contains all of the columns that you need to build a unique key on.

Error during installing dump

I have the following dump:
CREATE TABLE `testdata`.`carer` (
`ID` bigint(20) NOT NULL auto_increment,
`IS_DELETED` bit(1) default NULL,
`name` varchar(255) default NULL,
`account_id` bigint(20) NOT NULL,
`patient_carer_id` bigint(20) NOT NULL,
PRIMARY KEY (`ID`),
KEY `FK5A0E781D4C45C51` (`patient_carer_id`),
KEY `FK5A0E7818BCEF0FB` (`account_id`),
CONSTRAINT `FK5A0E781D4C45C51` FOREIGN KEY (`patient_carer_id`) REFERENCES `patients` (`ID`),
CONSTRAINT `FK5A0E7818BCEF0FB` FOREIGN KEY (`account_ID`) REFERENCES `account` (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=55 DEFAULT CHARSET=utf8
but as result i see the following:
ERROR 1061 (42000): Duplicate key name 'FK5A0E7818BCEF0FB'
Yep, key's name and constrain's name are same. But with other key we haven't any problems.
But if I'll change KEY FK5A0E7818BCEF0FB to FK5A0E7818BCEF0FB1 , it will be working.
for more information, dumps of two other tables:
CREATE TABLE `testdata`.`account` (
`ID` bigint(20) NOT NULL auto_increment,
`IS_DELETED` bit(1) default NULL,
`name` varchar(255) NOT NULL,
`template` varchar(255) default NULL,
`logoCache` bigint(20) default NULL,
`suspended` bit(1) default NULL,
`contactPerson` varchar(255) default NULL,
`contactPersonPosition` varchar(255) default NULL,
`clearLogo` bit(1) default NULL,
`scheduling_settings_id` bigint(20) default NULL,
`billing_id` bigint(20) default NULL,
`settingsId` bigint(20) default NULL,
`geocodingProvider_ID` bigint(20) default NULL,
`mapProvider_ID` bigint(20) default NULL,
`salesChannel_ID` bigint(20) default NULL,
`available_time_id` bigint(20) default NULL,
PRIMARY KEY (`ID`),
UNIQUE KEY `scheduling_settings_id` (`scheduling_settings_id`),
UNIQUE KEY `billing_id` (`billing_id`),
UNIQUE KEY `settingsId` (`settingsId`),
KEY `FKB9D38A2DBF9BE64A` (`mapProvider_ID`),
KEY `FKB9D38A2D64C6436C` (`billing_id`),
KEY `FKB9D38A2DBAC9B04B` (`geocodingProvider_ID`),
KEY `FKB9D38A2D641A10EA` (`available_time_id`),
KEY `FKB9D38A2D7D7F6D28` (`settingsId`),
KEY `FKB9D38A2D44D2DE01` (`scheduling_settings_id`),
KEY `FKB9D38A2D9A025321` (`salesChannel_ID`)
) ENGINE=InnoDB AUTO_INCREMENT=26 DEFAULT CHARSET=utf8
CREATE TABLE `testdata`.`patients` (
`ID` bigint(20) NOT NULL auto_increment,
`IS_DELETED` bit(1) default NULL,
`gpName` varchar(255) default NULL,
`title` varchar(255) default NULL,
`firstName` varchar(255) NOT NULL,
`lastName` varchar(255) NOT NULL,
`nhsNumber` varchar(255) NOT NULL,
`sex` varchar(255) default NULL,
`dateOfBirth` datetime default NULL,
`weight` varchar(255) default NULL,
`notificationMethod` varchar(255) default NULL,
`ethnicity_id` bigint(20) default NULL,
`mobilityCode_ID` bigint(20) default NULL,
`homeLocation_ID` bigint(20) default NULL,
`account_id` bigint(20) NOT NULL,
`original_patient_id` bigint(20) default NULL,
PRIMARY KEY (`ID`),
KEY `FK49A9760E511FA9D3` (`ethnicity_id`),
KEY `FK49A9760E8BCEF0FB` (`account_id`),
KEY `FK49A9760EC7B193C1` (`mobilityCode_ID`),
KEY `FK49A9760EC0EFD76E` (`homeLocation_ID`),
KEY `FK49A9760ED05CD81` (`original_patient_id`)
) ENGINE=InnoDB AUTO_INCREMENT=2434 DEFAULT CHARSET=utf8
You're hitting mysql bugs 39932 and 45307.
Instead of
CONSTRAINT `FK5A0E7818BCEF0FB` FOREIGN KEY (`account_ID`) REFERENCES `account` (`ID`)
try
CONSTRAINT `FK5A0E7818BCEF0FB` FOREIGN KEY (`account_id`) REFERENCES `account` (`ID`)
Note the change of case of account_id.