久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

<tfoot id='KpmeM'></tfoot>

    • <bdo id='KpmeM'></bdo><ul id='KpmeM'></ul>

      <i id='KpmeM'><tr id='KpmeM'><dt id='KpmeM'><q id='KpmeM'><span id='KpmeM'><b id='KpmeM'><form id='KpmeM'><ins id='KpmeM'></ins><ul id='KpmeM'></ul><sub id='KpmeM'></sub></form><legend id='KpmeM'></legend><bdo id='KpmeM'><pre id='KpmeM'><center id='KpmeM'></center></pre></bdo></b><th id='KpmeM'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='KpmeM'><tfoot id='KpmeM'></tfoot><dl id='KpmeM'><fieldset id='KpmeM'></fieldset></dl></div>
    1. <legend id='KpmeM'><style id='KpmeM'><dir id='KpmeM'><q id='KpmeM'></q></dir></style></legend>

      <small id='KpmeM'></small><noframes id='KpmeM'>

        MySQL外鍵約束,級聯(lián)刪除

        MySQL foreign key constraints, cascade delete(MySQL外鍵約束,級聯(lián)刪除)
        <i id='1GtAM'><tr id='1GtAM'><dt id='1GtAM'><q id='1GtAM'><span id='1GtAM'><b id='1GtAM'><form id='1GtAM'><ins id='1GtAM'></ins><ul id='1GtAM'></ul><sub id='1GtAM'></sub></form><legend id='1GtAM'></legend><bdo id='1GtAM'><pre id='1GtAM'><center id='1GtAM'></center></pre></bdo></b><th id='1GtAM'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='1GtAM'><tfoot id='1GtAM'></tfoot><dl id='1GtAM'><fieldset id='1GtAM'></fieldset></dl></div>
        1. <tfoot id='1GtAM'></tfoot>
          • <bdo id='1GtAM'></bdo><ul id='1GtAM'></ul>
              <legend id='1GtAM'><style id='1GtAM'><dir id='1GtAM'><q id='1GtAM'></q></dir></style></legend>

                    <tbody id='1GtAM'></tbody>

                  <small id='1GtAM'></small><noframes id='1GtAM'>

                  本文介紹了MySQL外鍵約束,級聯(lián)刪除的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  我想使用外鍵來保持完整性并避免孤兒(我已經(jīng)使用了 innoDB).

                  I want to use foreign keys to keep the integrity and avoid orphans (I already use innoDB).

                  我如何制作一個 DELETE ON CASCADE 的 SQL 語句?

                  How do I make a SQL statment that DELETE ON CASCADE?

                  如果我刪除一個類別,那么我如何確保它不會刪除與其他類別相關(guān)的產(chǎn)品.

                  If I delete a category then how do I make sure that it would not delete products that also are related to other categories.

                  數(shù)據(jù)透視表categories_products"在另外兩個表之間創(chuàng)建多對多關(guān)系.

                  The pivot table "categories_products" creates a many-to-many relationship between the two other tables.

                  categories
                  - id (INT)
                  - name (VARCHAR 255)
                  
                  products
                  - id
                  - name
                  - price
                  
                  categories_products
                  - categories_id
                  - products_id
                  

                  推薦答案

                  如果您的級聯(lián)刪除了一個產(chǎn)品,因為它是被殺死的類別的成員,那么您設(shè)置的外鍵不正確.鑒于您的示例表,您應(yīng)該具有以下表設(shè)置:

                  If your cascading deletes nuke a product because it was a member of a category that was killed, then you've set up your foreign keys improperly. Given your example tables, you should have the following table setup:

                  CREATE TABLE categories (
                      id int unsigned not null primary key,
                      name VARCHAR(255) default null
                  )Engine=InnoDB;
                  
                  CREATE TABLE products (
                      id int unsigned not null primary key,
                      name VARCHAR(255) default null
                  )Engine=InnoDB;
                  
                  CREATE TABLE categories_products (
                      category_id int unsigned not null,
                      product_id int unsigned not null,
                      PRIMARY KEY (category_id, product_id),
                      KEY pkey (product_id),
                      FOREIGN KEY (category_id) REFERENCES categories (id)
                         ON DELETE CASCADE
                         ON UPDATE CASCADE,
                      FOREIGN KEY (product_id) REFERENCES products (id)
                         ON DELETE CASCADE
                         ON UPDATE CASCADE
                  )Engine=InnoDB;
                  

                  這樣,您可以刪除一個產(chǎn)品或一個類別,并且只有categories_products 中的關(guān)聯(lián)記錄會一起消失.級聯(lián)不會在樹上進一步移動并刪除父產(chǎn)品/類別表.

                  This way, you can delete a product OR a category, and only the associated records in categories_products will die alongside. The cascade won't travel farther up the tree and delete the parent product/category table.

                  例如

                  products: boots, mittens, hats, coats
                  categories: red, green, blue, white, black
                  
                  prod/cats: red boots, green mittens, red coats, black hats
                  

                  如果刪除 'red' 類別,則只有類別表中的 'red' 條目以及 prod/cats 的兩個條目:'red boots' 和 'redcoats' 消失.

                  If you delete the 'red' category, then only the 'red' entry in the categories table dies, as well as the two entries prod/cats: 'red boots' and 'red coats'.

                  刪除不會進一步級聯(lián),也不會刪除靴子"和外套"類別.

                  The delete will not cascade any farther and will not take out the 'boots' and 'coats' categories.

                  評論跟進:

                  您仍然誤解了級聯(lián)刪除的工作原理.它們只影響定義了刪除級聯(lián)"的表.在這種情況下,級聯(lián)設(shè)置在categories_products"表中.如果刪除紅色"類別,則categories_products 中唯一會級聯(lián)刪除的記錄是那些category_id = red 的記錄.它不會觸及任何 'category_id = blue' 的記錄,也不會前進到products"表,因為該表中沒有定義外鍵.

                  you're still misunderstanding how cascaded deletes work. They only affect the tables in which the "on delete cascade" is defined. In this case, the cascade is set in the "categories_products" table. If you delete the 'red' category, the only records that will cascade delete in categories_products are those where category_id = red. It won't touch any records where 'category_id = blue', and it would not travel onwards to the "products" table, because there's no foreign key defined in that table.

                  這是一個更具體的例子:

                  Here's a more concrete example:

                  categories:     products:
                  +----+------+   +----+---------+
                  | id | name |   | id | name    |
                  +----+------+   +----+---------+
                  | 1  | red  |   | 1  | mittens |
                  | 2  | blue |   | 2  | boots   |
                  +---++------+   +----+---------+
                  
                  products_categories:
                  +------------+-------------+
                  | product_id | category_id |
                  +------------+-------------+
                  | 1          | 1           | // red mittens
                  | 1          | 2           | // blue mittens
                  | 2          | 1           | // red boots
                  | 2          | 2           | // blue boots
                  +------------+-------------+
                  

                  假設(shè)您刪除了類別 #2(藍色):

                  Let's say you delete category #2 (blue):

                  DELETE FROM categories WHERE (id = 2);
                  

                  DBMS 會查看所有有外鍵指向 'categories' 表的表,并刪除匹配 id 為 2 的記錄.因為我們只在 products_categories,一旦刪除完成,你就會得到這個表:

                  the DBMS will look at all the tables which have a foreign key pointing at the 'categories' table, and delete the records where the matching id is 2. Since we only defined the foreign key relationship in products_categories, you end up with this table once the delete completes:

                  +------------+-------------+
                  | product_id | category_id |
                  +------------+-------------+
                  | 1          | 1           | // red mittens
                  | 2          | 1           | // red boots
                  +------------+-------------+
                  

                  products 表中沒有定義外鍵,所以級聯(lián)在那里不起作用,所以你仍然列出了靴子和手套.只是不再有藍色靴子"和藍色手套"了.

                  There's no foreign key defined in the products table, so the cascade will not work there, so you've still got boots and mittens listed. There's just no 'blue boots' and no 'blue mittens' anymore.

                  這篇關(guān)于MySQL外鍵約束,級聯(lián)刪除的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

                  【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

                  相關(guān)文檔推薦

                  How to use windowing functions efficiently to decide next N number of rows based on N number of previous values(如何有效地使用窗口函數(shù)根據(jù) N 個先前值來決定接下來的 N 個行)
                  reuse the result of a select expression in the quot;GROUP BYquot; clause?(在“GROUP BY中重用選擇表達式的結(jié)果;條款?)
                  Does ignore option of Pyspark DataFrameWriter jdbc function ignore entire transaction or just offending rows?(Pyspark DataFrameWriter jdbc 函數(shù)的 ignore 選項是忽略整個事務(wù)還是只是有問題的行?) - IT屋-程序員軟件開發(fā)技
                  Error while using INSERT INTO table ON DUPLICATE KEY, using a for loop array(使用 INSERT INTO table ON DUPLICATE KEY 時出錯,使用 for 循環(huán)數(shù)組)
                  pyspark mysql jdbc load An error occurred while calling o23.load No suitable driver(pyspark mysql jdbc load 調(diào)用 o23.load 時發(fā)生錯誤 沒有合適的驅(qū)動程序)
                  How to integrate Apache Spark with MySQL for reading database tables as a spark dataframe?(如何將 Apache Spark 與 MySQL 集成以將數(shù)據(jù)庫表作為 Spark 數(shù)據(jù)幀讀取?)
                  • <bdo id='pD56i'></bdo><ul id='pD56i'></ul>
                    <legend id='pD56i'><style id='pD56i'><dir id='pD56i'><q id='pD56i'></q></dir></style></legend>

                      <small id='pD56i'></small><noframes id='pD56i'>

                      1. <tfoot id='pD56i'></tfoot>
                            <tbody id='pD56i'></tbody>
                        • <i id='pD56i'><tr id='pD56i'><dt id='pD56i'><q id='pD56i'><span id='pD56i'><b id='pD56i'><form id='pD56i'><ins id='pD56i'></ins><ul id='pD56i'></ul><sub id='pD56i'></sub></form><legend id='pD56i'></legend><bdo id='pD56i'><pre id='pD56i'><center id='pD56i'></center></pre></bdo></b><th id='pD56i'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='pD56i'><tfoot id='pD56i'></tfoot><dl id='pD56i'><fieldset id='pD56i'></fieldset></dl></div>

                          1. 主站蜘蛛池模板: 拍拍无遮挡人做人爱视频免费观看 | 少妇av片 | 国产欧美在线一区 | 天天影视亚洲综合网 | 三级黄色片在线观看 | 色婷婷综合久久久中字幕精品久久 | 最新av中文字幕 | 81精品国产乱码久久久久久 | 中文字幕一级 | 天堂网avav| www.色午夜.com | 日本三级电影免费观看 | 性做久久久久久免费观看欧美 | 农夫在线精品视频免费观看 | 成人av片在线观看 | 国内精品视频一区二区三区 | 国产一级一级毛片 | 国产成人精品高清久久 | 日韩精品成人 | 亚洲一区二区在线 | 国产一区二区在线免费观看 | 亚洲精品888 | 国产精品一区二 | 九九九视频 | 亚洲欧美日韩在线 | 日韩黄 | 亚洲一二三区av | 国产1区在线 | 欧美9999 | 国产欧美一区二区三区久久 | 欧美日韩视频在线 | 亚洲大片一区 | 天堂中文在线播放 | www国产成人免费观看视频,深夜成人网 | 亚洲精品乱码久久久久久久久久 | 在线日韩 | 国产激情在线 | 久色一区 | 正在播放一区二区 | 成人av在线播放 | 国产午夜精品理论片a大结局 |