PL/SQL - Simple trigger, take from one column and IF-THEN-ELSE to another column - sql

I am trying to figure out where I am going wrong on this simple trigger. I am quite new to triggers and trying to get used to using them with IFTTT statements.
I want the trigger to watch for a new row entry, and if the value is within a certain range within a column (col_a) it will then enter a certain value in the same row, but different column (col_b), which will be NULL up until this is entered. Please can you help?
CREATE TRIGGER trg_test
BEFORE INSERT
ON test_table
FOR EACH ROW
BEGIN
IF :new.col_a >= 10
THEN :new.col_b := 'High';
ELSE
:new.col_b := 'Low';
END IF;
END;
It just keeps coming back with "success with compilation error".

You can easily do this using a virtual column:
alter table test_table
add col_b generated always as (case when col_a >= 10 then 'High' else 'Low' end);
I wish that exercises in triggers used reasonable use-cases. This is not one -- you need both an insert and an update trigger, for instance.

Related

What is the right prototype for my SQL code?

I am new in learning SQL and I am trying to apply a trig on those two tables by using SQLcl:
prodc
( PRODC_NAME, DISCOUNTED CHAR(1) DEFAULT 'N',
PK PRIMARY KEY (PRODC_NAME));
peopleOrder
( ORDERID, PRODC_NAME,
PRIMARY KEY (ORDERID, PRODC_NAME),
FOREIGN KEY (PRODC_NAME) REFERENCES PRODC (PRODC_NAME));
I want my trig to do the following:
when I update or insert rows to peopleOrder, the trig should check if a row's product_name has value 'N' in the discounted column that is located in the product table; if it has no 'N' value, it should show an error message.
I tried many ways and the following trig, but when I insert rows, the trig seems to not be working and have no effect on the rows:
CREATE OR REPLACE TRIGGER constraint_1
After UPDATE OR INSERT on peopleOrder
for each row
begin
if not (:new.prodc_name in
(select prodc_name from prodc where discounted = 'N' )) then
RAISE_ERROR(-30001, 'No product can be ordered
!');
END IF;
INSERT INTO peopleOrder VALUES (:new.ORDERID, :new.PRODC_NAME);
END;
/
My insert command is:
INSERT INTO peopleOder (ORDERID, PRODC_NAME)
VALUES (251, 'Puton');
and 'Puton' has value 'N' in the discounted column in the prodc table.
The first thing you need is to understand what a trigger is. It is a piece of code that runs as part of the statement causing it to fire. For this reason you cannot reference the table causing it to fire. Normally that is completely unnecessary anyway. Every row trigger has access to to 2 rows: a copy of the existing data (old) and the resulting data (new). Update having both filled and Insert/Delete just new/old as appropriate for the action. Before statement triggers can modify the new data, after triggers cannot. Those rows allow you to process the columns in the table without DML or select on it. (However you cannot 'scan' the table). At any level a trigger may raise an exception which halts the entire process and the invoking statement. If no exception is raised then processing the invoking proceeds. For a more complete description see PL/SQL Language Reference or the version of Oracle you are running.
In this case your trigger breaks down to a single if
statement.
-- Revised. Orig missed that flag comming from different table.
create or replace trigger constraint_1
before update or insert on peopleOrder
for each row
declare
dis_num integer;
begin
select count(*)
into dis_num
from product
where discounted != 'N'
and product_name = :new.product_name
and rownum < 2;
if dis_num != 0 then
raise_application_error(-20001, 'No discounted product can be ordered!');
end if;
end constraint_1;
-- Orig ==============================================
CREATE OR REPLACE TRIGGER constraint_1
before UPDATE OR INSERT on peopleOrder
for each row
begin
if :new.discounted = 'N' then
RAISE_APPLICATION_ERROR(-20001, 'No discounted product can be ordered')
end if;
END;
/

Writing an SQL trigger to find if number appears in column more than X times?

I want to write a Postgres SQL trigger that will basically find if a number appears in a column 5 or more times. If it appears a 5th time, I want to throw an exception. Here is how the table looks:
create table tab(
first integer not null constraint pk_part_id primary key,
second integer constraint fk_super_part_id references bom,
price integer);
insert into tab values(1,NULL,100), (2,1,50), (3,1,30), (4,2,20), (5,2,10), (6,3,20);
Above are the original inserts into the table. My trigger will occur upon inserting more values into the table.
Basically if a number appears in the 'second' column more than 4 times after inserting into the table, I want to raise an exception. Here is my attempt at writing the trigger:
create function check() return trigger as '
begin
if(select first, second, price
from tab
where second in (
select second from tab
group by second
having count(second) > 4)
) then
raise exception ''Error, there are more than 5 parts.'';
end if;
return null;
end
'language plpgsql;
create trigger check
after insert or update on tab
for each row execute procedure check();
Could anyone help me out? If so that would be great! Thanks!
CREATE FUNCTION trg_upbef()
RETURN trigger as
$func$
BEGIN
IF (SELECT count(*)
FROM tab
WHERE second = NEW.second ) > 3 THEN
RAISE EXCEPTION 'Error: there are more than 5 parts.';
END IF;
RETURN NEW; -- must be NEW for BEFORE trigger
END
$func$ LANGUAGE plpgsql;
CREATE TRIGGER upbef
BEFORE INSERT OR UPDATE ON tab
FOR EACH ROW EXECUTE procedure trg_upbef();
Major points
Keyword is RETURNS, not RETURN.
Use the special variable NEW to refer to the newly inserted / updated row.
Use a BEFORE trigger. Better skip early in case of an exception.
Don't count everything for your test, just what you need. Much faster.
Use dollar-quoting. Makes your live easier.
Concurrency:
If you want to be absolutely sure, you'll have to take an exclusive lock on the table before counting. Else, concurrent inserts / updates might outfox each other under heavy concurrent load. While this is rather unlikely, it's possible.

Oracle 11g Trigger that sets a flag when number below x

I need to create a TRIGGER that sets a flag when a product’s quantity on hand falls below 5.
I've created a table "STP_STOCK" in Oracle 11g and it has the following column:
STP_QUANTITY, NUMBER(4,0), RANGE 0-9999
How do I create a trigger that sets a flag in SQL?
I've got as far as the following... but have no idea re setting a flag.
CREATE OR REPLACE TRIGGER STP_STOCK
AFTER UPDATE
ON orders
FOR STP_QUANTITY
BEGIN
END;
Assuming the flag is in the same table, I would suggest you use a before update trigger. Then you can do something like:
CREATE OR REPLACE TRIGGER STP_STOCK
before UPDATE
ON orders
FOR each row
BEGIN
if :new.stp_quantity < 5
then :new.flag := 'Y';
end if;
END;
(I don't have Oracle on hand to test the syntax, so the syntax might have an error.)
You want to do a before trigger, so you can just set the value in :new. It is bad practice to update the same table in an after trigger.
Presumably, you also want to check if the quantity is larger than 5 and set the flag to 'N', but that is not specified in the question.
Finally, instead of a trigger and a flag, you probably really want to do this with a view or virtual column. Something like:
select o.*, (case when stp_quantity < 5 then 'Y' else 'N' end) as Flag
from orders o
(Note: I'm using * as a convenience. In practice, you would want to list each column in the view.)

how to write Instead of update? - Trigger

I have table A and there is a column name COL_A.
I want that if someone change the value, lets say from 1 to 'X' (not costant) that the trigger will change it back from 'X' to 1.
SQLite does not support changing the new column values.
The only way to change a column in a trigger would be to run an UPDATE command,
but that would run the trigger again.
What you can do is to prevent changing the column in the first place:
CREATE TRIGGER IF NOT EXISTS prevent_col_a_change
BEFORE UPDATE OF col_a ON a
BEGIN
SELECT RAISE(ABORT, 'COL_A must not be changed');
END;
UPDATE trigger is a good solution for your case. Just set old value to the new value, that will lead to behaviour you want.
For example:
CREATE OR REPLACE TRIGGER orders_before_update
BEFORE UPDATE
ON orders
FOR EACH ROW
BEGIN
:new.CreatedAt:= :old.CreatedAt;
END;

Oracle SQL: Simple Trigger Problem affecting change of values in if clause

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 to 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.