I want to create a trigger in a table (called seat) with two attributes, seat number and seat class. If the seat is higher than let's say 50, the class should be 'high', otherwise it should be 'low'. I want the trigger t开发者_运维问答o automatically give the class when i enter a number!
See what I've got till now, tried some alternates as well ..
CREATE TRIGGER trigger_class
AFTER INSERT OR UPDATE ON seat
FOR EACH ROW
BEGIN
IF :NEW.seat_no <=50 THEN :NEW.class_code ='high';
ELSE :NEW.class_code = 'low';
END IF;
END; /
I'm very new into database coding, so... any help would be great!
Change "=" to ":=", put the "/" on a new line, change "AFTER" to "BEFORE", and change "<= 50" to "> 50":
CREATE OR REPLACE TRIGGER trigger_class
BEFORE INSERT OR UPDATE ON seat
FOR EACH ROW
BEGIN
IF :NEW.seat_no > 50 THEN :NEW.class_code :='high';
ELSE :NEW.class_code := 'low';
END IF;
END;
/
Apart from the syntax problems that jonearles already described:
Unless you are just playing around with triggers (or this is some kind of homework), this is not a very good design.
One rule in relational databases is that you should not store information that can be derived from existing data.
You can easily select the class_code during a select, there is no need to store it:
SELECT seat_no, CASE WHEN seat_no > 50 THEN 'high' ELSE 'low' END as class_code FROM seat;
With Oracle 11 you can even define a virtual column that will do the "computation" automatically during retrieval, otherwise you could define a view that will return that information.
精彩评论