error 105 mysql FOREIGN KEYS - mysql

I am trying to create database table and get them connected by mysql FOREIGN KEYS. I have ensured that my data types are identical. I also ensure that my tables are made before added the FK. Any advice would be greatly appreciated.
CREATE TABLE IF NOT EXISTS `af_feeds` (
`id` int(64) unsigned NOT NULL AUTO_INCREMENT,
`hash` char(255) NOT NULL,
`seed_id` int(64) NOT NULL,
`category_id` int(64) NOT NULL,
`title` varchar(255) NOT NULL,
`description` text,
`content` longtext,
`publishing_date` varchar(255) NOT NULL,
`link` text NOT NULL,
`status` int(1) NOT NULL,
`create_date` int(64) NOT NULL,
`update_date` int(64) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `hash` (`hash`),
FOREIGN KEY (`seed_id`) REFERENCES `af_seeds`(`id`)
ON UPDATE CASCADE
ON DELETE CASCADE,
FOREIGN KEY (`category_id`) REFERENCES `af_categories`(`id`)
ON UPDATE CASCADE
ON DELETE CASCADE
MySQL said:
1005 - Can't create table 'estafeed_rss.af_feeds' (errno: 150) (Details…)
--
-- Table structure for table `af_categories`
--
CREATE TABLE IF NOT EXISTS `af_categories` (
`id` int(64) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`description` varchar(255) NOT NULL,
`icon` varchar(255) NOT NULL,
`image` varchar(255) NOT NULL,
`status` int(1) NOT NULL,
`create_date` int(64) NOT NULL,
`update_date` int(64) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=8 ;
-- --------------------------------------------------------
--
-- Table structure for table `af_seeds`
--
CREATE TABLE IF NOT EXISTS `af_seeds` (
`id` int(64) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`link` varchar(255) NOT NULL,
`category_id` int(64) NOT NULL,
`loading_times` int(64) NOT NULL,
`status` int(1) NOT NULL,
`loading_each` int(64) NOT NULL,
`last_loading` int(64) NOT NULL,
`create_date` int(64) NOT NULL,
`update_date` int(64) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `url` (`link`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=7 ;

Related

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;

#1452 - Cannot add or update a child row: a foreign key constraint fails ... Jutts

I have created tables in MySQL Workbench as shown below :
Categories Table
CREATE TABLE IF NOT EXISTS `categories` (
`categoryId` int(11) primary key auto_incremnt NOT NULL,
`categoryName` varchar(200) DEFAULT NULL,
`categoryDescription` varchar(200) DEFAULT NULL,
`categoryPicture` varchar(100) DEFAULT NULL,
`PostDate` varchar(255) NOT NULL,
`status` varchar(10) NOT NULL DEFAULT '1',
`LastInsertID` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=45 ;
Products Table
CREATE TABLE IF NOT EXISTS `products` (
`proId` int(11) NOT NULL,
CONSTRAINT `categoryId` FOREIGN KEY (`categoryId`) REFERENCES `categories` (`categoryId`) ,
`proName` varchar(100) DEFAULT NULL,
`proPath` varchar(90) DEFAULT NULL,
`proPrice` float DEFAULT NULL,
`proQuantity` decimal(38,0) DEFAULT NULL,
`proImage` varchar(100) DEFAULT NULL,
`PostDate` varchar(50) NOT NULL,
`status` int(11) NOT NULL DEFAULT '1'
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;
I'm having a bit of a strange problem, I'm trying to add a foreign key
to one table that references another, but it is failing for some
reason. With my limited knowledge of MySQL, the only thing that could
possibly be suspect is that there is a foreign key on a different
table referencing the one I am trying to reference.
Please help me out as soon as possible .
Thanks in advance
You should create the categoryId column before assigning it in a foreign key,
categoryId int(11) NOT NULL,
CREATE TABLE IF NOT EXISTS `products` (
`proId` INT(11) NOT NULL,
`categoryId` INT ,
`proName` VARCHAR(100) DEFAULT NULL,
`proPath` VARCHAR(90) DEFAULT NULL,
`proPrice` FLOAT DEFAULT NULL,
`proQuantity` DECIMAL(38,0) DEFAULT NULL,
`proImage` VARCHAR(100) DEFAULT NULL,
`PostDate` VARCHAR(50) NOT NULL,
`status` INT(11) NOT NULL DEFAULT '1',
FOREIGN KEY (`categoryId`) REFERENCES `categories` (`categoryId`)
) ENGINE=INNODB DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;

MySQL table creation

Below is the code I used in MySQL workbench to create the database as well as the tables. Finally I have mentioned the error I face. Could anyone please help me with this?
delimiter $$
CREATE DATABASE `sa38team4` /*!40100 DEFAULT CHARACTER SET utf8 */$$
Employee Table :
delimiter $$
CREATE TABLE `employee` (
`EmpID` int(11) NOT NULL AUTO_INCREMENT,
`EmpName` varchar(100) NOT NULL,
`DOB` datetime NOT NULL,
`Gender` varchar(10) NOT NULL,
`Password` varchar(50) NOT NULL,
`ContactNumber` varchar(45) NOT NULL,
`Email` varchar(45) DEFAULT NULL,
`Role` varchar(15) NOT NULL,
`Address` varchar(200) DEFAULT NULL,
PRIMARY KEY (`EmpID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8$$
Inventory Table :
delimiter $$
CREATE TABLE `inventory` (
`ItemID` int(11) NOT NULL AUTO_INCREMENT,
`PID` int(11) NOT NULL,
`TotalQty` int(11) NOT NULL,
`Availability` int(11) NOT NULL,
`ReorderPt` int(11) NOT NULL,
`MinOrdQty` int(11) NOT NULL,
`UnitPrice` decimal(10,2) NOT NULL,
PRIMARY KEY (`ItemID`),
KEY `PID` (`PID`),
CONSTRAINT `PID` FOREIGN KEY (`PID`) REFERENCES `productdetail` (`PID`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8$$
`MID` varchar(45) NOT NULL,
`Manufacturer` varchar(100) NOT NULL,
`Model` varchar(45) NOT NULL,
`PartNo` varchar(45) NOT NULL,
`ItemDesc` varchar(100) NOT NULL,
PRIMARY KEY (`PID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8$$
Product Details Table :
delimiter $$
CREATE TABLE `productdetail` (
`PID` int(11) NOT NULL AUTO_INCREMENT,
`MID` varchar(45) NOT NULL,
`Manufacturer` varchar(100) NOT NULL,
`Model` varchar(45) NOT NULL,
`PartNo` varchar(45) NOT NULL,
`ItemDesc` varchar(100) NOT NULL,
PRIMARY KEY (`PID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8$$
Trans In Table :
delimiter $$
CREATE TABLE `transin` (
`TransID` int(11) NOT NULL AUTO_INCREMENT,
`Date` datetime NOT NULL,
`EmpID` int(11) NOT NULL,
`ItemID` int(11) NOT NULL,
`Qty` int(11) NOT NULL,
`UnitPrice` decimal(10,2) NOT NULL,
`Remark` varchar(100) DEFAULT NULL,
PRIMARY KEY (`TransID`),
KEY `EmpID` (`EmpID`),
KEY `ItemID` (`ItemID`),
CONSTRAINT `EmpID` FOREIGN KEY (`EmpID`) REFERENCES `employee` (`EmpID`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `ItemID` FOREIGN KEY (`ItemID`) REFERENCES `inventory` (`ItemID`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8$$
All the four above tables are created.
TransOut Tablle :
delimiter $$
CREATE TABLE `transout` (
`TransID` int(11) NOT NULL AUTO_INCREMENT,
`Date` datetime NOT NULL,
`EmpID` int(11) NOT NULL,
`ItemID` int(11) NOT NULL,
`Qty` int(11) NOT NULL,
`UnitPrice` decimal(10,2) NOT NULL,
`Remark` varchar(100) DEFAULT NULL,
PRIMARY KEY (`TransID`),
KEY `EmpID` (`EmpID`),
KEY `ItemID` (`ItemID`),
CONSTRAINT `EmpID` FOREIGN KEY (`EmpID`) REFERENCES `employee` (`EmpID`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `ItemID` FOREIGN KEY (`ItemID`) REFERENCES `inventory` (`ItemID`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8$$
The last table n creation, shows an error as
"Error Code : 1005. Can't create table 'sa38team4.transout'(errno: 121)"
Could anyone please help me?
Check out this post : MySQL errorno 121
As said :
Check that all your constraints are really spelled out correctly, also check that there's not any other tables that uses the constraint names ItemID or EmpID
So I think that you may use another name into your TransOut table for ItemID & EmpID

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.