Error during installing dump - mysql

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.

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;

Foreign Keys on two different schemas mysql

I have two tables on distinct schemas
db1.invoice
CREATE TABLE IF NOT EXISTS db1.`invoice` (
`invoice_id` int(11) NOT NULL AUTO_INCREMENT,
`qsales_sale_id` int(11) DEFAULT NULL,
`invoice_id_from_dosage_id` int(11) NOT NULL,
`number` int(11) DEFAULT NULL,
`enterprise_name` varchar(100) DEFAULT NULL,
`subsidiary_name` varchar(100) DEFAULT NULL,
`subsidiary_address` varchar(200) DEFAULT NULL,
`subsidiary_phone` varchar(40) DEFAULT NULL,
`client_name` varchar(200) DEFAULT NULL,
`nit` bigint(20) DEFAULT NULL,
`was_paid` tinyint(1) DEFAULT NULL,
PRIMARY KEY (`invoice_id`),
KEY `fk_invoice_qsales_sale1_idx` (`qsales_sale_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=457 ;
bd2.qsale_sale
CREATE TABLE IF NOT EXISTS db2.qsales_sale (
`qsales_sale_id` int(11) NOT NULL AUTO_INCREMENT,
`qsales_order_type_id` int(11) NOT NULL,
`client_id` int(11) NOT NULL,
`total_cost` decimal(10,2) DEFAULT NULL,
`currency` varchar(45) DEFAULT NULL,
`created_at` datetime DEFAULT NULL,
`has_discount` tinyint(1) DEFAULT NULL,
`created_by` int(11) NOT NULL,
`is_wholesaler` tinyint(1) NOT NULL DEFAULT '0',
`payment_type` varchar(65) DEFAULT NULL,
PRIMARY KEY (`qsales_sale_id`),
KEY `fk_qsales_sale_qsales_order_type1_idx` (`qsales_order_type_id`),
KEY `fk_qsales_sale_client1_idx` (`client_id`),
KEY `fk_qsales_sale_employee1_idx` (`created_by`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=51 ;
then i want to add a foreign key
I try:
1.
alter table bd1.invoice
add foreign key fk_invoice_qsales_sale1(qsales_sale_id)
references db2.qsales_sale (qsales_sale_id)
on delete cascade
on update cascade;
ALTER TABLE db1.invoice
ADD CONSTRAINT fk_invoice_qsales_sale1 FOREIGN KEY (qsales_sale_id)
REFERENCES db2.qsales_sale (qsales_sale_id) ON DELETE NO ACTION ON UPDATE NO ACTION;
But i have this error:
Error Code: 1452. Cannot add or update a child row: a foreign key constraint fails (`qfix`.`#sql-a28_5`, CONSTRAINT `#sql-a28_5_ibfk_1` FOREIGN KEY (`qsales_sale_id`) REFERENCES `qsales`.`qsales_sale` (`qsales_sale_id`) ON DELETE CASCADE ON UPDATE CASCADE)
0.625 sec

MySQL - Why can't I add a foreign key here?

I'm not sure why I'm not able to add a particular foreign key here. These tables were generated via MySQL Workbench, and based on the MySQL documentation and on searching for other similar problems / solutions, I think everything is in order...yet I can't add one foreign key in particular:
inspection_statuses.inspection_id needs to reference inspection_responses.tpa_result
What am I missing / overlooking?
This is the statement I'm trying to use to add the new foreign key:
ALTER TABLE `vipsouth_app`.`inspection_statuses`
ADD CONSTRAINT `inspection_statuses_ibfk_3`
FOREIGN KEY (`inspection_id`)
REFERENCES `vipsouth_app`.`inspection_responses` (`tpa_result`)
ON DELETE NO ACTION
ON UPDATE NO ACTION;
And that produces this error:
Operation failed: There was an error while applying the SQL script to the database.
ERROR 1005: Can't create table vipsouth_app.#sql-1f48_7 (errno: 150 "Foreign key constraint is incorrectly formed")
CREATE TABLE `inspection_responses` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`inspection_request_id` int(10) unsigned NOT NULL,
`tpa_result` varchar(10) CHARACTER SET utf8 NOT NULL,
`created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`created_by` int(10) unsigned NOT NULL DEFAULT '0',
`updated_at` timestamp NULL DEFAULT NULL,
`updated_by` int(10) unsigned DEFAULT NULL,
`deleted_at` timestamp NULL DEFAULT NULL,
`deleted_by` int(10) unsigned DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `id_UNIQUE` (`id`),
UNIQUE KEY `tpa_result_UNIQUE` (`tpa_result`),
KEY `inspection_responses_ibfk_1_idx` (`inspection_request_id`),
CONSTRAINT `inspection_responses_ibfk_1` FOREIGN KEY (`inspection_request_id`) REFERENCES `inspection_requests` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
CREATE TABLE `inspection_statuses` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`inspection_request_id` int(10) unsigned NOT NULL,
`inspection_response_id` int(10) unsigned NOT NULL,
`tpa_code` varchar(4) COLLATE utf8_unicode_ci NOT NULL,
`user_id` varchar(30) COLLATE utf8_unicode_ci NOT NULL,
`password` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`inspection_id` varchar(10) COLLATE utf8_unicode_ci NOT NULL,
`status` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
`note` varchar(1000) COLLATE utf8_unicode_ci DEFAULT NULL,
`url` varchar(1024) COLLATE utf8_unicode_ci DEFAULT NULL,
`created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`created_by` int(10) unsigned NOT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
`updated_by` int(10) unsigned DEFAULT NULL,
`deleted_at` timestamp NULL DEFAULT NULL,
`deleted_by` int(10) unsigned DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `id_UNIQUE` (`id`),
KEY `inspection_statuses_ibfk_1_idx` (`inspection_request_id`),
KEY `inspection_statuses_ibfk_2_idx` (`inspection_response_id`),
CONSTRAINT `inspection_statuses_ibfk_1` FOREIGN KEY (`inspection_request_id`) REFERENCES `inspection_requests` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `inspection_statuses_ibfk_2` FOREIGN KEY (`inspection_response_id`) REFERENCES `inspection_responses` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
The data types and constraints defined on both the columns should be exactly the same. Except for a foreign key can be NULLable.
This should do it for you. I did not get any error.
SET foreign_key_checks = 0;
CREATE TABLE `inspection_responses` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`inspection_request_id` int(10) unsigned NOT NULL,
`tpa_result` varchar(10) CHARACTER SET utf8 NOT NULL,
`created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`created_by` int(10) unsigned NOT NULL DEFAULT '0',
`updated_at` timestamp NULL DEFAULT NULL,
`updated_by` int(10) unsigned DEFAULT NULL,
`deleted_at` timestamp NULL DEFAULT NULL,
`deleted_by` int(10) unsigned DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `id_UNIQUE` (`id`),
UNIQUE KEY `tpa_result_UNIQUE` (`tpa_result`),
KEY `inspection_responses_ibfk_1_idx` (`inspection_request_id`),
CONSTRAINT `inspection_responses_ibfk_1` FOREIGN KEY (`inspection_request_id`) REFERENCES `inspection_requests` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
CREATE TABLE `inspection_statuses` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`inspection_request_id` int(10) unsigned NOT NULL,
`inspection_response_id` int(10) unsigned NOT NULL,
`tpa_code` varchar(4) COLLATE utf8_unicode_ci NOT NULL,
`user_id` varchar(30) COLLATE utf8_unicode_ci NOT NULL,
`password` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`inspection_id` varchar(10) COLLATE utf8_unicode_ci NOT NULL,
`status` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
`note` varchar(1000) COLLATE utf8_unicode_ci DEFAULT NULL,
`url` varchar(1024) COLLATE utf8_unicode_ci DEFAULT NULL,
`created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`created_by` int(10) unsigned NOT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
`updated_by` int(10) unsigned DEFAULT NULL,
`deleted_at` timestamp NULL DEFAULT NULL,
`deleted_by` int(10) unsigned DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `id_UNIQUE` (`id`),
KEY `inspection_statuses_ibfk_1_idx` (`inspection_request_id`),
KEY `inspection_statuses_ibfk_2_idx` (`inspection_response_id`),
CONSTRAINT `inspection_statuses_ibfk_1` FOREIGN KEY (`inspection_request_id`) REFERENCES `inspection_requests` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `inspection_statuses_ibfk_2` FOREIGN KEY (`inspection_response_id`) REFERENCES `inspection_responses` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
SET foreign_key_checks = 1;

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.