Fail to create relationship between two tables - mysql

I have two tables :
CREATE TABLE `Users` (
`user_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`first_name` varchar(50) NOT NULL DEFAULT '',
`last_name` varchar(50) NOT NULL DEFAULT '',
`login` varchar(50) NOT NULL DEFAULT '',
`password` varchar(50) NOT NULL DEFAULT '',
PRIMARY KEY (`user_id`)
) ENGINE=InnoDB AUTO_INCREMENT=15 DEFAULT CHARSET=utf8;
AND
CREATE TABLE `Books` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`title` varchar(50) NOT NULL DEFAULT '',
`author` varchar(50) NOT NULL DEFAULT '',
`year` int(4) NOT NULL,
`available` int(3) NOT NULL DEFAULT '0',
`availabledate` date NOT NULL,
`user_id` int(11) unsigned NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
I am trying to create a relationship between those two, so that one user may have multiple books (user_id) but whatever I'm doing I'm getting errors. Either
Cannot add or update a child row: a foreign key constraint fails
(bookstore.,
CONSTRAINT books_fk FOREIGN KEY (user_id) REFERENCES users
(user_id) ON DELETE CASCADE ON UPDATE CASCADE)
or before I didn't use unsigned int in the Books table and I said that default value is 0 (which I would prefere but I don't think I can do that?) In that case I got error 150.

I'd recommend, to change your database schema. Why?
Can a book exist without having a user? If yes, you shouldn't have a foreign key from books referencing users. Can a user exist without having a book? If yes, you shouldn't have a foreign key from users referencing books.
Can a user have multiple books? And a book multiple users? If yes, you have a m:n relationship. This means you need a bridge table.
In your tables you don't need foreign keys:
CREATE TABLE `Users` (
`user_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`first_name` varchar(50) NOT NULL DEFAULT '',
`last_name` varchar(50) NOT NULL DEFAULT '',
`login` varchar(50) NOT NULL DEFAULT '',
`password` varchar(50) NOT NULL DEFAULT '',
PRIMARY KEY (`user_id`)
) ENGINE=InnoDB AUTO_INCREMENT=15 DEFAULT CHARSET=utf8;
CREATE TABLE `Books` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`title` varchar(50) NOT NULL DEFAULT '',
`author` varchar(50) NOT NULL DEFAULT '',
`year` int(4) NOT NULL,
`available` int(3) NOT NULL DEFAULT '0',
`availabledate` date NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
And a bridge table would look like this:
CREATE TABLE books_users (
book_id int(11) unsigned NOT NULL,
user_id int(11) unsigned NOT NULL,
PRIMARY KEY (book_id, user_id),
KEY idx_user_id (user_id),
FOREIGN KEY fk_books (book_id) REFERENCES Books(id),
FOREIGN KEY fk_users (user_id) REFERENCES Users(user_id)
) ENGINE=InnoDB;
This solves both problems and is common practice.
To query both users and books in one query, you join them like this:
SELECT
whatever
FROM
Books b
INNER JOIN books_users bu ON b.id = bu.book_id
INNER JOIN users u ON bu.user_id = u.user_id
WHERE user_id = 1 /*for example*/
;
If you want to insert something in the tables, just do the insert and get the id which was generated for the row with SELECT LAST_INSERT_ID();, then insert this id in the books_users bridge table.
Updates don't affect anything, you can simply perform them on users or books. If you really really have to update the auto_increment column (which usually isn't needed and not recommended) you can add ON UPDATE CASCADE after the foreign keys in the books_users table.

Change these lines and try again
change user_id int(11) unsigned NOT NULL AUTO_INCREMENT to user_id
int(11) NOT NULL AUTO_INCREMENT
and
id int(11) unsigned NOT NULL AUTO_INCREMENT, to id int(11) NOT
NULL AUTO_INCREMENT,
and
user_id int(11) unsigned NOT NULL, to user_id int(11) NOT NULL,
Finally try
CREATE TABLE `Users` (
`user_id` int(11) NOT NULL AUTO_INCREMENT,
`first_name` varchar(50) NOT NULL DEFAULT '',
`last_name` varchar(50) NOT NULL DEFAULT '',
`login` varchar(50) NOT NULL DEFAULT '',
`password` varchar(50) NOT NULL DEFAULT '',
PRIMARY KEY (`user_id`)
) ENGINE=InnoDB AUTO_INCREMENT=15 DEFAULT CHARSET=utf8;
and
CREATE TABLE `Books` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(50) NOT NULL DEFAULT '',
`author` varchar(50) NOT NULL DEFAULT '',
`year` int(4) NOT NULL,
`available` int(3) NOT NULL DEFAULT '0',
`availabledate` date NOT NULL,
`user_id` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;

If you are using Mysql then you must use InnoDB database engine to enable relationship between two tables
MyISAM will not allow this relationship
alter table `users` add constraint `FK_users` FOREIGN KEY (`user_id`) REFERENCES `books` (`user_id`) ON DELETE NO ACTION ON UPDATE NO ACTION
or you can use this query for cascade edit delete
alter table `users` add constraint `FK_users` FOREIGN KEY (`user_id`) REFERENCES `books` (`user_id`) ON DELETE CASCADE ON UPDATE CASCADE
This is your new DDL for two tables try this
CREATE TABLE IF NOT EXISTS `books` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`title` varchar(50) NOT NULL DEFAULT '',
`author` varchar(50) NOT NULL DEFAULT '',
`year` int(4) NOT NULL,
`available` int(3) NOT NULL DEFAULT '0',
`availabledate` date NOT NULL,
`user_id` int(11) unsigned NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ;
-- --------------------------------------------------------
--
-- Table structure for table `users`
--
CREATE TABLE IF NOT EXISTS `users` (
`user_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`first_name` varchar(50) NOT NULL DEFAULT '',
`last_name` varchar(50) NOT NULL DEFAULT '',
`login` varchar(50) NOT NULL DEFAULT '',
`password` varchar(50) NOT NULL DEFAULT '',
PRIMARY KEY (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=15 ;
--
-- Constraints for dumped tables
--
--
-- Constraints for table `users`
--
ALTER TABLE `users`
ADD CONSTRAINT `FK_users` FOREIGN KEY (`user_id`) REFERENCES `books` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;

Related

Foreign key constraint is incorrectly formed (MySQL)

I am trying to create a database for this project: https://sourceforge.net/projects/ess/
I've created a database called ess via PHPmyadmin and when I run the SQL query to create the tables I get an error on the second table areasuper.
The error is: "#1005 - Can't create table `ess.areasuper` (errno: 150 "Foreign key constraint is incorrectly formed")"
I don't really understand what I have been able to find online regarding this type of error so I'm hoping someone here can spell it out for me.
Also, I'm not sure how much info anyone will need to understand and replicate this so please let me know what to include if I have missed anything.
Thank you.
CREATE TABLE areas (
area_id int(11) unsigned NOT NULL auto_increment,
area_name varchar(20) default NULL,
area_desc varchar(100) NOT NULL default '',
area_templ text,
PRIMARY KEY (area_id)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Table structure for table `areasuper`
--
CREATE TABLE areasuper (
as_id int(11) unsigned NOT NULL auto_increment,
as_area_id int(11) unsigned NOT NULL default '0',
as_uid int(11) unsigned NOT NULL default '0',
PRIMARY KEY (as_id),
KEY as_area_id (as_area_id),
KEY as_uid (as_uid),
CONSTRAINT areasuper_ibfk_1 FOREIGN KEY (as_area_id) REFERENCES areas (area_id) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT areasuper_ibfk_2 FOREIGN KEY (as_uid) REFERENCES users (user_id) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Table structure for table `assignments`
--
CREATE TABLE assignments (
assign_id int(10) unsigned NOT NULL auto_increment,
assign_uid int(10) unsigned NOT NULL default '0',
assign_pid int(10) unsigned NOT NULL default '0',
assign_eid int(10) unsigned NOT NULL default '0',
PRIMARY KEY (assign_id),
KEY assign_uid (assign_uid),
KEY assign_pid (assign_pid),
KEY assign_eid (assign_eid),
CONSTRAINT assignments_ibfk_1 FOREIGN KEY (assign_uid) REFERENCES users (user_id) ON DELETE NO ACTION ON UPDATE CASCADE,
CONSTRAINT assignments_ibfk_2 FOREIGN KEY (assign_pid) REFERENCES positions (pos_id) ON DELETE NO ACTION ON UPDATE CASCADE,
CONSTRAINT assign_assign_eid_fk FOREIGN KEY (assign_eid) REFERENCES `events` (event_id) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Table structure for table `availabletimes`
--
CREATE TABLE availabletimes (
avail_id int(10) unsigned NOT NULL auto_increment,
avail_uid int(11) unsigned NOT NULL default '0',
avail_day char(1) NOT NULL default '',
avail_start time NOT NULL default '00:00:00',
avail_end time NOT NULL default '00:00:00',
PRIMARY KEY (avail_id),
KEY avail_uid (avail_uid),
CONSTRAINT availabletimes_ibfk_1 FOREIGN KEY (avail_uid) REFERENCES users (user_id) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Table structure for table `dayoff`
--
CREATE TABLE dayoff (
day_id int(11) unsigned NOT NULL auto_increment,
day_uid int(11) unsigned NOT NULL default '0',
day_start date NOT NULL default '0000-00-00',
day_end date NOT NULL default '0000-00-00',
day_desc varchar(50) NOT NULL default '',
PRIMARY KEY (day_id),
KEY day_uid (day_uid),
CONSTRAINT dayoff_ibfk_1 FOREIGN KEY (day_uid) REFERENCES users (user_id) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Table structure for table `events`
--
CREATE TABLE `events` (
event_id int(11) unsigned NOT NULL auto_increment,
event_start time NOT NULL default '00:00:00',
event_end time NOT NULL default '00:00:00',
event_area_id int(11) unsigned NOT NULL default '0',
event_name varchar(50) NOT NULL default '',
event_comments text,
event_date date NOT NULL default '0000-00-00',
PRIMARY KEY (event_id),
KEY event_area_id (event_area_id),
CONSTRAINT events_ibfk_1 FOREIGN KEY (event_area_id) REFERENCES areas (area_id) ON DELETE NO ACTION ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Table structure for table `positions`
--
CREATE TABLE positions (
pos_id int(10) unsigned NOT NULL auto_increment,
pos_area_id int(10) unsigned NOT NULL default '0',
pos_name varchar(20) NOT NULL default '',
pos_desc varchar(100) NOT NULL default '',
PRIMARY KEY (pos_id),
KEY pos_area_id (pos_area_id),
CONSTRAINT positions_ibfk_1 FOREIGN KEY (pos_area_id) REFERENCES areas (area_id) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Table structure for table `supervisors`
--
CREATE TABLE supervisors (
super_emp int(11) unsigned NOT NULL default '0',
super_super int(11) unsigned NOT NULL default '0',
KEY super_emp (super_emp),
CONSTRAINT supervisors_ibfk_1 FOREIGN KEY (super_emp) REFERENCES users (user_id) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Table structure for table `types`
--
CREATE TABLE `types` (
type_id int(10) unsigned NOT NULL auto_increment,
type_name varchar(10) NOT NULL default '',
PRIMARY KEY (type_id)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Table structure for table `users`
--
CREATE TABLE users (
user_id int(10) unsigned NOT NULL auto_increment,
user_name varchar(10) NOT NULL default '',
user_pass varchar(40) NOT NULL default '',
user_first varchar(10) NOT NULL default '',
user_last varchar(20) NOT NULL default '',
user_email varchar(50) NOT NULL default '',
user_phone1 bigint(20) NOT NULL default '0',
user_phone2 bigint(20) default NULL,
user_type int(11) unsigned NOT NULL default '0',
user_pay_rate float(5,2) default '0.00',
PRIMARY KEY (user_id),
KEY user_type (user_type),
CONSTRAINT users_ibfk_1 FOREIGN KEY (user_type) REFERENCES `types` (type_id) ON DELETE NO ACTION ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

how to get autoincrement ID of one table and insert in another table

I have a mysql table whose description is given below
Create Table
CREATE TABLE `question` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`category` varchar(255) DEFAULT NULL,
`is_deleted` bit(1) NOT NULL,
`question` varchar(255) DEFAULT NULL,
`version` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=15 DEFAULT CHARSET=utf8
I have another table
Create Table
CREATE TABLE `parent_question` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`is_deleted` bit(1) NOT NULL,
`version` int(11) DEFAULT NULL,
`pid` bigint(20) NOT NULL,
`qid` bigint(20) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `FK8FEA83DBE860AF9` (`pid`),
KEY `FK8FEA83DBF34C20F6` (`qid`),
CONSTRAINT `FK8FEA83DBE860AF9` FOREIGN KEY (`pid`) REFERENCES `parent` (`id`),
CONSTRAINT `FK8FEA83DBF34C20F6` FOREIGN KEY (`qid`) REFERENCES `question` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=23 DEFAULT CHARSET=utf8
I have the right to insert one row in the question table.So when I add a new row then id will be generated (because it is auto_increment) and I want to store this id in the qid field parent_question table.
Can anybody please tell me how to do it?
Use the last_insert_id() function:
insert into question values (...);
insert into parent_question values (..., last_insert_id(), ...);

MySQL Foreign keys - Error 150 while creating tables

I have written some SQL code to create five tables with several relations(foreign keys).
The first foreign key relation works fine, the tables are created without any errors, but when I try to create gasten_url_toegangscodes I get the following error:
#1005 - Can't create table 'dbname.gasten_url_toegangscodes' (errno: 150).
I've read the docs about foreign keys and I still can't find the problem.. The most strange thing about this is that it works in the first three tables..
Can anyone help me please?
My full SQL code is:
CREATE TABLE IF NOT EXISTS `groepen` (
`id` tinyint(3) NOT NULL AUTO_INCREMENT,
`naam` varchar(50) NOT NULL DEFAULT '',
`status` tinyint(1) NOT NULL DEFAULT '1',
PRIMARY KEY (`id`)
) ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS `gasten` (
`id` int(5) NOT NULL AUTO_INCREMENT,
`bedrijf` varchar(100) NOT NULL DEFAULT '',
`uniek` varchar(64) NOT NULL,
`groepid` tinyint(3) NOT NULL,
PRIMARY KEY (`id`),
FOREIGN KEY (groepid) REFERENCES `groepen` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION
) ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS `passen` (
`id` tinyint(3) NOT NULL AUTO_INCREMENT,
`naam` varchar(30) NOT NULL DEFAULT '',
`color` varchar(7) NOT NULL DEFAULT '#FFFFFF',
`bekend` tinyint(1) NOT NULL DEFAULT '0',
`welkom` tinyint(1) NOT NULL DEFAULT '0',
`aantal` tinyint(1) NOT NULL DEFAULT '0',
`nummer` int(11) NOT NULL DEFAULT '0',
`begrens` tinyint(1) NOT NULL DEFAULT '0',
`status` tinyint(1) NOT NULL DEFAULT '1',
`priority` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
KEY `naam` (`naam`)
) ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS `gasten_url_toegangscodes` (
`uniek` varchar(64) NOT NULL,
`unieke_url_code` char(64) NOT NULL,
`salt` char(16) NOT NULL,
FOREIGN KEY (uniek) REFERENCES `gasten` (`uniek`) ON DELETE CASCADE ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE IF NOT EXISTS `gasten_tickets` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`pas_id` tinyint(3) NOT NULL,
`uniek` varchar(64) NOT NULL,
`barcode` char(16) NOT NULL,
`secret_key` char(32) NOT NULL,
`download_count` int(11) NOT NULL DEFAULT '0',
`last_download_time` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`scanned` enum('N','Y') NOT NULL,
`scanned_datetime` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (`id`),
FOREIGN KEY (uniek) REFERENCES `gasten` (`uniek`) ON DELETE CASCADE ON UPDATE NO ACTION,
FOREIGN KEY (pas_id) REFERENCES `passen` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION
) ENGINE=InnoDB;
Thanks in advance!
Create an index on gasten.uniek
CREATE TABLE IF NOT EXISTS `gasten` (
`id` int(5) NOT NULL AUTO_INCREMENT,
`bedrijf` varchar(100) NOT NULL DEFAULT '',
`uniek` varchar(64) NOT NULL,
`groepid` tinyint(3) NOT NULL,
PRIMARY KEY (`id`),
INDEX `idx_uniek` (`uniek`),
FOREIGN KEY (groepid) REFERENCES `groepen` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION
) ENGINE=InnoDB;
And set the charset of the FK in gasten_url_toegangscodes the same as that of the related column in gasten.
Assuming gasten is UTF-8:
CREATE TABLE IF NOT EXISTS `gasten_url_toegangscodes` (
/* use same charset for this column */
`uniek` varchar(64) NOT NULL CHARSET UTF-8,
`unieke_url_code` char(64) NOT NULL,
`salt` char(16) NOT NULL,
FOREIGN KEY (uniek) REFERENCES `gasten` (`uniek`) ON DELETE CASCADE ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Declare Foreign key in MySQL

I have these database
=== Invoices ===
id
status
description
=== Invoice Items ===
id
invoice_id (FK)
item_name
description
To make this table I have made this MySQL command
CREATE TABLE IF NOT EXISTS `nt_invoices` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`status` varchar(45) NOT NULL DEFAULT '',
`description` text NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=24 ;
CREATE TABLE IF NOT EXISTS `nt_invoice_items` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`invoice_id` int(11) NOT NULL,
`item_name` varchar(45) NOT NULL,
`description` text NOT NULL,
PRIMARY KEY (`id`),
KEY `invoice_id` (`invoice_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=12 ;
My problem is that I want to declare a foreign key in the invoice_items table and to make the invoice_id the foreign key of invoices table id. So how to write that command? Any help and suggestions will be highly appreciated.
MyISAM does not support foreign keys. You need to use InnoDB (which is a better choice in all aspects anyway). Then it's just like in any other SQL dialect:
`invoice_id` int(11) NOT NULL references nt_invoices(id),
P.S. Also, always use utf8 encoding everywhere. It will bite you in the ass if you don't.
You should have innodb engine type for using foreign keys.
CREATE TABLE IF NOT EXISTS `nt_invoice_items` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`invoice_id` int(11) NOT NULL references nt_invoices(id)
`item_name` varchar(45) NOT NULL,
`description` text NOT NULL,
PRIMARY KEY (`id`),
KEY `invoice_id` (`invoice_id`)
) ENGINE=INNODB DEFAULT CHARSET=utf8;
OR if you want to use cascaded update delete:
CREATE TABLE IF NOT EXISTS `nt_invoice_items` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`invoice_id` int(11) NOT NULL,
`item_name` varchar(45) NOT NULL,
`description` text NOT NULL,
PRIMARY KEY (`id`),
KEY `invoice_id` (`invoice_id`),
FOREIGN KEY (invoice_id) REFERENCES nt_invoices(id)
ON DELETE CASCADE
ON UPDATE CASCADE,
) ENGINE=INNODB DEFAULT CHARSET=utf8;
You may also use ALTER command to declare FOREIGN KEY as follows:
Alter table table_name add foreign key(column_name)
references other_table_name(column);

phpmyadmin Ondelete cascade not working

I have Three tables 1) product, 2)product_x, 3) product_y. I have set primary key for these three tables. The tables are
1) product : id, name, product_type, created_at
2) product_x : id, product_id,description, created_at
3) product_y : id,product_id,description, created_at
The product_id table in the product_x and product_y tables are foreign reference from the table product. If product_type is=1 entry will go to product_x and if product_type=0 entry will go to product_1. SO here my problem is i have set delete on cascade for the foreign key reference for these two tables. But when i delete an entry from product_x or product_y the corresponding id from the product table is not deleted. That means delete on cascade in not working. I need help from you guys, Please help.
Here is my product table.
--
-- Table structure for table `product`
--
CREATE TABLE IF NOT EXISTS `product` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(100) NOT NULL,
`code` varchar(100) NOT NULL,
`description` text NOT NULL,
`product_type` tinyint(4) NOT NULL COMMENT '1=pronova product,2=doctor product',
`ingredients` varchar(200) NOT NULL,
`directions` varchar(200) NOT NULL,
`status` tinyint(1) NOT NULL COMMENT '0=inactive,1=active',
`created_at` datetime NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=67 ;
my product_x table
--
-- Table structure for table product_x
CREATE TABLE IF NOT EXISTS `product_x` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`inventory_id` int(11) NOT NULL,
`doctor_id` int(11) NOT NULL,
`stock` varchar(20) NOT NULL,
`image` varchar(50) NOT NULL,
`small_image` varchar(100) NOT NULL,
`sail_price` float DEFAULT NULL,
`acquire_price` float DEFAULT NULL,
`created_at` datetime NOT NULL,
PRIMARY KEY (`id`),
KEY `product_id` (`product`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;
and my product_y table is
CREATE TABLE IF NOT EXISTS `product_y` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`inventory_id` int(11) NOT NULL,
`specialization_type` int(11) NOT NULL,
`stock` varchar(20) NOT NULL,
`image` varchar(100) NOT NULL,
`unit_credit_value` int(11) NOT NULL,
`suggested_price` float NOT NULL,
`list_price` float DEFAULT NULL COMMENT 'the price which this product sold if pronova sold this',
`created_at` datetime NOT NULL,
PRIMARY KEY (`id`),
KEY `inventory_id` (`product`),
KEY `FK50E07CF68B1B2BCE` (`inventory_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;
Check to make sure that you are using InnoDB and not MyISAM, if you really don't have control over it, you could write a trigger as explained below:
How to use delete cascade on MySQL MyISAM storage engine?