Reputation: 912
I get this error when trying to delete a row on a view
"ORA-01732: data manipulation operation not legal on this view "
SQL Command issued is
DELETE FROM RegisterHelper
WHERE StudentID = 111111111 AND CourseID = 'ASD123';
This creates the errors. I've doublechecked that these values exists.
My view is
CREATE VIEW RegisterHelper AS
SELECT studentId, courseID, NULL as position, 'registered' AS status
FROM registeredOn
UNION
SELECT studentId, courseID, position, 'waiting' AS status
FROM waitingOn;
This view creates a complete list of waiting and registered students on all courses, together with their status (which is used in the trigger). All the data given here is essential to the trigger.
Trigger is
CREATE OR REPLACE TRIGGER CourseUnregistration
INSTEAD OF DELETE ON RegisterHelper
etc
Tables are as follows
CREATE TABLE RegisteredOn (
StudentID REFERENCES Student(StudentID) NOT NULL,
CourseID REFERENCES Course(CourseID) NOT NULL
);
CREATE TABLE WaitingOn (
StudentID REFERENCES Student(StudentID) NOT NULL,
CourseID REFERENCES Course(CourseID) NOT NULL,
Position INT NOT NULL,
PRIMARY KEY(StudentID, CourseID)
);
Since I only delete from tables with references values, there will be no problems with deleting values with are referenced (obviously) - this should pose no problem.
An interesting thing was that it actually worked to issue the DELETE command on the RegisterHelper view a few hours ago. For some reason it has stopped working, and I can't remember changing it.
The reason the trigger exists on the RegisterHelper view is because students are both inserted and removed on the RegisteredOn and WaitingOn table. Inserts works fine, removal does not, as I get the error.
Simply put, I just want the trigger to work when something is deleted on the RegisteredOn or WaitingOn tables. If anyone has a different solution I'm open to it.
Upvotes: 1
Views: 3360
Reputation: 44941
You can't delete from a view with unioned tables that are different. You will need to execute two different delete statements, one for each table.
Update
In order to make the operation as easy from code as the single delete statement you are looking for now, I would suggest that you create a stored procedure to perform the deletion and pass the student and course information to this proc.
This is a good general design anyway because if you add additional tables whose data may need to be deleted in the future in the same logic (lab registration, for example), then you only have to modify the stored procedure and not the dynamic SQL.
Upvotes: 3