473,405 Members | 2,300 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,405 software developers and data experts.

Complicate delete-statement involving two tables

Or, at least, I find it complicated :-)

Hopefully it is not that hard but here goes:

I have two tables:
Persons and Relations

Persons contain personal data such as person_ID, name, adresse, etc.
and a special-status-code (a person with this code set = 1 is a VIP)

Relations contains a person_ID and a vip_ID (the vip_ID is in fact a
person_ID - it is just a person with his special-status-code = 1). So
the relation table is used to register who a VIP knows (his contacts).

My problem is when I delete a VIP.

I have no problems just deleting the VIP from the Persons-table, but I
also need to delete all of his contacts (not only from the
Relations-table but also from the Persons-table).

I think this might be part of the solution:
ALTER PROCEDURE Delete_Vip
(
@Vip_ID INT /* ID of the VIP to be deleted */
)

AS

DELETE
FROM Persons p
WHERE r.Vip_ID = @Vip_ID
AND p.Person_ID NOT IN (
SELECT * FROM Relations r WHERE Vip_ID <> @Vip_ID
)

This does not work, but the idea in the subquery is to NOT delete
persons that are known by other VIP's than the one I am deleting. In
other words I am only interested in the VIP's exclusive contacts.

I am also quite sure that the above SQL-statement does not remove the
VIP's posts in the Relations-table.
I am open to suggestions :-)

Have a splendid weekend!
Jul 23 '05 #1
1 5700
Swaq wrote:
I have no problems just deleting the VIP from the Persons-table, but I
also need to delete all of his contacts (not only from the
Relations-table but also from the Persons-table).

I think this might be part of the solution:
ALTER PROCEDURE Delete_Vip
(
@Vip_ID INT /* ID of the VIP to be deleted */
)


Your first problems is that you are using the Microsoft SQL Server
syntax: '@' to introduce variables. MySQL syntax is different. Refer
to the MySQL 5.x documentation for examples of proper procedure language
syntax. Also, I assume you're aware that procedures are only available
in MySQL 5, and this is still in a Beta state -- not recommended for
production use.

I also recommend you name your procedure's input parameter something
different from your field name, to avoid confusion. For instance,
"Vip_ID_to_delete".

Does the following get you closer to your solution?

// Delete the VIP and all his relationships.
DELETE FROM Persons, Relations
USING Persons P JOIN Relations R ON (P.Person_ID = R.Vip_ID)
WHERE P.Person_ID = Vip_ID_to_delete;

// Then delete all persons for whom no VIP relationship exists;
// skip those persons for whom a relationship still exists, because
// it must be for a different VIP, following the above operation.
DELETE FROM Persons
USING Persons P LEFT OUTER JOIN Relations R
ON (P.Person_ID = R.Vip_ID)
WHERE R.Vip_ID IS NULL;

Regards,
Bill K.
Jul 23 '05 #2

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

Similar topics

3
by: Nimmi Srivastav | last post by:
There's a rather nondescript book called "Using Borland C++" by Lee and Mark Atkinson (Que Corporation) which presents an excellent discussion of overloaded new and delete operators. I am...
1
by: Douglas Peterson | last post by:
class Allocator { public: virtual void * Alloc(size_t) = 0; virtual void * Free(void*) = 0; }; class Object { public:
2
by: Dave | last post by:
Hello all, I'd like to find a source on the web that discusses, in a comprehensive manner and in one place, everything about new / delete. It should include overloading operator new, the new...
6
by: Tamir Khason | last post by:
Is there "smart" way of converting DataSet to Class ???
3
by: silver360 | last post by:
Hello, I'm trying to create a basic Heap manager and i have some question about new/delete overloading. The following code give me this output : >> $./heap >> registered : 0x804d098 >>...
5
by: junw2000 | last post by:
I use the code below to study delete and destructor. #include <iostream> using namespace std; struct A { virtual ~A() { cout << "~A()" << endl; }; //LINE1 void operator delete(void* p) {...
9
by: rohits123 | last post by:
I have an overload delete operator as below ////////////////////////////////// void operator delete(void* mem,int head_type) { mmHead local_Head = CPRMemory::GetMemoryHead(head_type);...
12
by: yufufi | last post by:
Hello, How does delete know how much memory to deallocate from the given pointer? AFAIK this informations is put there by new. new puts the size of the allocated memory before the just before...
10
by: jeffjohnson_alpha | last post by:
We all know that a new-expression, foo* a = new foo() ; allocates memory for a single foo then calls foo::foo(). And we know that void* p = ::operator new(sizeof(foo)) ; allocates a...
1
by: =?Utf-8?B?QmlsbHkgWmhhbmc=?= | last post by:
I have a asp.net app, in client I create a Complicate json string, for example: { a: ‘a’, b : { b ; ‘b’ } } If .net has corresponding type, it could be deserialized, for example: ...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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,...
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
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...
0
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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,...

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.