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?
Related
I am a self taught CS and I am really novice at mySQL. I created a table called "jobs". I would like to create a new table keywords with 3 columns:
keyword_id as a primary key
job_id as a foreign key from the jobs table
keyword, text
This is the query I wrote:
CREATE TABLE `keywords` (
`keyword_id` int(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY(`keyword_id`),
`keyword` text NOT NULL,
FOREIGN KEY (job_id) REFERENCES jobs(job_id)
) ENGINE=InnoDB AUTO_INCREMENT=28 DEFAULT CHARSET=latin1;
I got this error message:
Key column 'job_id' doesn't exist in table
The current jobs table code is the following:
CREATE TABLE `jobs` (
`title` text NOT NULL,
`type` text NOT NULL,
`location` text NOT NULL,
`salary` int(11) NOT NULL,
`description` text NOT NULL,
`date` date NOT NULL,
`job_id` int(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`job_id`)
) ENGINE=InnoDB AUTO_INCREMENT=28 DEFAULT CHARSET=latin1;
You need to have a column in the keywords table to hold the foreign key.
Like this
CREATE TABLE `jobs` (
`title` text NOT NULL,
`type` text NOT NULL,
`location` text NOT NULL,
`salary` int(11) NOT NULL,
`description` text NOT NULL,
`date` date NOT NULL,
`job_id` int(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`job_id`)
) ENGINE=InnoDB AUTO_INCREMENT=28 DEFAULT CHARSET=latin1;
CREATE TABLE `keywords` (
`keyword_id` int(11) NOT NULL AUTO_INCREMENT,
`keyword` text NOT NULL,
`job_id` int(11) NOT NULL, #<- new column
PRIMARY KEY(`keyword_id`),
FOREIGN KEY (job_id) REFERENCES jobs(job_id)
) ENGINE=InnoDB AUTO_INCREMENT=28 DEFAULT CHARSET=latin1;
I have the following query and I really need help optimizing it:
SELECT min(p.price) as min_price, max(p.price) as max_price
FROM product p
INNER JOIN product_category pc
ON p.id_product = pc.id_product AND
p.id_project = 1 AND
pc.id_category = 2 AND
p.active = 1
The tables are:
CREATE TABLE `product` (
`id_product` bigint(10) UNSIGNED NOT NULL,
`id_project` int(11) NOT NULL,
`reference` varchar(50) NOT NULL,
`reference_internal` varchar(125) NOT NULL,
`sku` varchar(50) NOT NULL,
`price` float(12,2) NOT NULL,
`old_price` float NOT NULL,
`reduction_amount` float NOT NULL,
`reduction_percent` float NOT NULL,
`is_reduced` int(1) NOT NULL,
`id_manufacturer` int(10) NOT NULL,
`id_supplier` int(10) NOT NULL,
`is_new` int(1) NOT NULL,
`popularity` int(1) NOT NULL,
`quantity` int(1) NOT NULL,
`active` int(1) NOT NULL,
`date_add` datetime NOT NULL,
`date_upd` datetime NOT NULL,
`id_category` int(11) NOT NULL,
`indexed` int(1) NOT NULL,
`id_color` int(13) NOT NULL,
`rating` int(1) NOT NULL,
`rating_count` int(11) NOT NULL,
`viewed` int(5) NOT NULL,
`a_id_product` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
ALTER TABLE `product`
ADD PRIMARY KEY (`id_product`),
ADD KEY `id_product` (`id_product`),
ADD KEY `reference` (`reference`),
ADD KEY `reference_internal` (`reference_internal`),
ADD KEY `sku` (`sku`),
ADD KEY `reduction_amount` (`reduction_amount`),
ADD KEY `reduction_percent` (`reduction_percent`),
ADD KEY `is_reduced` (`is_reduced`),
ADD KEY `id_manufacturer` (`id_manufacturer`),
ADD KEY `id_supplier` (`id_supplier`),
ADD KEY `is_new` (`is_new`),
ADD KEY `popularity` (`popularity`),
ADD KEY `date_add` (`date_add`),
ADD KEY `date_upd` (`date_upd`),
ADD KEY `id_category` (`id_category`),
ADD KEY `indexed` (`indexed`),
ADD KEY `id_color` (`id_color`),
ADD KEY `price` (`price`),
ADD KEY `rating` (`rating`),
ADD KEY `a_id_product` (`a_id_product`),
ADD KEY `t7` (`price`,`id_product`,`id_project`,`active`) USING BTREE;
-- AUTO_INCREMENT for dumped tables
-- AUTO_INCREMENT for table `product`
ALTER TABLE `product`
MODIFY `id_product` bigint(10) UNSIGNED NOT NULL AUTO_INCREMENT;
COMMIT;
There product_categories table:
CREATE TABLE `product_category` (
`id_product` int(10) DEFAULT NULL,
`id_project` int(11) NOT NULL,
`id_category` int(5) NOT NULL,
`menu_order` int(5) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
--
-- Indexes for dumped tables
--
--
-- Indexes for table `product_category`
--
ALTER TABLE `product_category`
ADD KEY `id_product` (`id_product`),
ADD KEY `id_project` (`id_project`),
ADD KEY `id_category` (`id_category`),
ADD KEY `menu_order` (`menu_order`),
ADD KEY `t1` (`id_product`,`id_category`),
ADD KEY `t2` (`id_product`,`id_project`,`id_category`);
COMMIT;
The explain statement https://snag.gy/8ClFnh.jpg
I have 45 MS execution time but the database is not very large, i have around 20.000 products but when it's live i will have million of products and the 0.0550 seconds execution time seems very long for this number of products, anyone can help with an optimization suggestion?
I have mysql 5.7.
Your query should be pretty easy to optimize, if the number of rows you are selecting is low (0.5% or less of the total).
The indexes below should help:
create index ix1_product on product (id_project, active, price);
create index ix2_procat on product_category (id_product, id_category);
I am using phpMyAdmin while trying to create a table.
The definition is as follows
CREATE TABLE `koment` (
`movieID` int(11) NOT NULL,
`userID` int(11) NOT NULL,
`id` int(11) NOT NULL,
`text` varchar(255) DEFAULT NULL,
PRIMARY KEY (`movieID`,`userID`,`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
I want to have id as an auto increment variable but it seems that I can't make it thus even though it is a primary key(I needed the id attribute because a user can write many comments on the same movie). So I don't understand why I still get this error. Thanks in advance.
I'd rather advise you to have clear PRIMARY KEY which is AUTO_INCREMENT and UNIQUE KEY like in your example:
CREATE TABLE `koment` (
`movieID` int(11) NOT NULL,
`userID` int(11) NOT NULL,
`id` int(11) NOT NULL AUTO_INCREMENT,
`text` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY (`movieID`,`userID`,`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE `koment` (
`movieID` int(11) NOT NULL,
`userID` int(11) NOT NULL,
`id` int(11) NOT NULL AUTO_INCREMENT,
`text` varchar(255) DEFAULT NULL,
PRIMARY KEY (`movieID`,`userID`,`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
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 ;
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;