Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 如何从多个表中删除productid_Mysql_Asp.net - Fatal编程技术网

Mysql 如何从多个表中删除productid

Mysql 如何从多个表中删除productid,mysql,asp.net,Mysql,Asp.net,我想在单击按钮时删除一个产品id,我只能删除1个表如何处理我的查询 protected void btnDelete_Click(object sender, EventArgs e) { string connection = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; SqlConnection connection1

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

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();
            }
        }
签入MySql

或者编写更多查询以首先删除链接到产品表的记录

例如


或者,您可以在一个查询中执行此操作,而无需级联

为简洁起见,请考虑隐藏以下链接表

 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        |
 +---------------+------------+

在主表中设置层叠删除在主表中为每个表设置多个delete语句层叠删除在主表中如何设置?使用google或读取answerTag中的链接表示这是mysql。你的意思是创建4行?字符串SQL语句?你可以编写4个不同的查询或单个存储过程。如您所愿,这取决于您的应用程序,取决于您要执行的检查等etcstring sqlStatement=DELETE FROM Product、ProductStock、ProductSideImage、ProductImage,其中ProductID=@pid;如何联系关系?
 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        |
 +---------------+------------+