473,585 Members | 2,496 Online
Bytes | Software Development & Data Engineering Community
+ 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_q uestions (questionnaires _id, id, name, qf_type)

The questionnaire_q uestions table contains a list of questions for a
specific questionnaire (indicated by the questonnaires_i d). I would
like to remove a questionnaire, and when doing so all corresponding
questionnaire_q uestions who have matching id's (questionnaires .id =
questionnaire_q uestions.questi onnaires_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_q uestions FROM questionnaires,
questionnaire_q uestions WHERE questionnaires. id =
questionnaire_q uestions.questi onnaires_id AND questionnaires. id =
THE_QUESTIONAIR E_TO_DELETE_ID

Thanks in advance, any help much appreciated.

Chris
Jul 17 '05 #1
4 5304
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_q uestions (questionnaires _id, id, name, qf_type)

The questionnaire_q uestions table contains a list of questions
for a specific questionnaire (indicated by the
questonnaires_i d). I would like to remove a questionnaire, and
when doing so all corresponding questionnaire_q uestions who
have matching id's (questionnaires .id =
questionnaire_q uestions.questi onnaires_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_q uestions FROM
questionnaires, questionnaire_q uestions WHERE
questionnaires. id = questionnaire_q uestions.questi onnaires_id
AND questionnaires. id = THE_QUESTIONAIR E_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_qu estions
WHERE f_questionaireI D = 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_q uestions (questionnaires _id, id, name, qf_type)

The questionnaire_q uestions table contains a list of questions for a
specific questionnaire (indicated by the questonnaires_i d). I would
like to remove a questionnaire, and when doing so all corresponding
questionnaire_q uestions who have matching id's (questionnaires .id =
questionnaire_q uestions.questi onnaires_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_q uestions FROM questionnaires,
questionnaire_q uestions WHERE questionnaires. id =
questionnaire_q uestions.questi onnaires_id AND questionnaires. id =
THE_QUESTIONAIR E_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.co m (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:

questionnair es (id, name);

questionnaire_ questions (questionnaires _id, id, name, qf_type)

The questionnaire_q uestions table contains a list of questions for a
specific questionnaire (indicated by the questonnaires_i d). I would
like to remove a questionnaire, and when doing so all corresponding
questionnaire_ questions who have matching id's (questionnaires .id =
questionnaire_ questions.quest ionnaires_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_q uestions FROM questionnaires,
questionnaire_ questions WHERE questionnaires. id =
questionnaire_ questions.quest ionnaires_id AND questionnaires. id =
THE_QUESTIONAI RE_TO_DELETE_ID


Add a foreign key constraint from questionnaire_q uestions 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_q uestions (
-> 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_q uestions 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_q uestions;
+-------------------+----+------+
| 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_q uestions;
Empty set (0.00 sec)

--
Andy Hassall / <an**@andyh.co. uk> / <http://www.andyh.co.uk >
<http://www.andyhsoftwa re.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
3993
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 fieldX=valueX WHERE id IN (SELECT tX.id FROM tableX AS tX WHERE tX.fieldY=valueY AND filedZ=valueZ);
0
3938
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 version of this document can be found at: http://prdownloads.sourceforge.net/souptonuts/README_mysql.txt?download
3
8707
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
8390
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 of stammering and sputtering, and managed to pull out something I heard a couple of years back - that there was no real transaction safety in MySql....
4
3688
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 that starting on 4/25/2006 the dump files are empty and only shows a few lines of comments or something instead of creating a normal dump as it had...
7
4180
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 things this way because it made things easy. Well, I'm the person that has to put up with the long-term headache. Anywho, someone at work wants things...
0
6626
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? I'm not a db admin, so any help would be grateful. Regards,
1
5268
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 #./configure it goes through all of the preparing tables and starting mysqlServer and daemon, but than immediaetly says "mysql ended" -- I try to do...
6
38484
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 through this without much trouble. Programming knowledge is not required. Index What is SQL? Why MySQL? Installing MySQL. Using the MySQL...
0
7908
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7836
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8199
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8336
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7950
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
5389
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3835
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2343
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 we have to send another system
1
1447
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.