2012-12-06 314 views
1

我已將此代碼發佈到另一個威脅中,但此問題稍有不同。我必須做出觸發。這個問題聽起來是這樣的:刪除MYSQL中的觸發器

"Create a trigger that removes the season if unused whenever information about when and who produces a product is deleted (i.e., whenever a record is removed from the 'Produces' table, check if there are other records about product being produced for the same season, if not, remove also the corresponding record from the season table)"

我的MySQL代碼:

的MySQL的代碼是在這裏:

CREATE TABLE IF NOT EXISTS Dvds(
Serial integer NOT NULL, 
Name varchar(50), 
Year integer, 
Genre varchar(50), 
Price integer, 
PRIMARY KEY (Serial)); 

CREATE TABLE IF NOT EXISTS Shops(
Id integer NOT NULL, 
Name varchar(50), 
Address varchar(50), 
PRIMARY KEY (Id)); 

CREATE TABLE IF NOT EXISTS Customers(
CNo integer NOT NULL, 
AccNo integer, 
Time varchar(50), 
PRIMARY KEY (CNo)); 

CREATE TABLE IF NOT EXISTS ContactPersons(
Id integer NOT NULL, 
Name varchar(50), 
Phone integer, 
PRIMARY KEY (Id)); 

CREATE TABLE IF NOT EXISTS Seasons(
StartDate date NOT NULL, 
EndDate date NOT NULL, 
PRIMARY KEY (StartDate,EndDate)); 

CREATE TABLE IF NOT EXISTS WebShops(
Id integer NOT NULL, 
Url varchar(50), 
FOREIGN KEY (Id) REFERENCES Shops (Id), 
PRIMARY KEY (Id)); 

CREATE TABLE IF NOT EXISTS Producer(
Id integer NOT NULL, 
Address varchar(50), 
Name varchar(50), 
PRIMARY KEY (Id)); 

CREATE TABLE IF NOT EXISTS Sold(
Id integer NOT NULL, 
CNo integer NOT NULL, 
Serial integer NOT NULL, 
FOREIGN KEY (Id) REFERENCES Shops (Id), 
FOREIGN KEY (CNo) REFERENCES Customers (CNo), 
FOREIGN KEY (Serial) REFERENCES Dvds (Serial), 
PRIMARY KEY (Id,CNo,Serial)); 

CREATE TABLE IF NOT EXISTS Has(
Id integer NOT NULL, 
Serial integer NOT NULL, 
FOREIGN KEY (Id) REFERENCES Shops (Id), 
FOREIGN KEY (Serial) REFERENCES Dvds (Serial), 
PRIMARY KEY (Id,Serial)); 

CREATE TABLE IF NOT EXISTS Has2(
Serial integer NOT NULL, 
Producer_Id integer NOT NULL, 
StartDate date NOT NULL, 
EndDate date NOT NULL, 
ContactPersons_Id integer NOT NULL, 
FOREIGN KEY (Serial) REFERENCES Dvds (Serial), 
FOREIGN KEY (Producer_Id) REFERENCES Producer (Id), 
FOREIGN KEY (StartDate) REFERENCES Seasons (StartDate), 
FOREIGN KEY (EndDate) REFERENCES Seasons (EndDate), 
FOREIGN KEY (ContactPersons_Id) REFERENCES ContactPersons (Id), 
PRIMARY KEY (Serial,Producer_Id,StartDate,EndDate,ContactPersons_Id)); 

觸發:

CREATE TRIGGER `Seasons_before_delete` 
    AFTER DELETE ON `Seasons` 
    FOR EACH ROW 
    BEGIN 
    DELETE FROM seasonstart 
    WHERE seasonstart.??????? 
    DELETE FROM seasonend 
    WHERE seasonend.?????? 
    END 

,但我真的不知道。我的老師告訴我,我可以使用IF,ELSEIF,GOTOEND?但我在正確的軌道上?我現在非常空白,現在該做什麼,所以希望有人有一個建議,我可以做些什麼來解決這個問題?

+0

要格式化代碼,突出顯示它,然後點擊ctrl-k,或單擊編輯器中的「{}」按鈕。對於小的內聯代碼位,在想要「編碼」的文本週圍使用反引號(')。 –

+0

@Johan - 請不要使用作業標籤。已經過時了。 – Mike

+0

我知道,但這裏100%準確。 – Johan

回答

2

在觸發器中有2個虛擬表。

NEWOLD

顯然在create觸發沒有old,並在delete觸發沒有new。 因此,如果您想使用發生更改的表格(觸發器附加的表格)的數據,請使用這兩個表格。

因爲這是一個delete觸發器,所以您需要使用old表。

所以你的觸發器將是這個樣子:

CREATE TRIGGER `Seasons_before_delete` AFTER DELETE ON `Seasons` 
FOR EACH ROW 
BEGIN 
    DELETE FROM seasonstart 
    WHERE seasonstart.seasonID = old.ID 

    DELETE FROM seasonend 
    WHERE seasonend.seasonID = old.ID 
END 

請注意,這是毫無意義的調用AFTER DELETE觸發「something_something_BEFORE」 :-)。

此外請注意,我不知道爲什麼您需要在此觸發器中使用IF,但刪除語句中的where已經處理此問題。

+0

非常感謝您的回答。我試着玩弄它。從來就輸入這個在: CREATE TRIGGER'Seasons_before_delete'後刪除'Seasons' FOR EACH ROW BEGIN DELETE FROM seasonstart WHERE seasonstart.seasonID = old.ID DELETE FROM seasonend WHERE seasonend.seasonID =老.ID END 和我得到這個錯誤: http://postimage.org/image/5sr35zrfv/ 很抱歉,但我在mysql的非常新。嗯,我找不到{}按鈕?希望你能再次幫助我,儘管代碼搞砸了嗎? : -/ – McDuck4

+0

嗯最後一件事..我如何測試,看看我的觸發器是否工作: - /? 感謝您的幫助,直到現在。 – McDuck4

+0

myphpadmin中的My Seasons數據庫看起來像這樣: http:// postimage。組織/圖像/ 5n4yczg57/ – McDuck4