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

SQL Server:帶有級聯刪除外鍵的表

SQL Server : table with foreign key to itself with cascade delete(SQL Server:帶有級聯刪除外鍵的表)
本文介紹了SQL Server:帶有級聯刪除外鍵的表的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我需要一個表格來保存這樣的層次樹數據(即大陸、國家、城市)

I need a table table to hold hierarchy tree data (i.e. continents, countries, cities) like this

id  name    parent
-------------------
1   world   null
2   Europe  1
3   Asia    1
4   France  2
5   Paris   4
6   Lyon    4

我想刪除 France 并希望該表級聯刪除所有法國城市.但是當我像這樣創建表時

I want to delete France and would expect the table to cascade delete all French cities. But when I create the table like this

create table locations 
(
    id int identity(1, 1),
    name varchar(255) not null,
    parent_id int,

    constraint pk__locations
        primary key clustered (id),

    constraint fk__locations
        foreign key (parent_id) 
            references locations (id)
                on delete cascade
                on update no action 
)

我有一個錯誤

在表上引入 FOREIGN KEY 約束 'fk__locations'位置"可能會導致循環或多個級聯路徑.指定開啟DELETE NO ACTION 或 ON UPDATE NO ACTION,或修改其他 FOREIGN KEY約束.

Introducing FOREIGN KEY constraint 'fk__locations' on table 'locations' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.

信息說

  1. 指定ON DELETE NO ACTION - 這正是我不想要的
  2. 指定ON UPDATE NO ACTION - 指定
  3. 修改其他 FOREIGN KEY 約束 - 我不明白這個
  1. to specify ON DELETE NO ACTION - that is exactly what do not I want
  2. to specify ON UPDATE NO ACTION - specified
  3. modify other FOREIGN KEY constraint - I don't understand this one

有人可以幫忙嗎?

推薦答案

這是不可能的.您可以使用 INSTEAD OF TRIGGER

This is not possible. You can solve this with an INSTEAD OF TRIGGER

create table locations 
(
    id int identity(1, 1),
    name varchar(255) not null,
    parent_id int,

    constraint pk__locations
        primary key clustered (id)

)
GO

INSERT INTO locations(name,parent_id)  VALUES
 ('world',null)
,('Europe',1)
,('Asia',1)
,('France',2)
,('Paris',4)
,('Lyon',4);
GO

--此觸發器將使用遞歸 CTE 來獲取您要刪除的所有 ID 之后的所有 ID.這些 ID 被刪除.

--This trigger will use a recursive CTE to get all IDs following all ids you are deleting. These IDs are deleted.

CREATE TRIGGER dbo.DeleteCascadeLocations ON locations
INSTEAD OF DELETE 
AS
BEGIN
    WITH recCTE AS
    (
        SELECT id,parent_id
        FROM deleted

        UNION ALL

        SELECT nxt.id,nxt.parent_id
        FROM recCTE AS prv
        INNER JOIN locations AS nxt ON nxt.parent_id=prv.id
    )
    DELETE FROM locations WHERE id IN(SELECT id FROM recCTE);
END
GO

--在這里測試,嘗試使用不同的 ID.你也可以試試 WHERE id IN(4,3)...

--Test it here, try with different IDs. You can try WHERE id IN(4,3) also...

SELECT * FROM locations;

DELETE FROM locations WHERE id=4;

SELECT * FROM locations
GO

--清理(仔細處理真實數據!)

--Clean-Up (Carefull with real data!)

if exists(select 1 from INFORMATION_SCHEMA.TABLES where TABLE_NAME='locations')
---DROP TABLE locations;

這篇關于SQL Server:帶有級聯刪除外鍵的表的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

Converting Every Child Tags in to a Single Column with multiple Delimiters -SQL Server (3)(將每個子標記轉換為具有多個分隔符的單列-SQL Server (3))
How can I create a view from more than one table?(如何從多個表創建視圖?)
Create calculated value based on calculated value inside previous row(根據前一行內的計算值創建計算值)
How do I stack the first two columns of a table into a single column, but also pair third column with the first column only?(如何將表格的前兩列堆疊成一列,但也僅將第三列與第一列配對?) - IT屋-程序員軟件開發技
Recursive t-sql query(遞歸 t-sql 查詢)
Convert Month Name to Date / Month Number (Combinations of Questions amp; Answers)(將月份名稱轉換為日期/月份編號(問題和答案的組合))
主站蜘蛛池模板: 天堂久久一区 | 久草.com | 日日射夜夜骑 | 日本中文字幕在线视频 | 中文在线一区二区 | 在线一区 | 国产91久久精品一区二区 | 欧美在线网站 | 国产精品一区二区久久 | 91精品国产综合久久久久久漫画 | 天天操操| 国产精品看片 | 精品欧美一区二区精品久久久 | 国产一区二区在线免费播放 | cao在线 | 在线日韩欧美 | 九九热免费视频在线观看 | 久久久久成人精品免费播放动漫 | 高清人人天天夜夜曰狠狠狠狠 | 91在线精品秘密一区二区 | 天天碰日日操 | 99pao成人国产永久免费视频 | 国产免费一区 | 国产激情视频 | 日本视频一区二区 | 免费黄色大片 | 五月婷婷激情网 | 亚洲精品一区二区 | 91国自视频 | 999久久久久久久久6666 | 精品久久久久久久久久久久 | 精品国产欧美一区二区三区成人 | 国产中文字幕在线 | 精品久久久久久久久久久久久久 | 中文无吗 | 精品欧美乱码久久久久久 | 精品欧美乱码久久久久久 | 国产黄色av电影 | 91一区二区三区 | 国产精品 欧美精品 | 国产成人综合在线 |