我有一个关于这个而不是更新触发器的问题:
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
数据库不能更改基表的条目?!
这是我的代码:
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;
我得到了两个表的自动增量的序列和触发器。
谢谢你的帮助!