Advertisement

10.06.2008 at 04:09PM PDT, ID: 23792280
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

9.0

Best practices for deleting records in multiple tables

Asked by Simon336697 in MySQL Server

Hi everyone hope you are all well.
Guys im struggling with the concept of the best practices in MySQL to delete records from multiple tables in the one go.

I posted a previous question whereby some of you nice gurus said a way of doing this is to use
ON DELETE CASCADE
but they dont use this method in applications, they only use it for database cleanup.

My question is........... if this is only used for database cleanup, then if you have to remove records from multiple tables, then if you dont use the ON DELETE CASCADE, then what do you use if you have FOREIGN KEYS?

Basically, I want to be able to:
# Delete a project even if it has associated tasks in the currenttasks_ctk table, but NOT have those tasks orphaned.
# Delete a task in the currenttasks_ctk table and not delete the project from the projects_pro table.

For example, I built the below 2 tables, and the only reason I added a foreign key to the currenttasks_ctk table was so that if I added a new task, then the record will only be allowed if it is associated with a project. I also dont want orphaned tasks records (that is, dont have an associated project).  If my design is flawed, I would love your help, and would love to know what you guys normally do in cases like this?

Do you guys suggest that I should modify my table design somehow?

Currently,

# I CAN remove tasks from the currenttasks_ctk table without removing the project this task is associated with.
# I CANNOT remove project records, and im using the following for example.
delete from projects_pro where id_pro = '10';

The error i get is:

Cannot delete or update a parent row: a foreign key constraint fails (`tasksdb/currenttasks_ctk`, CONSTRAINT `currenttasks_ctk_ibfk_1` FOREIGN KEY (`fk_id_pro_ctk`) REFERENCES `projects_pro` (`id_pro`))

I have been advised that using the ON CASCADE DELETE is not recommended, but dont know what else to do in the situation where you use foreign keys.
If you guys use foreign keys, what do you guys do if you need to delete records if you dont use ON CASCADE?


Any help on this greatly appreciated.
Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
/**********************************
Create the projects_pro table:
***********************************/
 
create table projects_pro (
id_pro int(4) not null auto_increment,
name_pro varchar(65) not null,
primary key (id_pro),
unique (name_pro)
)
engine=innodb;
 
/**********************************
Create the currenttasks_ctk table:
***********************************/
 
create table currenttasks_ctk (
id_ctk int(4) not null auto_increment,
name_ctk varchar(65) not null,
fk_id_pro_ctk int(4) not null,
description_ctk text,
foreign key (fk_id_pro_ctk) references projects_pro (id_pro),
primary key (id_ctk, fk_id_pro_ctk),
unique (name_ctk)
)
engine=innodb;
[+][-]10.06.2008 at 05:02PM PDT, ID: 22655561

View this solution now by starting your 14-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: MySQL Server
Sign Up Now!
Solution Provided By: dqmq
Participating Experts: 1
Solution Grade: A
 
 
[+][-]10.06.2008 at 05:20PM PDT, ID: 22655624

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 14-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10.06.2008 at 05:23PM PDT, ID: 22655630

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 14-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10.06.2008 at 05:42PM PDT, ID: 22655717

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 14-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20081112-EE-VQP-43 / EE_QW_2_20070628