473,467 Members | 1,477 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

MySQL - Delete across two tables. OT

Hi, sorry to post OT but i cant find the MySQL newsgroup, however i am
hoping to pick up on some expert advice from php/mysql gurus here. I'm
having some trouble performing a delete across two tables.

The tables i have are:

questionnaires (id, name);

questionnaire_questions (questionnaires_id, id, name, qf_type)

The questionnaire_questions table contains a list of questions for a
specific questionnaire (indicated by the questonnaires_id). I would
like to remove a questionnaire, and when doing so all corresponding
questionnaire_questions who have matching id's (questionnaires.id =
questionnaire_questions.questionnaires_id).

I have this statement, however it only works if a questionnaire has
questions... however i would like it to delete even if no questions
exist for that specific questionnaire. The SQL i am using is:

delete questionnaires, questionnaire_questions FROM questionnaires,
questionnaire_questions WHERE questionnaires.id =
questionnaire_questions.questionnaires_id AND questionnaires.id =
THE_QUESTIONAIRE_TO_DELETE_ID

Thanks in advance, any help much appreciated.

Chris
Jul 17 '05 #1
4 5293
Chris wrote:
Hi, sorry to post OT but i cant find the MySQL newsgroup,
however i am hoping to pick up on some expert advice from
php/mysql gurus here. I'm having some trouble performing a
delete across two tables.

The tables i have are:

questionnaires (id, name);

questionnaire_questions (questionnaires_id, id, name, qf_type)

The questionnaire_questions table contains a list of questions
for a specific questionnaire (indicated by the
questonnaires_id). I would like to remove a questionnaire, and
when doing so all corresponding questionnaire_questions who
have matching id's (questionnaires.id =
questionnaire_questions.questionnaires_id).

I have this statement, however it only works if a
questionnaire has questions... however i would like it to
delete even if no questions exist for that specific
questionnaire. The SQL i am using is:

delete questionnaires, questionnaire_questions FROM
questionnaires, questionnaire_questions WHERE
questionnaires.id = questionnaire_questions.questionnaires_id
AND questionnaires.id = THE_QUESTIONAIRE_TO_DELETE_ID

Thanks in advance, any help much appreciated.

Chris


It is safer to do this in two steps: First delete the questions
and then delete the questionaire. The reason I say this is
that the optimizer (either now or in the future) may decide to
delete the questionaire first and then delete the questions --
which is fine as long as the transaction always runs to
completion. The problem is that if the transaction doesn't run
to completion you can wind up with orphaned questions.
--
Jerry Gitomer
Jul 17 '05 #2
If you have the questionaire ID, then just use 2 queries.

DELETE FROM questionaire_questions
WHERE f_questionaireID = QUESTIONAIREID

DELETE FROM questionaires
WHERE questionaireID = QUESTIONAIREID

Unless of course you are not working in a semi-persistent environment
(like PHP)...if not, you may have to create variables in the mysql
itself (go to mysql.com for this, because I havent done it in years)

Chris wrote:
Hi, sorry to post OT but i cant find the MySQL newsgroup, however i am hoping to pick up on some expert advice from php/mysql gurus here. I'm having some trouble performing a delete across two tables.

The tables i have are:

questionnaires (id, name);

questionnaire_questions (questionnaires_id, id, name, qf_type)

The questionnaire_questions table contains a list of questions for a
specific questionnaire (indicated by the questonnaires_id). I would
like to remove a questionnaire, and when doing so all corresponding
questionnaire_questions who have matching id's (questionnaires.id =
questionnaire_questions.questionnaires_id).

I have this statement, however it only works if a questionnaire has
questions... however i would like it to delete even if no questions
exist for that specific questionnaire. The SQL i am using is:

delete questionnaires, questionnaire_questions FROM questionnaires,
questionnaire_questions WHERE questionnaires.id =
questionnaire_questions.questionnaires_id AND questionnaires.id =
THE_QUESTIONAIRE_TO_DELETE_ID

Thanks in advance, any help much appreciated.

Chris


Jul 17 '05 #3
On 6 Dec 2004 07:19:30 -0800, g1**@hotmail.com (Chris) wrote:
Hi, sorry to post OT but i cant find the MySQL newsgroup, however i am
hoping to pick up on some expert advice from php/mysql gurus here. I'm
having some trouble performing a delete across two tables.

The tables i have are:

questionnaires (id, name);

questionnaire_questions (questionnaires_id, id, name, qf_type)

The questionnaire_questions table contains a list of questions for a
specific questionnaire (indicated by the questonnaires_id). I would
like to remove a questionnaire, and when doing so all corresponding
questionnaire_questions who have matching id's (questionnaires.id =
questionnaire_questions.questionnaires_id).

I have this statement, however it only works if a questionnaire has
questions... however i would like it to delete even if no questions
exist for that specific questionnaire. The SQL i am using is:

delete questionnaires, questionnaire_questions FROM questionnaires,
questionnaire_questions WHERE questionnaires.id =
questionnaire_questions.questionnaires_id AND questionnaires.id =
THE_QUESTIONAIRE_TO_DELETE_ID


Add a foreign key constraint from questionnaire_questions to questionnaires
(you should have this anyway) and declare it as 'on delete cascade'.

http://dev.mysql.com/doc/mysql/en/In...nstraints.html

mysql> create table questionnaires (
-> id int not null auto_increment primary key,
-> name varchar(16) not null
-> )
-> TYPE=INNODB;
Query OK, 0 rows affected (0.35 sec)

mysql> create table questionnaire_questions (
-> questionnaires_id int not null,
-> id int not null auto_increment primary key,
-> name varchar(16) not null,
-> index (questionnaires_id),
-> foreign key (questionnaires_id)
-> references questionnaires (id)
-> on delete cascade
-> )
-> TYPE=INNODB;
Query OK, 0 rows affected (0.03 sec)

mysql> insert into questionnaires values (null, 'Quest.1');
Query OK, 1 row affected (0.05 sec)

mysql> insert into questionnaires values (null, 'Quest.2');
Query OK, 1 row affected (0.00 sec)

mysql> insert into questionnaire_questions values (1, null, 'Q1');
Query OK, 1 row affected (0.02 sec)

mysql> select * from questionnaires;
+----+---------+
| id | name |
+----+---------+
| 1 | Quest.1 |
| 2 | Quest.2 |
+----+---------+
2 rows in set (0.03 sec)

mysql> select * from questionnaire_questions;
+-------------------+----+------+
| questionnaires_id | id | name |
+-------------------+----+------+
| 1 | 1 | Q1 |
+-------------------+----+------+
1 row in set (0.01 sec)

mysql> delete from questionnaires;
Query OK, 2 rows affected (0.08 sec)

mysql> select * from questionnaire_questions;
Empty set (0.00 sec)

--
Andy Hassall / <an**@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
Jul 17 '05 #4
Many thanks for spending the time and replying chaps, you have all been
a great help. Whilst waiting for replies i did indeed implement a two
stage 'delete'... however the cascade approach is interesting.
Thanks again :)

Chris

Jul 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: red85 | last post by:
hello i have mysql 4.1 with win2000 SP3, i know that it is only an alpha and i don't know if someone else has already posted this problem: when i execute this sql UPDATE tableX SET...
0
by: Mike Chirico | last post by:
Interesting Things to Know about MySQL Mike Chirico (mchirico@users.sourceforge.net) Copyright (GPU Free Documentation License) 2004 Last Updated: Mon Jun 7 10:37:28 EDT 2004 The latest...
3
by: saracen44 | last post by:
Hi I have MyISAM tables When I'm deleting parent I want to delete children in the same time. How can I do It? What are possibilities? Thanks
39
by: Mairhtin O'Feannag | last post by:
Hello, I have a client (customer) who asked the question : "Why would I buy and use UDB, when MySql is free?" I had to say I was stunned. I have no experience with MySql, so I was left sort...
4
by: Robert Blackwell | last post by:
A while ago I had some help from here to make a batch file that would dump my db. Everything was working just fine until 2 days ago and I just found out. I checked my backup folder and found...
7
by: Randy | last post by:
Folks: We have a web-based app that's _really_ slowing down because multiple clients are writing their own private data into a single, central database. I guess the previous programmer did...
0
by: Bucker | last post by:
Could someone view the following that I copied from phpMyAdmin and tell me from the statistics if are server is running OK? Does it look like we will have problems as more people hit our server?...
1
by: PowerLifter1450 | last post by:
I've been having a very rough time installinig mySQL on Linux. I have been following the instructions form here: http://www.hostlibrary.com/installing_apache_mysql_php_on_linux Everytime I get to...
6
Atli
by: Atli | last post by:
This is an easy to digest 12 step guide on basics of using MySQL. It's a great refresher for those who need it and it work's great for first time MySQL users. Anyone should be able to get...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.