我正在创建一个数据库来模拟游戏行业,它有两个看起来像这样的表:
PLAYABLE (Which games are playable on which consoles)
--------------------------------------------------
| Game_ID (int) | Console_Name (varchar(255)) | (both primary/foreign)
--------------------------------------------------
CONSOLE
--------------------------------------------------
| Console_Name (PK) | Exclusives (int) |
--------------------------------------------------
exclusives属性给出了控制台的游戏数量,只有控制台的游戏数量。
所以当我在“可播放的”中插入一个新的元组时关系,如果那是' Game_ID'已经存在过一次,那么我需要减少“排他性”。属性与相应的' Console_Name。'同样,如果它不存在,那么我会增加该控制台的独家数量。
我是Oracle新手,这是我尝试为此创建触发器:
CREATE OR REPLACE TRIGGER Exclusives_Count
BEFORE INSERT ON Playable
FOR EACH ROW
BEGIN
DECLARE
Game_Count NUMBER;
Obsolete_Exclusive_Console VARCHAR(255);
SELECT COUNT(Game_ID) INTO Game_Count FROM Playable WHERE Game_ID = :NEW.Game_ID;
IF Game_Count = 0
THEN
UPDATE Console
SET Exclusives = Exclusives + 1
WHERE Console.Console_Name = :NEW.Console_Name;
ELSIF Game_Count = 1
THEN
SELECT Console_Name INTO Obsolete_Exclusive_Console FROM Playable WHERE Game_ID = :NEW.Game_ID;
UPDATE Console
SET Exclusives = Exclusives - 1
WHERE Console.Console_Name = Obsolete_Exclusive_Console;
END IF;
END;
/
输入此内容即表示“警告:使用编译错误创建触发器”#34;但我不确定自己哪里出错了。
答案 0 :(得分:2)
declare
应该在代码中begin
之前
见下面的声明:
create table Playable(Game_ID int, Console_Name varchar(255));
create table CONSOLE(Console_Name varchar(255), Exclusives int);
SQL>
SQL> CREATE OR REPLACE TRIGGER Exclusives_Count
2 BEFORE INSERT ON Playable
3 FOR EACH ROW
4
5 DECLARE
6 Game_Count NUMBER;
7 Obsolete_Exclusive_Console VARCHAR(255);
8 BEGIN
9
10 SELECT COUNT(Game_ID) INTO Game_Count FROM Playable WHERE Game_ID = :NEW.Game_ID;
11 IF Game_Count = 0
12 THEN
13 UPDATE Console
14 SET Exclusives = Exclusives + 1
15 WHERE Console.Console_Name = :NEW.Console_Name;
16 ELSIF Game_Count = 1
17 THEN
18 SELECT Console_Name INTO Obsolete_Exclusive_Console FROM Playable WHERE Game_ID = :NEW.Game_ID;
19
20 UPDATE Console
21 SET Exclusives = Exclusives - 1
22 WHERE Console.Console_Name = Obsolete_Exclusive_Console;
23 END IF;
24 END;
25 /
Trigger created
SQL> show errors
No errors for TRIGGER SCM.EXCLUSIVES_COUNT
SQL>