如何从多个表中删除productid

时间:2013-12-26 17:15:53

标签: mysql asp.net

我想在点击按钮时删除产品ID,我只能删除1个表如何处理我的查询

enter image description here

protected void btnDelete_Click(object sender, EventArgs e)
        {
            string connection = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
            SqlConnection connection1 = new SqlConnection(connection);
            string sqlStatement = "DELETE FROM Product WHERE ProductID = @pid";

            try
            {
                connection1.Open();
                SqlCommand cmd = new SqlCommand(sqlStatement, connection1);
                cmd.Parameters.AddWithValue("@pid", Id);
                cmd.CommandType = CommandType.Text;
                cmd.ExecuteNonQuery();

            }
            finally
            {
                connection1.Close();
            }
        }

2 个答案:

答案 0 :(得分:0)

检查MySql中的ON DELETE CASCADE

或编写更多查询以首先删除链接到Product表的记录。

例如

DELETE from ProductSideImage WHERE ProductID = 1;
DELETE from ProductImage WHERE ProductID = 1;
DELETE from ProductStock WHERE ProductID = 1;
DELETE from Product WHERE ProductID = 1;

string sqlStatement = "DELETE FROM Product WHERE ProductID = @pid; DELETE FROM ProductStock WHERE ProductID = @pid; DELETE FROM ProductSideImage WHERE ProductID = @pid; DELETE FROM ProductImage WHERE ProductID = @pid;";

答案 1 :(得分:0)

或者,您可以在没有CASCADE的情况下进行一次查询。

请考虑以下内容(为简洁起见隐藏链接表)......

 SELECT * FROM recipes;
 +-----------+-------------------------+
 | recipe_id | recipe                  |
 +-----------+-------------------------+
 |         1 | Macaroni & Cheese       |
 |         2 | Cheese on Toast         |
 |         3 | Beans on Toast          |
 |         4 | Cheese & Beans on Toast |
 |         5 | Toast & Jam             |
 |         6 | Humus                   |
 +-----------+-------------------------+

 SELECT * FROM ingredients;
 +---------------+------------+
 | ingredient_id | ingredient |
 +---------------+------------+
 |             1 | Macaroni   |
 |             2 | Cheese     |
 |             3 | Beans      |
 |             4 | Toast      |
 |             5 | Jam        |
 |             6 | Chickpeas  |
 |             7 | Tahini     |
 +---------------+------------+

 SELECT r.*
      , i.* 
   FROM recipes r 
   JOIN recipe_ingredient ri 
     ON ri.recipe_id = r.recipe_id 
   JOIN ingredients i 
     ON i.ingredient_id = ri.ingredient_id;
 +-----------+-------------------------+---------------+------------+
 | recipe_id | recipe                  | ingredient_id | ingredient |
 +-----------+-------------------------+---------------+------------+
 |         1 | Macaroni & Cheese       |             1 | Macaroni   |
 |         1 | Macaroni & Cheese       |             2 | Cheese     |
 |         2 | Cheese on Toast         |             2 | Cheese     |
 |         2 | Cheese on Toast         |             4 | Toast      |
 |         3 | Beans on Toast          |             3 | Beans      |
 |         3 | Beans on Toast          |             4 | Toast      |
 |         4 | Cheese & Beans on Toast |             2 | Cheese     |
 |         4 | Cheese & Beans on Toast |             3 | Beans      |
 |         4 | Cheese & Beans on Toast |             4 | Toast      |
 |         5 | Toast & Jam             |             4 | Toast      |
 |         5 | Toast & Jam             |             5 | Jam        |
 |         6 | Humus                   |             6 | Chickpeas  |
 |         6 | Humus                   |             7 | Tahini     |
 +-----------+-------------------------+---------------+------------+

 DELETE r
      , ri
      , i 
   FROM recipes r 
   JOIN recipe_ingredient ri 
     ON ri.recipe_id = r.recipe_id 
   JOIN ingredients i 
     ON i.ingredient_id = ri.ingredient_id 
  WHERE r.recipe = 'Humus';

 SELECT * FROM recipes;
 +-----------+-------------------------+
 | recipe_id | recipe                  |
 +-----------+-------------------------+
 |         1 | Macaroni & Cheese       |
 |         2 | Cheese on Toast         |
 |         3 | Beans on Toast          |
 |         4 | Cheese & Beans on Toast |
 |         5 | Toast & Jam             |
 +-----------+-------------------------+

 SELECT * FROM ingredients;
 +---------------+------------+
 | ingredient_id | ingredient |
 +---------------+------------+
 |             1 | Macaroni   |
 |             2 | Cheese     |
 |             3 | Beans      |
 |             4 | Toast      |
 |             5 | Jam        |
 +---------------+------------+