2014-04-19 111 views
1

我有一個用戶表,其中包括一個引用他們在遊戲中的位置的列。刪除set ... not null?

我有一個位置表。

我有一個外鍵綁在一起。

我想這樣做,以便當某個位置被刪除時,用戶被髮送到最近的位置 - 我可以通過簡單的查找輕鬆確定要將用戶發送到哪個位置,但是我需要知道的是我可以在哪裏放置這個觸發器?

如果我將外鍵設置爲「ON DELETE SET NULL」,那麼我可以使用觸發器來檢測設置爲NULL的位置並使用old.location來確定將它們發送到哪裏?那是一件事嗎?

回答

1

你需要一個BEFORE DELETE觸發您locations表,類似如下:

create trigger move_users 
before delete 
on locations 
for each row 
    update users 
    set locationid = (select nearest from nearest where locationid = old.locationid) 
    where locationid = old.locationid 

這裏有一個sqlfiddle一些示例數據。