久久久久久久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)(將月份名稱轉換為日期/月份編號(問題和答案的組合))
主站蜘蛛池模板: 国产精品96 | 国产超碰在线 | 天天操天天碰 | 黄色三级网站 | 高清一级片 | 国产午夜视频在线观看 | 色婷av | 看片黄全部免费 | 欧美专区在线 | 91成人精品一区在线播放 | 国产午夜视频在线观看 | 久久久久久久99 | 观看av| 久久爱综合 | 免费看黄色大片 | av一区在线 | 亚洲高清视频在线观看 | 日本人做爰全过程 | 欧美视频久久 | 丰满少妇高潮在线观看 | 91在线播放视频 | 91网站免费 | 国产成人精品一区 | 黄色福利 | 日本不卡免费 | 69人人| 成人午夜视频在线观看 | 一级黄色大片 | 日韩免费一级片 | 久久久在线 | 精品国产一区二 | 五月天在线观看 | 亚洲视频三区 | 日韩黄色一级视频 | 欧美色综合天天久久综合精品 | 国产性生活 | 在线观看福利影院 | 亚洲在线观看视频 | 欧美日韩一区二区三区 | 成人三级在线观看 | 中文在线字幕观看 |