473,386 Members | 1,745 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Deleting many-to-many realtionships

dickiedyce
I have a simple many-to-many three-table set-up:

Expand|Select|Wrap|Line Numbers
  1. CREATE TABLE `people` (
  2.   `id` int(10) unsigned NOT NULL auto_increment,
  3.   `salutation` varchar(15) default NULL,
  4.   `forename` varchar(60) NOT NULL default '',
  5.   `middle_initial` varchar(5) NOT NULL default '',
  6.   `surname` varchar(60) NOT NULL default '',
  7.   `suffix` varchar(10) default NULL,
  8.   `is_tenant` tinyint(1) NOT NULL default '0',
  9.   PRIMARY KEY  (`id`),
  10.   KEY `is_tenant` (`is_tenant`),
  11.   KEY `fullname` (`surname`,`forename`)
  12. )
  13.  
  14. CREATE TABLE `phones` (
  15.   `id` int(10) unsigned NOT NULL auto_increment,
  16.   `phone_label` varchar(127) NOT NULL default '',
  17.   `notes` varchar(127) NOT NULL default '',
  18.   `number` varchar(255) NOT NULL default '',
  19.   PRIMARY KEY  (`id`)
  20. )
  21.  
  22. CREATE TABLE `person_phones` (
  23.   `person_id` int(10) unsigned NOT NULL,
  24.   `phone_id` int(10) unsigned NOT NULL,
  25.   PRIMARY KEY  (`person_id`,`phone_id`),
  26.   KEY `phone_id` (`phone_id`)
  27. )
mapping a person to a number of phones (phones can also be mapped to organisations - another table - which explains why I'm not using a simple one-to-many).

Starting from a value of people.id (which corresponds to person_phones.person_id) I want to be able to delete the link in person_phones and the corresponding entry in phones. I realise that I have to do this in reverse order using some sort of join or sub-select to delete the phones first, and then the link, but for the life of me I can't get the syntax right.

Anyone have any ideas?
Oct 5 '07 #1
2 1103
Sorry - managed to fix problem myself - was making it over complicated:

Expand|Select|Wrap|Line Numbers
  1. delete from phones where id in (select phone_id from person_phones where person_id = 5396);
Again, apologies.
Oct 5 '07 #2
Atli
5,058 Expert 4TB
Hi. Welcome to The Scripts!

I'm glad you were able to solve your problem.
Don't hesitate to post again if you have any questions or problems we can help with.
Oct 5 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Sebastian | last post by:
Hi I got a little problem with deleting from my database, i'm making a simple newsscript. I get a Parse error: syntax error, unexpected T_ELSE on line 24, I have written the error as a comment...
1
by: Wilfredo Sanchez | last post by:
The docs for imaplib are pretty scant, and i guess the library is meant for people who already know IMAP well and want low-level access to the protocol, as opposed to someone who wants "get me the...
3
by: Savas Ates | last post by:
i cant delete data from excell which i use as a database with asp .. i encounter an error report about isam. how can i delete data from excell help meee!
4
by: Dariusz Tomoń | last post by:
Hi How could I perform the task of deleting or renaming file on the server where I have ISS via ASP page. best ragards Derek
1
by: Mark | last post by:
Not sure this is the right place for this questions, but here goes: I get an error message when deleting an table from a Access database. The code is as follows and the error message is after...
15
by: Rick | last post by:
Hi, Does deleting an object more than one times incur undefined behavior? I think it doesn't but just making sure... thanks Rick
1
by: Microsoft News Group | last post by:
I am setting up a customer portal. I am setting up users, and then deleting them. But if I set up a user with the same name I deleted. Then I get this error. The user 'Flynn' is already in...
8
by: Christian Bruckhoff | last post by:
Hi. I got a problem with deleting items of a vector. I did it like this: void THIS::bashDelPerson() { cout << "Bitte Suchstring eingeben: "; char search; cin >search;...
1
by: vikram.lakhotia | last post by:
Hi Have you tried deleting a directory in Asp.Net. Here is an article discussing the problem deleting a Directory in Asp.Net http://www.vikramlakhotia.com/Deleting_Directory_in_ASPnet_20.aspx...
3
gauravgmbhr
by: gauravgmbhr | last post by:
hi friends I have been doing testing on an existing project The tables in postgresQl Db has some trigger function using keyword 'DELETING' IF DELETING THEN SOME SELECT STATEMENT...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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...
0
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,...

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.