I'm working on a little support (ticket) system. My tables are tickets and ticket_replies.
Design of tickets table is
id|user_id|title|...
Design of ticket_replies looks like:
id|ticket_id|...
The foreign key I added looks like this:
ALTER TABLE `ticket_replies` ADDFOREIGN KEY (`ticket_id`)
REFERENCES `sampleauth`.`tickets`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;
Now when I delete a "ticket" in the "ticket" table it gets deleted in "ticket_replies" too. The other way this doesn't work, all in all I would like this to work the other way too, so my database has all the time consistency. How to do so?
Add this trigger will delete its primary key when you try to delete foreign key
CREATE TRIGGER `ticket_replies_BEFORE_DELETE` BEFORE DELETE ON `ticket_replies` FOR EACH ROW
BEGIN
DELETE FROM tickets WHERE id = OLD.ticket_id;
END
Related
I have one table referencing another. As I see - there are two ways to delete cascading:
What is the difference between CREATE TRIGGER BEFORE DELETE and FOREIGN KEY ON DELETE? Are there any differences in performance?
I came up with this advantage of FOREIGN KEY:
The cascading delete is more obvious because it's attached in the table definition.
Full question:
I have the two tables:
project(id, ...) <- works_on(id, project_id, ...)
What are the differences in
CREATE TABLE works_on (
...
FOREIGN KEY (project_id) REFERENCES project ON DELETE CASCADE
...
);
and
CREATE TRIGGER trigger_delete_cascading
BEFORE DELETE ON project
DELETE works_on
WHERE project_id = id;
A FOREIGN KEY will restrict values that can be stored in the project_id column of the works_on table. You will not be able to set a value that does not exist in the project table.
A TRIGGER does not restrict the range of values that can be stored.
If wrote trigger BEFORE delete,will DELETE record from CHILD TABLE and due to some Server error or Other constraint if record is unable to delete from MAIN TABLE(PARENT) then it makes redundant data.
So whenever you required delete plus more action like maintaining LOG table then only you have to go with Trigger.Otherwise ON DELETE CASCADE is great to work.
Hope this will helps you.
When I execute the next SQL code:
delete from product where idproduct = 2;
I get the next error:
Error Code: 1451. Cannot delete or update a parent row: a foreign key constraint fails (`store`.`sale_detail`, CONSTRAINT `fk_sale_detail_product1` FOREIGN KEY (`idproduct`) REFERENCES `product` (`idproduct`) ON DELETE NO ACTION ON UPDATE NO ACTION)
when I try to delete the child of my table dont want, but however I just want to delete product rows without delete brands..
Image of brand table: Brand table
Image of product table: Product table
You can't since that violates Referencial Integrity Constraint rule. You should chose cascading option saying ON DELETE CASCADE.
You need to delete the child row first and then delete the parent row.
You can temporarily disable FK check saying SET FOREIGN_KEY_CHECKS=0; and enable them once done with deletion.
Drop all FK constraint, perform delete and then recreate FK constraint again.
Best option: Go for a soft delete by having a Status column in your table and just update that column to Deleted instead.
You have a foreign key constraint on your store.sale_detail table in the idproduct column. So if you are trying to delete a product you need to be sure that product is not referenced on the sale_detail table.
Another solution is to modify your foreign key so when you delete a row on the product table the referenced row on the sale_detail gets NULL on the corresponding column. Actually you have:
ON DELETE NO ACTION
But you can have:
ON DELETE SET NULL
Also, you can force to delete all references on the sale_detail table:
ON DELETE CASCADE
Hope it helps.
Although there are some similar questions about the subject, I can't find the right answer for my problem. I have 2 tables called customer and car. What I want to do is this: When I delete a customer from database, I want the car that belongs to that customer will be automatically deleted as well. The code that MySQL Workbench generated for me is this:
ALTER TABLE `autocare`.`car`
ADD CONSTRAINT `customerId`
FOREIGN KEY (`CUSTOMER_ID`)
REFERENCES `autocare`.`customer` (`ID`)
ON DELETE CASCADE
ON UPDATE RESTRICT;
And I get this error:
ERROR 1452: Cannot add or update a child row: a foreign key constraint fails
(`autocare`.`#sql-80c_388`, CONSTRAINT `customerId` FOREIGN KEY (`CUSTOMER_ID`)
REFERENCES `customer` (`ID`) ON DELETE CASCADE)
There was no relation between those tables before. Any ideas? Thanks in advance!
Your goal is ultimately to implement a cascading deletion from customer to car. When you attempt to add the constraint with the tables as they are now, it fails because the car table must include rows having a a CUSTOMER_ID value which does not currently exist in the parent customer table.
You should first locate those orphan rows and delete them (since your goal is to delete them anyway). You can find them with a query like:
SELECT *
FROM car
WHERE
CUSTOMER_ID NOT IN (SELECT ID FROM customer)
Once the orphan records are removed, the foreign key constraint can be met by the remaining existing rows and your ALTER TABLE statement will succeed.
I have a mysql database with a lot of tables. Each table has InnoDB format.
I have a table called "companies". Each company has an account, account has transactions and there are lots of other related tables.
I want to delete some companies and all data related to them.
Each foreign key has "NO ACTION" on delete event.
How could I temporary delete all relations without manually setting ON DELETE CASCADE for each foreign key?
If you want to change fk on delete compartament you will do something like this
ALTER TABLE `table1`
ADD CONSTRAINT `fk_name`
FOREIGN KEY (`fk_table2_id`) REFERENCES `table2` (`t2`) ON DELETE CASCADE;
I'm deleting selected rows from both table in MYSQL, the two tables have foreign keys.
DELETE d,b
FROM A as b
INNER JOIN B as d on b.bid=d.bid WHERE b.name LIKE '%xxxx%';
MYSQL complains about foreign keys even though I'm trying to delete from both tables:
Error: Cannot delete or update a parent row: a foreign key constraint
fails (`yyy/d`, CONSTRAINT `fk_d_bid` FOREIGN KEY (`bid`) REFERENCES
`b` (`bid`) ON DELETE NO ACTION ON UPDATE NO ACTION)
what's the best solution here to delete from both table?
Change this constraint to use ON DELETE CASCADE -- which means that if a row is deleted, then any "child" rows will be automatically deleted as well.
Of course take good care of using CASCADE -- only use it when necessary. If you're overzealous with it, and accidentally do a well-placed DELETE, it might end up deleting half of your database. :)
See documentation on foreign key constraints.
I think I see what you're trying to do
If you can't change the table structure, then you could use 2 statements, the first with a sub-select
delete from B where bid IN (select bid from A where name like '%xxxx%');
delete from A where name like '%xxxx%';