Discussions
Categories
- 197K All Categories
- 2.5K Data
- 546 Big Data Appliance
- 1.9K Data Science
- 450.8K Databases
- 221.9K General Database Discussions
- 3.8K Java and JavaScript in the Database
- 31 Multilingual Engine
- 552 MySQL Community Space
- 479 NoSQL Database
- 7.9K Oracle Database Express Edition (XE)
- 3.1K ORDS, SODA & JSON in the Database
- 556 SQLcl
- 4K SQL Developer Data Modeler
- 187.2K SQL & PL/SQL
- 21.4K SQL Developer
- 296.4K Development
- 17 Developer Projects
- 139 Programming Languages
- 293K Development Tools
- 110 DevOps
- 3.1K QA/Testing
- 646.1K Java
- 28 Java Learning Subscription
- 37K Database Connectivity
- 158 Java Community Process
- 105 Java 25
- 22.1K Java APIs
- 138.2K Java Development Tools
- 165.3K Java EE (Java Enterprise Edition)
- 19 Java Essentials
- 162 Java 8 Questions
- 86K Java Programming
- 81 Java Puzzle Ball
- 65.1K New To Java
- 1.7K Training / Learning / Certification
- 13.8K Java HotSpot Virtual Machine
- 94.3K Java SE
- 13.8K Java Security
- 205 Java User Groups
- 24 JavaScript - Nashorn
- Programs
- 468 LiveLabs
- 39 Workshops
- 10.2K Software
- 6.7K Berkeley DB Family
- 3.5K JHeadstart
- 5.7K Other Languages
- 2.3K Chinese
- 175 Deutsche Oracle Community
- 1.1K Español
- 1.9K Japanese
- 233 Portuguese
A trigger to give a 10 per cent discount to a regular customer

Hi,
I am trying to create a trigger to give a discount of 10% of the field 'totalCost' in the bookings table, if the 'statusname' is regular in the Passengers table. Below is what I have come up with so far. But I know its wrong. Can you please help?
create or replace TRIGGER Ten_Percent_Discount BEFORE UPDATE ON Bookings FOR EACH ROW BEGIN UPDATE Bookings b SET b.TotalCost = 0.10 * TotalCost WHERE Passengers.StatusName = 'Regular'; END;
Thanks,
Fareedh
Best Answer
-
Assuming you have a passenger_id in your bookings table you probably can use something like this
eate or replace TRIGGER Ten_Percent_Discount
BEFORE UPDATE ON Bookings
FOR EACH ROWCURSOR C_Passengers
IS
SELECT Status_Name
FROM Passengers
WHERE Passenger_Id = :NEW.Passenger_Id
;l_status_name Passengers.Status_Name%TYPE;
BEGIN
OPEN C_Passengers;
FETCH C_Passengers INTO l_status_name;
CLOSE C_Passengers;
IF l_status_name = 'Regular'
THEN
:New.total_cost = 0.90 * :New.Total_Cost;
END IF;END;
Answers
-
You are trying to update the table that is updating .. simple do this
- create or replace TRIGGER Ten_Percent_Discount
- BEFORE UPDATE ON Bookings
- FOR EACH ROW
- BEGIN
- :New.total_cost = 0.90 * :New.Total_Cost;
- END;
Assuming you want to give 10% discount instead of 90% ;-).
And where does your Passengers.Statusname come from :-)?
-
Hi,
Thanks for the response
Its from the Passengers table.
Thanks,
Fareedh
-
Assuming you have a passenger_id in your bookings table you probably can use something like this
eate or replace TRIGGER Ten_Percent_Discount
BEFORE UPDATE ON Bookings
FOR EACH ROWCURSOR C_Passengers
IS
SELECT Status_Name
FROM Passengers
WHERE Passenger_Id = :NEW.Passenger_Id
;l_status_name Passengers.Status_Name%TYPE;
BEGIN
OPEN C_Passengers;
FETCH C_Passengers INTO l_status_name;
CLOSE C_Passengers;
IF l_status_name = 'Regular'
THEN
:New.total_cost = 0.90 * :New.Total_Cost;
END IF;END;
-
Little type, use StatusName rather than Status_Name of course ..
-
Hi,
Thanks for your help. But can you please explain on what the trigger is doing as I am really new to triggers and does it execute the trigger when I update the statusname?
Thanks,
Fareedh
-
And TotalCost rather than Total_Cost :-)
-
I am getting a syntax error on this line called something expected
:New.TotalCost = 0.90 * :New.TotalCost;
-
3128080 wrote: Hi, I am trying to create a trigger to give a discount of 10% of the field 'totalCost' in the bookings table, if the 'statusname' is regular in the Passengers table. Below is what I have come up with so far. But I know its wrong. Can you please help?
- create or replace TRIGGER Ten_Percent_Discount
- BEFORE UPDATE ON Bookings
- FOR EACH ROW
- BEGIN
- UPDATE Bookings b
- SET b.TotalCost = 0.10 * TotalCost
- WHERE Passengers.StatusName = 'Regular';
- END;
create or replace TRIGGER Ten_Percent_Discount BEFORE UPDATE ON Bookings FOR EACH ROW BEGIN UPDATE Bookings b SET b.TotalCost = 0.10 * TotalCost WHERE Passengers.StatusName = 'Regular'; END;
Thanks, Fareedh
This is business logic and really shouldn't be something you put in a trigger. It should be at the application/business layer of your coding, not the database layer.
Triggers should be used to adjust or default values on the table to which the trigger is associated, and should ideally be the minimal code to allow for the trigger to execute quickly (consider what happens if you have to populate the table with a large quantity of data - the trigger has to fire for each row inserted).
Move this logic to your business layer where you can query the other table to determine the status and adjust the cost accordingly as the booking is inserted/updated.
-
Hi,
Thanks for the reply. I totally understand that however this is for my assignment and it requires a business rule trigger and I could not think of anything else.
Can you also please tell me on why I am getting a syntax error on this line :New.TotalCost = 0.90 * :New.TotalCost;
Thanks
-
I agree that this logic should not be in a trigger (nothing should be in a trigger as far as I'm concerned ;-)).
However, just to get this working ... what is your syntax error? Maybe submit your code as you have it now?
I think you missed :New.TotalCost := 0.90 * :New.TotalCost;
So the : should be before the =.
You seem fairly new to PL/SQL, this is not a production system thing is it?? Looks more like an excercise to learn PL/SQL?