我有一个问题,而不是更新触发器:
CREATE OR REPLACE TRIGGER view_player_update
INSTEAD OF INSERT ON view_player
FOR EACH ROW
DECLARE
anzTeams NUMBER;
teamIdentifier NUMBER;
BEGIN
SELECT count(*) INTO anzTeams FROM team WHERE team=:NEW.team;
IF anzTeams = 0
THEN
INSERT INTO team(team) VALUES (:NEW.team);
SELECT teamid INTO teamIdentifier FROM team WHERE team=:NEW.team;
ELSE
SELECT teamid INTO teamIdentifier FROM team WHERE team=:NEW.team;
END IF;
UPDATE player SET player=:NEW.player, position=:NEW.position,teamid=teamIdentifier WHERE playerid=:OLD.playerid;
END;
一个案例是错误的。通过更新团队
UPDATE view_player SET team='Real Madrid' WHERE playerid=1
我得到了这个例外:
ORA-01779:Kann keine Spalte,die einer Basistabelle zugeordnet wird,verändern
数据库无法更改basistable的条目?!
这是我的代码:
CREATE TABLE team(
teamid integer PRIMARY KEY,
team VARCHAR2(100)
);
CREATE TABLE player(
playerid integer PRIMARY KEY,
player VARCHAR2(100),
position VARCHAR2(100),
teamid integer REFERENCES team(teamid)
);
INSERT ALL
INTO team (teamid, team) VALUES (1, 'FC Bayern Muenchen')
INTO team (teamid, team) VALUES (2, 'Manchester United')
SELECT * FROM DUAL;
INSERT ALL
INTO player (playerid, player, position, teamid) VALUES (1, 'Manuel Neuer', 'goalkeeper', 1)
INTO player (playerid, player, position, teamid) VALUES (2, 'Dante', 'defense', 1)
INTO player (playerid, player, position, teamid) VALUES (3, 'Cesc Fabregas', 'midfield', 2)
INTO player (playerid, player, position, teamid) VALUES (4, 'Lionel Messi', 'striker', 2)
INTO player (playerid, player, position, teamid) VALUES (5, 'Arjen Robben', 'midfield', 1)
SELECT * FROM DUAL;
CREATE VIEW view_player AS
SELECT p.playerid,p.player,p.position, t.team FROM player p
INNER JOIN team t
ON p.teamid = t.teamid;
我得到了两个表的自动增量的序列和触发器。
谢谢你的帮助!
答案 0 :(得分:1)
想一想:当你运行update语句时,你打算做什么?它应该:
数据库无法知道你想要什么,即使意图明确,也不可能自己做任何一个。
如果你想做前者,那么你应该更新播放器表。
如果你想做后者,那么更新团队表。
我认为你在视图上使用替代触发器的这种方法是一种非常糟糕的做法,你应该坚持使用传统方法来维护数据。