How to check if a given data exists in onther table MySql? - 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.

Related

MY-SQL table query giving incorrect result until i optimize the table

I have this table:
CREATE TABLE `posts` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL,
`origin_post_id` int(11) NOT NULL,
`ref_post_id` int(11) NOT NULL,
`broker_user_id` int(11) NOT NULL,
`isshared` tinyint(4) NOT NULL,
`type` tinyint(4) NOT NULL,
`deal_type` tinyint(4) NOT NULL,
`title` varchar(200) NOT NULL,
`currency` varchar(10) NOT NULL,
`country_code` varchar(10) NOT NULL,
`price` float(10,2) NOT NULL,
`price_to` float(10,2) NOT NULL,
`sector` int(11) NOT NULL,
`protype` int(11) NOT NULL,
`sea_view` int(11) NOT NULL,
`sea_view_to` int(11) NOT NULL,
`area` int(11) NOT NULL,
`area_to` int(11) NOT NULL,
`area_mesure_type` tinyint(4) NOT NULL DEFAULT '1',
`building_area` int(11) NOT NULL,
`building_area_to` int(11) NOT NULL,
`building_area_mesure_type` tinyint(4) NOT NULL DEFAULT '1',
`bathrooms` int(11) NOT NULL,
`bathrooms_to` int(11) NOT NULL,
`rooms` int(11) NOT NULL,
`rooms_to` int(11) NOT NULL,
`location` varchar(300) NOT NULL,
`lat` float NOT NULL,
`lng` float NOT NULL,
`description` text NOT NULL,
`map_data` varchar(500) NOT NULL,
`shape_data` text NOT NULL,
`seo_title` varchar(300) NOT NULL,
`seo_keywords` varchar(300) NOT NULL,
`seo_description` varchar(300) NOT NULL,
`status` tinyint(4) NOT NULL,
`publish_date` date NOT NULL,
`expiry_date` date NOT NULL,
`request_status` enum('','NEW','ACCEPTED','REJECTED') NOT NULL,
`request_date` date NOT NULL,
`accept_date` date NOT NULL,
`share_per` float(5,2) NOT NULL,
`deal_completed` tinyint(4) NOT NULL,
`completed_by` int(11) NOT NULL,
`created` datetime NOT NULL,
`updated` datetime NOT NULL,
PRIMARY KEY (`id`),
KEY `type` (`type`),
KEY `deal_type` (`deal_type`),
KEY `price` (`price`),
KEY `sector` (`sector`),
KEY `protype` (`protype`),
KEY `sea_view` (`sea_view`),
KEY `area` (`area`),
KEY `building_area` (`building_area`),
KEY `bathrooms` (`bathrooms`),
KEY `rooms` (`rooms`),
KEY `lat` (`lat`),
KEY `lng` (`lng`),
KEY `status` (`status`),
KEY `price_to` (`price_to`),
KEY `sea_view_to` (`sea_view_to`),
KEY `area_to` (`area_to`),
KEY `building_area_to` (`building_area_to`),
KEY `bathrooms_to` (`bathrooms_to`),
KEY `rooms_to` (`rooms_to`),
KEY `request_status` (`request_status`),
KEY `request_date` (`request_date`),
KEY `accept_date` (`accept_date`),
KEY `ref_post_id` (`ref_post_id`),
KEY `deal_completed` (`deal_completed`),
KEY `location` (`location`(255)),
KEY `user_id` (`user_id`),
KEY `country_code` (`country_code`),
KEY `expiry_date` (`expiry_date`),
KEY `origin_post_id` (`origin_post_id`),
KEY `completed_by` (`completed_by`),
FULLTEXT KEY `title_location_description` (`title`,`location`,`description`),
CONSTRAINT `posts_ibfk_3` FOREIGN KEY (`user_id`) REFERENCES `app_users` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=833 DEFAULT CHARSET=utf8
When I run select count(id) from posts where deal_completed='1' and user_id='<anyuserid>' then it gives zero records always but when I optimize this table then it's giving correct result and after some time I need again to optimize the table.
May be it is because of too many key you are using. Key should only be used if you are going to search table using that column or order by that column which makes operation fast. If not required do not use too many keys, as you have optimize the table again and again. Try to normalize the table as much you can and try to use FOREIGN KEYS. And as #Tim said if it is an int try int.

#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

MySQL count from one table while joining another

I don't know if it's just been a long day or what, but I cannot figure out the query that I need to run here. We have two tables - One for leads generated and one for reports. The leads table has basic lead info, along with the Source (Campaign) of the lead. However, we need to know the number of leads that an ACCOUNT has received within a date range. Here is the relevant table structure:
client_leads:
id
source
date
client_reports:
account
campaign
date
The 'source' column contains the same values as the 'campaign' column. So, how would I achieve the following:
Say there are 10 leads in the leads table, each with the campaign that generated the lead. There are 10 accounts in the reports table, each with hundreds of campaigns. I need to list each account and how many leads it has in the leads table.
I just can't get the logic straight in my head. I've tried everything that I can think of and it's just not working out for me. If you need further explanation, let me know. I'm trying to describe the problem to the best of my ability.
Edit:
CREATE TABLE `client_leads` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`site_id` int(10) DEFAULT NULL,
`ip` varchar(255) DEFAULT NULL,
`source` varchar(255) DEFAULT NULL,
`kw` varchar(255) DEFAULT NULL,
`adgroup` varchar(255) DEFAULT NULL,
`time` time DEFAULT NULL,
`date` date DEFAULT NULL,
`dayweek` varchar(255) DEFAULT NULL,
`first_name` varchar(255) DEFAULT NULL,
`last_name` varchar(255) DEFAULT NULL,
`address` varchar(255) DEFAULT NULL,
`city` varchar(255) DEFAULT NULL,
`postal_code` char(5) DEFAULT NULL,
`state` char(2) DEFAULT NULL,
`email` varchar(255) DEFAULT NULL,
`preferred_phone` varchar(10) DEFAULT NULL,
`alternate_phone` varchar(10) DEFAULT NULL,
`level_of_education` varchar(255) DEFAULT NULL,
`program_of_interest` varchar(255) DEFAULT NULL,
`organic` tinyint(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
KEY `site_id` (`site_id`),
KEY `date_indeces` (`time`,`date`,`dayweek`) USING BTREE,
CONSTRAINT `site_id` FOREIGN KEY (`site_id`) REFERENCES `client_sites` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=32 DEFAULT CHARSET=utf8
CREATE TABLE `client_reports` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`account` varchar(255) DEFAULT NULL,
`friendly_name` varchar(255) DEFAULT NULL,
`sites_id` int(10) DEFAULT NULL,
`service` varchar(255) DEFAULT NULL,
`date` date DEFAULT NULL,
`campaign` varchar(255) DEFAULT NULL,
`adgroup` varchar(255) DEFAULT NULL,
`keyword` varchar(255) DEFAULT NULL,
`impressions` int(10) DEFAULT NULL,
`clicks` int(10) DEFAULT NULL,
`cost` float DEFAULT NULL,
`max_cpc` float DEFAULT NULL,
`avg_pos` float DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `stats` (`impressions`,`clicks`,`cost`),
KEY `date` (`date`),
KEY `campaign` (`campaign`),
KEY `adgroup` (`adgroup`),
KEY `keyword` (`keyword`),
KEY `service` (`service`),
KEY `sites_id` (`sites_id`),
CONSTRAINT `sites_id` FOREIGN KEY (`sites_id`) REFERENCES `client_sites` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=109167 DEFAULT CHARSET=utf8
Edit Again:
client_reports table data viewable at http://pastebin.com/T532W3Eq
client_leads table data viewable at http://pastebin.com/9cjWEvck
SELECT cr.account, cr.campaign, cr.date, COUNT(cl.id) AS number_of_leads
FROM client_reports cr
LEFT JOIN client_leads cl
ON cl.source = cr.campaign
GROUP BY cl.source

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.