0

我有一个关于这个而不是更新触发器的问题:

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;

我得到了两个表的自动增量的序列和触发器。

谢谢你的帮助!

4

1 回答 1

1

Think about this: when you run the update statement, what do you intend should happen? Should it:

  1. change the name of the team to which playerid 1 is joined to be 'Real Madrid', or
  2. change the teamid for playerid 1 to be that for Real Madrid?

The database cannot know what you want, and it is unlikely on its own to do either of those even if the intention was clear.

If you want to do the former then you should instead update the player tables.

If you want to do the latter then update the team table.

I think that this approach you're taking of using instead-of triggers on a view is a very bad practice, and you should stick to the conventional methods for maintaining the data.

于 2013-09-16T12:26:29.660 回答