How to create database with foreign keys - mysql

I've got an issue when I try to fill my fixtures. I've got 2 tables (client and partner).
CREATE TABLE `partner` (
`id` tinyint(3) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(20) NOT NULL,
`client_id` int(10) unsigned NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `name` (`name`),
UNIQUE KEY `client_id` (`client_id`),
CONSTRAINT `partner_ibfk_3` FOREIGN KEY (`client_id`) REFERENCES `client`
(`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
CREATE TABLE `client` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`partner_id` tinyint(3) unsigned DEFAULT NULL,
`partner_ref` varchar(7) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `partner_ref` (`partner_ref`,`partner_id`),
KEY `partner_id` (`partner_id`),
CONSTRAINT `client_ibfk_1` FOREIGN KEY (`partner_id`) REFERENCES `partner`
(`id`),
) ENGINE=InnoDB AUTO_INCREMENT=667305 DEFAULT CHARSET=utf8 PACK_KEYS=0;
When I run my fixtures I've got this issue :
ERROR 1215 (HY000) at line 863: Cannot add foreign key constraint
I supposed it's because when the table 'partner' is created, the table 'client' is not so the foreign key is not found.
However if I change and I put 'client' before 'partner', the problem it's same.
Thanks for your help !

I found a solution :
I removed the constraint in the creation of 'partner' and I added this constraint after the creation of 'client' with an ALTER TABLE :
CREATE TABLE `partner` (
`id` tinyint(3) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(20) NOT NULL,
`client_id` int(10) unsigned NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `name` (`name`),
UNIQUE KEY `client_id` (`client_id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
CREATE TABLE `client` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`partner_id` tinyint(3) unsigned DEFAULT NULL,
`partner_ref` varchar(7) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `partner_ref` (`partner_ref`,`partner_id`),
KEY `partner_id` (`partner_id`),
CONSTRAINT `client_ibfk_1` FOREIGN KEY (`partner_id`) REFERENCES `partner`
(`id`),
) ENGINE=InnoDB AUTO_INCREMENT=667305 DEFAULT CHARSET=utf8 PACK_KEYS=0;
ALTER TABLE `partner`
ADD CONSTRAINT `partner_ibfk_3` FOREIGN KEY (`client_id`) REFERENCES
`client` (`id`);

Related

foreign key relationship is not working in mysql

Here I am doing foreign key relation for my SQL, I am having 3 tables
product
gallery
offer
Here product table is a master table and gallery, offer tables are child table, now I trying to delete the master table entry but it not happening I am getting an error like
Cannot delete or update a parent row: a foreign key constraint fails
CREATE TABLE `product` (
`productId` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(100) COLLATE utf16_bin NOT NULL,
`price` decimal(10,0) NOT NULL,
`regOn` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`productId`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf16 COLLATE=utf16_bin
CREATE TABLE `offer` (
`offerId` int(11) NOT NULL AUTO_INCREMENT,
`productId` int(11) NOT NULL,
`offerpercentage` text COLLATE utf16_bin NOT NULL,
PRIMARY KEY (`offerId`),
KEY `productId` (`productId`),
CONSTRAINT `offer_ibfk_1` FOREIGN KEY (`productId`) REFERENCES `product` (`productId`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf16 COLLATE=utf16_bin
CREATE TABLE `gallery` (
`galleryId` int(11) NOT NULL AUTO_INCREMENT,
`productId` int(11) NOT NULL,
`galleryName` text COLLATE utf16_bin NOT NULL,
PRIMARY KEY (`galleryId`),
KEY `productId` (`productId`),
CONSTRAINT `gallery_ibfk_1` FOREIGN KEY (`productId`) REFERENCES `product` (`productId`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf16 COLLATE=utf16_bin
Expected output
Suppose I am deleting master table entry means i have to delete all child tables entry also
You need to apply the ON DELETE CASCADE to your foreign key references.
CREATE TABLE `product` (
`productId` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(100) COLLATE utf16_bin NOT NULL,
`price` decimal(10,0) NOT NULL,
`regOn` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`productId`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf16 COLLATE=utf16_bin
CREATE TABLE `offer` (
`offerId` int(11) NOT NULL AUTO_INCREMENT,
`productId` int(11) NOT NULL,
`offerpercentage` text COLLATE utf16_bin NOT NULL,
PRIMARY KEY (`offerId`),
KEY `productId` (`productId`),
CONSTRAINT `offer_ibfk_1` FOREIGN KEY (`productId`) REFERENCES `product` (`productId`) ON DELETE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf16 COLLATE=utf16_bin
CREATE TABLE `gallery` (
`galleryId` int(11) NOT NULL AUTO_INCREMENT,
`productId` int(11) NOT NULL,
`galleryName` text COLLATE utf16_bin NOT NULL,
PRIMARY KEY (`galleryId`),
KEY `productId` (`productId`),
CONSTRAINT `gallery_ibfk_1` FOREIGN KEY (`productId`) REFERENCES `product` (`productId`) ON DELETE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf16 COLLATE=utf16_bin
This tells MySQL that when the parent is deleted, delete the referencing child.

Foreign key failure when inserting into child table ( referencing composite unique index)

I have problem when trying to insert row into my child database table.
Here is my parent table:
CREATE TABLE `payables` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`type` varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL,
`currency_id` int(10) unsigned NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `payables_id_type_unique` (`id`,`type`),
KEY `payables_currency_id_foreign` (`currency_id`),
CONSTRAINT `payables_currency_id_foreign` FOREIGN KEY (`currency_id`) REFERENCES `currencies` (`id`) ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
And here is my child table:
CREATE TABLE `pays` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`transaction_id` int(10) unsigned NOT NULL,
`user_id` int(10) unsigned NOT NULL,
`value` int(10) unsigned NOT NULL,
`status_id` int(10) unsigned NOT NULL,
`payable_id` int(10) unsigned NOT NULL,
`payable_type` varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
`deleted_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `pays_transaction_id_unique` (`transaction_id`),
KEY `pays_user_id_index` (`user_id`),
KEY `pays_status_id_index` (`status_id`),
KEY `pays_payable_id_index` (`payable_id`),
KEY `pays_payable_type_index` (`payable_type`),
KEY `pays_payable_id_payable_type_foreign` (`payable_id`,`payable_type`),
CONSTRAINT `pays_payable_id_payable_type_foreign` FOREIGN KEY (`payable_id`, `payable_type`) REFERENCES `payables` (`id`, `type`) ON UPDATE CASCADE,
CONSTRAINT `pays_status_id_foreign` FOREIGN KEY (`status_id`) REFERENCES `statuses` (`id`) ON UPDATE CASCADE,
CONSTRAINT `pays_transaction_id_foreign` FOREIGN KEY (`transaction_id`) REFERENCES `transactions` (`id`) ON UPDATE CASCADE,
CONSTRAINT `pays_user_id_foreign` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
That is the table I'm trying to seed, the parent table is already seeded.
I'm using laravel query builder to create these tables, so if anyone more experienced with databases could review the code, that would be much appreciated, thanks.
Sorry just missed that i was inserting different data type to my column.

not able to apply foreign key constraint in mysql

I can run the below query and there are no nulls in the user_id column in the result -
select c.*,r.user_id from chat_group_users c left join roleuser r
on c.user_id = r.user_id;
But I cannot apply the foreign key constraint and mysql just says
Error Code: 1215. Cannot add foreign key constraint
The command i wrote -
ALTER TABLE chat_group_users
ADD CONSTRAINT fk_roleuser FOREIGN KEY (user_id) REFERENCES roleuser (user_id)
ON DELETE cascade ON UPDATE cascade;
The table structures -
CREATE TABLE `roleuser` (
`User_ID` int(10) unsigned NOT NULL AUTO_INCREMENT,
`UserNM` varchar(50) NOT NULL,
`UserPS` varchar(150) NOT NULL,
`r_username` varchar(50) DEFAULT NULL,
`UGroup` varchar(5) NOT NULL DEFAULT 'Usar',
`FirstName` varchar(45) NOT NULL,
`LastName` varchar(45) NOT NULL,
PRIMARY KEY (`User_ID`),
UNIQUE KEY `Index_2` (`UserNM`),
) ENGINE=InnoDB AUTO_INCREMENT=50 DEFAULT CHARSET=latin1;
CREATE TABLE `chat_group_users` (
`auto_id` int(11) NOT NULL AUTO_INCREMENT,
`group_id` int(11) DEFAULT NULL,
`user_id` int(10) DEFAULT NULL,
`is_admin` varchar(5) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
PRIMARY KEY (`auto_id`),
UNIQUE KEY `unique_user_group` (`user_id`,`group_id`),
KEY `fc_group_id` (`group_id`),
CONSTRAINT `fc_group_id` FOREIGN KEY (`group_id`)
REFERENCES `chat_group`(`auto_id`)
ON DELETE CASCADE ON UPDATE CASCADE
)ENGINE=InnoDB AUTO_INCREMENT=21 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
missed the unsigned on the chat_group_users -
`User_ID` int(10) unsigned NOT NULL AUTO_INCREMENT, ...

ERROR:: a foreign key constraint fails

Error::1452: Cannot add or update a child row: a foreign key constraint fails (findmybuffet.det_res_item_selected, CONSTRAINTfk_det_res_item_selected_Buf_Off_Id1FOREIGN KEY (Buf_Off_Id) REFERENCESmas_buf_off(Buf_Off_Id) ON DELETE CASCADE)
I have three tables below::
det_res_item_selected is throwing error when i tried to create the table
How can i resolve this ?
CREATE TABLE `det_res_item_selected` (
`Line_Selected_Item_Id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`Item_Id` int(11) unsigned NOT NULL,
`Item_Selected` tinyint(4) DEFAULT '0',
`Buf_Off_Id` int(11) unsigned DEFAULT NULL,
`Item_Image` varchar(50) NOT NULL,
PRIMARY KEY (`Line_Selected_Item_Id`),
KEY `fk_det_res_item_selected_Item1_Idx` (`Item_Id`),
KEY `fk_det_res_item_selected_Buf_Off_Id1_Idx` (`Buf_Off_Id`),
CONSTRAINT `fk_det_res_item_selected_Buf_Off_Id1` FOREIGN KEY (`Buf_Off_Id`) REFERENCES `mas_buf_off` (`Buf_Off_Id`) ON DELETE CASCADE,
CONSTRAINT `fk_det_res_item_selected_Item_Id1` FOREIGN KEY (`Item_Id`) REFERENCES `mas_item` (`Item_Id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `mas_buf_off` (
`Buf_Off_Id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`Buf_Type_Id` int(11) unsigned NOT NULL,
`From_Time` time NOT NULL,
`To_Time` time NOT NULL,
`Res_Id` int(11) unsigned NOT NULL,
PRIMARY KEY (`Buf_Off_Id`),
KEY `fk_mas_buf_off_Buf_Type_Id1_Idx` (`Buf_Type_Id`),
KEY `fk_mas_buf_off_Res_Id1_Idx` (`Res_Id`),
CONSTRAINT `fk_mas_buf_off_Buf_Type_Id1` FOREIGN KEY (`Buf_Type_Id`) REFERENCES `mas_buf_type` (`Buf_Type_Id`) ON DELETE CASCADE,
CONSTRAINT `fk_mas_buf_off_Res_Id1_Id1` FOREIGN KEY (`Res_Id`) REFERENCES `mas_restaurant` (`Res_Id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `mas_restaurant` (
`Res_Id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`Res_Name` varchar(100) NOT NULL,
`Res_Featured` tinyint(4) NOT NULL DEFAULT '0',
PRIMARY KEY (`Res_Id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
I Just ran below script and it worked on my system.
set foreign_key_checks=0;
CREATE TABLE `det_res_item_selected` (
`Line_Selected_Item_Id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`Item_Id` int(11) unsigned NOT NULL,
`Item_Selected` tinyint(4) DEFAULT '0',
`Buf_Off_Id` int(11) unsigned DEFAULT NULL,
`Item_Image` varchar(50) NOT NULL,
PRIMARY KEY (`Line_Selected_Item_Id`),
KEY `fk_det_res_item_selected_Item1_Idx` (`Item_Id`),
KEY `fk_det_res_item_selected_Buf_Off_Id1_Idx` (`Buf_Off_Id`),
CONSTRAINT `fk_det_res_item_selected_Buf_Off_Id1` FOREIGN KEY (`Buf_Off_Id`) REFERENCES `mas_buf_off` (`Buf_Off_Id`) ON DELETE CASCADE,
CONSTRAINT `fk_det_res_item_selected_Item_Id1` FOREIGN KEY (`Item_Id`) REFERENCES `mas_item` (`Item_Id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `mas_buf_off` (
`Buf_Off_Id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`Buf_Type_Id` int(11) unsigned NOT NULL,
`From_Time` time NOT NULL,
`To_Time` time NOT NULL,
`Res_Id` int(11) unsigned NOT NULL,
PRIMARY KEY (`Buf_Off_Id`),
KEY `fk_mas_buf_off_Buf_Type_Id1_Idx` (`Buf_Type_Id`),
KEY `fk_mas_buf_off_Res_Id1_Idx` (`Res_Id`),
CONSTRAINT `fk_mas_buf_off_Buf_Type_Id1` FOREIGN KEY (`Buf_Type_Id`) REFERENCES `mas_buf_type` (`Buf_Type_Id`) ON DELETE CASCADE,
CONSTRAINT `fk_mas_buf_off_Res_Id1_Id1` FOREIGN KEY (`Res_Id`) REFERENCES `mas_restaurant` (`Res_Id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `mas_restaurant` (
`Res_Id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`Res_Name` varchar(100) NOT NULL,
`Res_Featured` tinyint(4) NOT NULL DEFAULT '0',
PRIMARY KEY (`Res_Id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
set foreign_key_checks=1;

Foreign Key Constraint Fails - Probably misunderstanding something about my relationships

I'm having a little trouble with some MySQL relationships. I think I'm missing something obvious in my structure. Here's my SQL:
DROP TABLE IF EXISTS `parentlist_comments`;
CREATE TABLE `parentlist_comments` (
`id` char(36) NOT NULL,
`parentlist_id` char(36) NOT NULL,
`user_id` char(36) NOT NULL,
`comment` char(50) NOT NULL,
`accepted` tinyint(1) NOT NULL DEFAULT '0',
`submitted` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
KEY `fk_parentlist_comments_parentlist` (`parentlist_id`),
KEY `fk_parentlist_comment_user` (`user_id`),
CONSTRAINT `fk_parentlist_comments_parentlist` FOREIGN KEY (`parentlist_id`) REFERENCES `parentlists` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `fk_parentlist_comment_user` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS `parentlist_submissions`;
CREATE TABLE `parentlist_submissions` (
`id` char(36) NOT NULL,
`parentlist_id` char(36) NOT NULL,
`type_id` char(36) NOT NULL,
`name` char(25) NOT NULL,
`user_id` char(36) NOT NULL,
`accepted` tinyint(1) NOT NULL DEFAULT '0',
`submitted` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`votes` int(3) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
KEY `fk_parentlist_submissions_user` (`user_id`),
KEY `fk_parentlist_submissions_list` (`parentlist_id`),
KEY `fk_parentlist_submissions_type` (`type_id`),
CONSTRAINT `fk_parentlist_submissions_list` FOREIGN KEY (`parentlist_id`) REFERENCES `parentlists` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `fk_parentlist_submissions_type` FOREIGN KEY (`type_id`) REFERENCES `types` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `fk_parentlist_submissions_user` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS `parentlists`;
CREATE TABLE `parentlists` (
`id` char(36) NOT NULL,
`name` char(20) NOT NULL,
`description` char(50) NOT NULL,
`user_id` char(36) NOT NULL,
`max_comments` int(3) NOT NULL DEFAULT '0',
`max_submissions` int(3) NOT NULL DEFAULT '10',
`max_votes` int(3) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
KEY `fk_list_user` (`user_id`),
CONSTRAINT `fk_list_user` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=FIXED;
DROP TABLE IF EXISTS `submissions`;
CREATE TABLE `submissions` (
`id` char(36) NOT NULL,
`type_id` char(36) NOT NULL,
`name` char(30) NOT NULL,
`description` char(50) NOT NULL,
`embed` char(200) DEFAULT NULL,
`user_id` char(36) NOT NULL,
`accepted` tinyint(1) NOT NULL DEFAULT '0',
`submitted` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`votes` int(5) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
KEY `fk_submission_user` (`user_id`),
KEY `fk_submission_type` (`type_id`),
CONSTRAINT `fk_submission_type` FOREIGN KEY (`type_id`) REFERENCES `types` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `fk_submission_user` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS `types`;
CREATE TABLE `types` (
`id` char(36) NOT NULL,
`name` char(20) NOT NULL,
`description` char(20) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS `users`;
CREATE TABLE `users` (
`id` char(36) NOT NULL,
`name` char(20) NOT NULL,
`password` char(20) NOT NULL,
`email` char(50) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
I created an column called submission_id in parentlist_submissions. I'm trying to create a foreign key relationship between parentlist_submissions.submission_id and submissions.id, when I attempt to do this I get the error: Foriegn key constraint fails. For whatever reason my query browser won't let me copy this.
Any help here is greatly appreciated!
That error is usually caused by the tables already being populated with data that violate the constraint. (Note that nulls may be a problem if you've just added the column.)
I'm guessing, because I don't see that you've posted the statement where you create the submission_index column or where you create the foreign key constraint.
You seem to be missing the "parentlist_submissions.submission_id" column.