Showing posts with label related. Show all posts
Showing posts with label related. Show all posts

Thursday, March 29, 2012

Deleting rows from many tables

What I'm trying to do is delete a user and all their related information within the other tables. I'm not wanting to delete the table, just one column with that user and their related information. So my Primary_Key is UserID within the table [alumni] and my three Foreign_Keys are CommentID, PhotoID, and AlbumID within the tables [comments], [photos], and [albums]. Here is some of the code that I have:

<asp:SqlDataSourceID="SqlDataSource2"runat="server"ConnectionString="<%$ ConnectionStrings:SoderquistString %>"DeleteCommand="DELETE FROM [alumni] WHERE [UserID] = @.UserID"SelectCommand="SELECT [UserID], [UserName], [FirstName], [LastName], [State] FROM [alumni] WHERE ([State] = @.State)"><DeleteParameters><asp:ParameterName="UserID"Type="Int32"/></DeleteParameters><SelectParameters><asp:ControlParameterControlID="DropDownList1"Name="state"PropertyName="SelectedValue"Type="String"/></SelectParameters></asp:SqlDataSource>

The users are set up in GridView form. Is there some type of DELETE command that I need to be writing that is different than the one above? I have tried adding onto the following DELETE statment:

DeleteCommand="DELETE FROM [alumni] WHERE [UserID] = @.UserID

DELETE FROM [photo] WHERE [UserID] = @.UserID;

DELETE FROM [album] WHERE [UserID] = @.UserID;

DELETE FROM [comment] WHERE [UserID] = @.UserID;

...but that doesn't work...and doesn't look right. I would really appreciate anyones suggestions or help that you may be able to provide. Thank you!

Try this. I tested for two tables

DeleteCommand="DELETE FROM [alumni] WHERE [UserID] = @.UserID;DELETE FROM [photo] WHERE [UserID] = @.UserID;DELETE FROM [album] WHERE [UserID] = @.UserID;DELETE FROM [comment] WHERE [UserID] = @.UserID"

|||

Thanks for the info. I've tried that already and I still get an error:

Invalid column name 'UserID'.

Description:An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details:System.Data.SqlClient.SqlException: Invalid column name 'UserID'.

Any suggestions?

|||

Do you have UserID column for all tables you are going to delete?

Since I tested the syntax for only two tables, I will test for more tables to confirm whether the syntax works for multiple tables.

|||

This DeleteCommand works on the dummy test tables in SQL Server 2005:

DeleteCommand="DELETE FROM [T1] WHERE [ID1] = @.ID;DELETE FROM [T2] WHERE [ID1] = @.ID;DELETE FROM [T3] WHERE [ID1] = @.ID;DELETE FROM [T4] WHERE [ID1] = @.ID;DELETE FROM [T5] WHERE [ID1] = @.ID"<DeleteParameters><asp:ParameterName="ID"Type="Int32"/></DeleteParameters>

But if you click delete rows too fast, you may get error message for the parameter. You should be able to delete from a stored procedure with error checking from there.

|||

Limno, thanks and I appreciate the help you have given me. I was looking over my tables and noticed that there is a difference in one of them. I'm going to write out the four tables and a few columns that I have within them and hopefully this will help clear a few things up that I'm having trouble with:

alumni - UserID (PK), FirstName, LastName, UserName

photo - PhotoID (FK), AlbumID, PhotoPath

album - AlbumID (FK), UserID, PhotoPath, AlbumName

comments - CommentID (FK), UserID, Comment, UserName, CommentDate

What I have is one primary_key, which is UserID and the other three are foreign_keys (PhotoID, AlbumID, and CommentID). All of the foreign keys have UserID in them except for the photo table because it associates with the album table that has UserID in it. That was the one difference that I forgot about.

So is it possible, with the way I have this set up for me to be able to delete a user from the alumni table along with the other information in the other tables that are associated with that user? I can delete a user if there are no albums, photos or comments connected with that person, but it's when they have something connected to them a problem comes up because of certain constraints.

Thank you in advanced!

|||

Hello:

I tested with the following DELETECommand:

DeleteCommand="DELETE FROM [photo] WHERE ([phtoID] IN (SELECT photoID FROM album WHERE userID = @.userID)); DELETE FROM [comments] WHERE [userID] = @.userID;DELETE FROM [alumni] WHERE [userID] = @.userID;DELETE FROM [album] WHERE [userID] = @.userID"

It seems that the cascading delete through a trigger would be a perfect fit for your case. However, I rearrange the delete action sequence for your case in this DeleteCommand. I tested with success. But you need to test on your data to make sure it works properly. If you want to control the flow, you may need to go with the trigger and/or stored procedure to handle your multiple cascading deletion.

|||

It worked! Thank you for your time, I really appreciate it. It looks like my problem was with the way I was deleting the information from the photo table and how it was related to the albums. This peace of code here...

DELETE FROM [photo] WHERE ([phtoID] IN (SELECT photoID FROM album WHERE userID = @.userID));

...did the trick.

Just for my understanding, what did you mean by "If you want to control the flow, you may need to go with the trigger and/or stored procedure to handle your multiple cascading deletion"? Thanks again!

|||

The code works but you have no way to know if something wrong with it during deletion. I did run into a few times during my test. That is what I mean. If the project becomes critical, you can go with the trigger and stored procedure. Or You can use your own error catch logic from code behind. Just my thought for your situation. I want to know what will happen if there is an error for learning purpose.

I am glad this hack works for you now. If you are interested, you can search for the topic on cascading deleting. The constrains on the foreign keys are very usful to maintain the integrity of your data set.

Tuesday, March 27, 2012

Deleting records

How do I delete one record from one table and cascade down all related tables?
Mike BKBA1 (http://support.microsoft.com/default.aspx?scid=kb;en-us;825021) & KBA2 (http://support.microsoft.com/?scid=kb;en-us;Q142480) to highlight issue if any trigger is involved.

This one too http://www.sqlteam.com/item.asp?ItemID=8595
HTH|||Establish cascading deletes between your tables, or use a trigger.|||Thanks for the replies guys, but apparently, I just have to learn patience. I am getting SQL Server 2000 at the end of the month and this supports Cascading DRI :).

Mike B|||Gotta be the same Mike

http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=33023

What about performance though..being "old school", I do it the old fashion way...|||What version are you using now? Hasn't cascading been a feature since at least 7.0, if not 6.5?|||Yeah...but you gotta remeber there's more than one platform...

DB2 OS/390, Oracle, SQL Server...oye

changing gears can get REAL interesting sometimes...

I guess I'm just from the school that here is no such thing as a key change...it's a new element...

and cascading deletes? I would imagine, is like giving a gun to a 2 year old for most developers...

no?

"OH SH-T, It's a MIRACLE! ALL THE DATA IS GONE!"

:eek:

Hey...triggers is hard enough for them to understand...

"Why are there 2 Rows affected?"|||Yeah, but my policy is not to design databases or write code for the lowest common denominator. If a business is too cheap to hire a competent DBA or Developer who can understand what I've built, then they deserve to have problems.|||Originally posted by blindman
What version are you using now? Hasn't cascading been a feature since at least 7.0, if not 6.5?
Update and Insert yes, not deleting

Mike B|||Originally posted by blindman
What version are you using now? Hasn't cascading been a feature since at least 7.0, if not 6.5?
6.5 never had it, neither did 7.0|||Fully agree,

As a developer I would never touch an application until I understood the data model and business rules defining it.

I find that triggered deletes too often get overlooked by developers, more so than the expectation that deletes WILL cascade throughout the data model.

Nothing worse than orphaned entries when trying to troubleshoot data errors.
Originally posted by MikeB_2k4
Update and Insert yes, not deleting

Mike B|||Originally posted by Brett Kaiser
Yeah...but you gotta remeber there's more than one platform...

DB2 OS/390, Oracle, SQL Server...oye

changing gears can get REAL interesting sometimes...

I guess I'm just from the school that here is no such thing as a key change...it's a new element...

and cascading deletes? I would imagine, is like giving a gun to a 2 year old for most developers...

no?

"OH SH-T, It's a MIRACLE! ALL THE DATA IS GONE!"

:eek:

Hey...triggers is hard enough for them to understand...

"Why are there 2 Rows affected?"

Extremely valid points!! (and yes it is the same Mike :) ) I am not sure if I even want to provide the deletion capability, but that doesn't mean I don't have to know how to do it.

Mike|||Originally posted by blindman
Yeah, but my policy is not to design databases or write code for the lowest common denominator. If a business is too cheap to hire a competent DBA or Developer who can understand what I've built, then they deserve to have problems.

What's the largest group you ever worked with?

And you know...it's always the database that's at fault...

Until you waste the time and prove them wrong (again)|||Yeah, and when you do, - they'll gang up on you and wait till it's really the database, and then you'll never hear the end of it... Hate them, hate them all!!!|||We have about 3K of them here, and only 5 of us...well, 2, if you count real bodies ;)|||Originally posted by rdjabarov
We have about 3K of them here, and only 5 of us...well, 2, if you count real bodies ;)

3,000? On a project level?

Or are you glass house support?|||Honestly, I don't know what 4/5 of them are here for, a mistery to me. But when on-call, I still have to be polite with them...at 3AM...when they trashed something, or when they can't connect and ask me to run a Profiler for them (that's the extent of their troubleshooting skills)|||I'll always remember getting an emergency call from the help desk once because a critical user couldn't get into the database. So I check the DB and everything is fine, and then I hustle down to her cube and find out that she has not electrical power to her station.

...but the case was a database issue because she couldn't get into her database... :rolleyes:|||You should embelish taht and send it in to the shark tank...|||Shark tank ?

-PatP|||oooh yeah...

You got to sign up and get the daily shark tank mailed to you...

http://www.computerworld.com/departments/opinions/sharktank?from=left|||I am firmly convinced that some of our vendors give their help-desk techs this as their script:

1) Are you sure the power cord is plugged in?
2) Have you applied the latest service pack?
3) Can you send us a copy of your database?

Hmm. Let's see. I have an application that works from one client, but does not work from another. Yeah, I can see as that is a database problem. Wait here, while I go get my troubleshooting baseball bat. ;-)|||That's not just any troubleshooting baseball bat...

It's a troubleshooting saux baseball bat!

And then there's April 16th in beantown! 8:05 prime time, national tv!

Maybe a ride up to visit the sister in law...

At least get a seat at the cask n' flaggon..

Still there?|||It's still there.

http://boston.citysearch.com/profile/4771681/

Who the heck runs this place? A Yankees fan?

Excerpt:

...and when the memories get too depressing, you can get a drink at the huge wraparound bar ....

Probably the most profitable bar in Boston ;-).|||I'm sitting here cracking up...and that doesn't look the flaggon I remeber...it was a dump...but then again it was late and I was hammered...

Is it still next to fenway?

EDIT: Or I could just click on the map...yup still there...

Another must visit place when I'm up is the beer works..well actually all the breweries...need new t shirts...

Thursday, March 22, 2012

deleting dependent rows in SQL 2000

I have a contacts table that has around 30 tables related to it (which
have other tables related to it - that often refer back to the same
contacts table). I am trying to delete a single contact and all rows
in any table related to it. I wrote a recursive query to scan the
database and remove the corresponding rows before deleting the contact
row.
I'm not sure this is the best solution. It takes a great deal of time
to run and seems to have problems with certain sections of the
database. Another idea I had was to modify each of the foreign keys
to turn on Cascade Delete whenever possibe, delete the row in the
contacts table then turn Cascade Delete off.
I would simply write the query out by hand but, because this is one of
the core tables, any new tables are usually linked to it and usually
kill the script (forcing yet another rewrite). Anyone have any
suggestions on what the best method would be to do this? Thanks for
your help.
AlexAlex,
Yes, you've got to tear them down in order of hierarchy.
DELETE GREATGRANDCHILDREN
DELETE GRANDCHILDREN
DELETE CHILDREN
DELETE PARENT
No shortcuts.
The good new is, once you get it written, you shouldn't have to write it
again. ;-)
James Hokes
"Alex" <under_payd@.yahoo.com> wrote in message
news:64b52496.0402231433.10fdcf5a@.posting.google.com...
> I have a contacts table that has around 30 tables related to it (which
> have other tables related to it - that often refer back to the same
> contacts table). I am trying to delete a single contact and all rows
> in any table related to it. I wrote a recursive query to scan the
> database and remove the corresponding rows before deleting the contact
> row.
> I'm not sure this is the best solution. It takes a great deal of time
> to run and seems to have problems with certain sections of the
> database. Another idea I had was to modify each of the foreign keys
> to turn on Cascade Delete whenever possibe, delete the row in the
> contacts table then turn Cascade Delete off.
> I would simply write the query out by hand but, because this is one of
> the core tables, any new tables are usually linked to it and usually
> kill the script (forcing yet another rewrite). Anyone have any
> suggestions on what the best method would be to do this? Thanks for
> your help.
> Alex

deleting dependent rows in SQL 2000

I have a contacts table that has around 30 tables related to it (which
have other tables related to it - that often refer back to the same
contacts table). I am trying to delete a single contact and all rows
in any table related to it. I wrote a recursive query to scan the
database and remove the corresponding rows before deleting the contact
row.
I'm not sure this is the best solution. It takes a great deal of time
to run and seems to have problems with certain sections of the
database. Another idea I had was to modify each of the foreign keys
to turn on Cascade Delete whenever possibe, delete the row in the
contacts table then turn Cascade Delete off.
I would simply write the query out by hand but, because this is one of
the core tables, any new tables are usually linked to it and usually
kill the script (forcing yet another rewrite). Anyone have any
suggestions on what the best method would be to do this? Thanks for
your help.
AlexAlex,
Yes, you've got to tear them down in order of hierarchy.
DELETE GREATGRANDCHILDREN
DELETE GRANDCHILDREN
DELETE CHILDREN
DELETE PARENT
No shortcuts.
The good new is, once you get it written, you shouldn't have to write it
again. ;-)
James Hokes
"Alex" <under_payd@.yahoo.com> wrote in message
news:64b52496.0402231433.10fdcf5a@.posting.google.com...
> I have a contacts table that has around 30 tables related to it (which
> have other tables related to it - that often refer back to the same
> contacts table). I am trying to delete a single contact and all rows
> in any table related to it. I wrote a recursive query to scan the
> database and remove the corresponding rows before deleting the contact
> row.
> I'm not sure this is the best solution. It takes a great deal of time
> to run and seems to have problems with certain sections of the
> database. Another idea I had was to modify each of the foreign keys
> to turn on Cascade Delete whenever possibe, delete the row in the
> contacts table then turn Cascade Delete off.
> I would simply write the query out by hand but, because this is one of
> the core tables, any new tables are usually linked to it and usually
> kill the script (forcing yet another rewrite). Anyone have any
> suggestions on what the best method would be to do this? Thanks for
> your help.
> Alex