简体   繁体   中英

Delete Multiple rows in several tables

I have 3 tables in an SQL database:

  1. Animal Status
  2. Animals
  3. Cart

I want to be able to delete several items from the Animals and Animal Status tables based on the cart.

What's the same between all the tables is the AnimalID which is the primary key in all the tables and based on that I'm supposed to delete them. But at the same time I need to delete it for someone with a specific email.

Example is if I have someone with the email 123@gmail.com and this person has 2 animals in his cart 1 with the ID 123 and the other with the id 456 I need to delete these 2 specific animals from the other 2 tables.

I know how to do it for 1 table but not when I have several tables that depend on each other along with them depending on the specific email when there are also other emails and animals in the cart.

Here is how it works on the outside:

x= AnimalStatus.Delete(AdoptCart.GetAnimalId(Session["email"].ToString()).ToString());
            if (x > 0)
            {
                x = Animal.Delete(AdoptCart.GetAnimalId(Session["email"].ToString()).ToString());
                if (x > 0)
                {
                    AdoptCart.RemoveAllbyEmail(Session["email"].ToString());

Here are all the deletion codes in the order they work:

Removes the AnimalStatus based on the animals ID:

static public int Delete(string id)
    {
        int rowsAffected;
        string strSql = string.Format("delete from AnimalStatus where AnimalID='" + id + "'");
        rowsAffected = (int)dataservice.ExecuteNonQuery(strSql);
        return rowsAffected;
    }

Removes an animal from the animal table based on the animal's ID:

 static public int Delete(string id)
        {
            int rowsAffected;
            string strSql = string.Format("delete from Animal where AnimalID='" + id + "'");
            rowsAffected = (int)dataservice.ExecuteNonQuery(strSql);
            return rowsAffected;
        }

Removes all animals from the Cart Table:

static public int Remove(string AnimalID) 
    {
        int rowsAffected;
        string strSql = string.Format("delete from Cart where AnimalID='" + AnimalID + "'");
        rowsAffected = (int)dataservice.ExecuteNonQuery(strSql);
        return rowsAffected;
    }

Issue with this code is that all of that only deletes 1 animal and I need several removed.

I have tried creating a statement like this, but I didn't manage to make it work.:

Delete Animal.*, AnimalStatus.*

FROM     Animal INNER JOIN
                  AnimalStatus ON Animal.AnimalID = AnimalStatus.AnimalID INNER JOIN
                  Cart ON Animal.AnimalID = Cart.AnimalID
WHERE  (Cart.UserEmail = N'Email')

Based on the example I gave in words the ones that are supposed to be deleted are the following marked in //removed .

Cart Table
 | AnimalID | Email_Addr |
--------------------------------------
| 123 | 123@gmail.com | //removed
| 456 | 123@gmail.com | //removed
| 765 | jj@gmail.com  |
| 343 | bb@gmail.com  |
| 256 | cc@gmail.com  |

Animal Status Table
 | AnimalID | Vaccinated |
--------------------------------------
| 123 | Yes | //removed
| 456 | Yes | //removed
| 765 | No  |
| 343 | No  |
| 256 | No  |

Animals Tables
 | AnimalID | Age |
--------------------------------------
| 123 | 3 | //removed
| 456 | 4 | //removed
| 765 | 3 |
| 343 | 7 |
| 256 | 10|

Looking at your current work there are a few things you could do. You could rewrite your query logic like you're asking, but what I'm not seeing in your example is primary/foreign key relationships. So cascade won't work with how you currently have your tables set up. This is a good write up of cascade delete. Cascade Delete

So if you don't want to rewrite everything you really could just do all of this in your C# code. All you'd have to do is loop through the cart table and get all of the id's that have the e-mail "123@gmail.com" and call your three delete statements. You'd just need to write a method to grab the id's and call the delete methods until the id variable is empty.

It just depends where you want all of the logic to exist, in sql or in your C# code. If you're using Entity Framework, whether code first or database first, you have options to use LINQ Query syntax or extension methods. Just depends on preference really.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM