Oracle Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesOracle Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
  #1  
Old September 7th, 2004, 04:33 AM
smiler1 smiler1 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2004
Posts: 22 smiler1 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Major Trigger Confusion

I am using Oracle 9i using the Scott database and need to implement some constraints into the database, however I am having major trouble with the syntax and coding of these triggers enough to satisfy the scenario. These are the ones I have tried to satisfy, however I am totally unsure of what the syntax is for the triiger to work.

Q1) No new orders are to be accepted from customers in the city of BURLINGAME.

CREATE OR REPLACE TRIGGER check_custid
BEFORE UPDATE OF custid ON ord
FOR EACH ROW
WHEN (city = 'BURLINGAME')
BEGIN
IF :city = 'BURLINGAME' THEN
RAISE_APPLICATION_ERROR(-20101,'Cannot accept new order for city of BURLINGAME');
ELSE DISPLAY(‘Update unsuccessful’);
END IF;
END;
/

Q2) No deletions can leave an order without any associated items

CREATE OR REPLACE TRIGGER UpdateItemid
AFTER INSERT OR DELETE OR UPDATE on Ord

DECLARE

CURSOR c_Itemordid IS
SELECT itemid
FROM Item
GROUP BY ordid;

BEGIN
display ('Trigger delete_itemid in progress');

FOR v_ItemRow in c_Itemid LOOP
DELETE itemid

Q3) Any new department based in ‘HOUSTON’ is to have the string ‘OIL’ somewhere in the department’s name.

CREATE OR REPLACE TRIGGER check_dept
BEFORE INSERT OR UPDATE OF loc ON dept
FOR EACH ROW
WHEN(new.dept <> ‘HOUSTON’)
DECLARE
v_OILdept.loc%TYPE;
BEGIN
SELECT loc,
INTO v_loc
FROM dept
WHERE loc = :'HOUSTON';
IF :deptname = 'OIL'
RAISE_APPLICATION_ERROR(-20102’Update succeeded’);
END IF;
END;
/

Q4) The itemtot attribute (the inclusion of which in the database is evidence of very bad design) needs to be automatically maintained and not be directly updateable.

CREATE OR REPLACE TRIGGER check_itemtot
AFTER UPDATE OF itemtot ON Item
DECLARE
v_itemtot_changes NUMBER;
BEGIN
SELECT upd, max_itemtot
INTO v_itemtot_changes
FROM audit_table


Q5) No individual user is allowed to update the products associated with a particular order more than twice

CREATE OR REPLACE TRIGGER secure_Ord
BEFORE INSERT OR UPDATE OR DELETE ON Ord
BEGIN
IF (To_char(Ordid, 'prodid') IN ('ITEM', 'ORD')
THEN
IF DELETING
THEN RAISE_APPLICATION_ERROR(-20103, You may only update
the products more than twice');

END IF
ENDIF;
END;
/

Q6) Any update or insert into the SALGRADE table has to ensure that salary ranges for each grade do not overlap.

CREATE OR REPLACE TRIGGER check_salary_count
AFTER UPDATE OR INSERT OF grade ON salgrade
DECLARE
v__grade_changes NUMBER
v_sal_changes NUMBER;
BEGIN
SELECT upd, max_upd
INTO v_grade_changes, v_max_changes
FROM audit_table
WHERE user_name = user
AND tablename = ‘SALGRADE’
AND column_name = ‘GRADE’

IF v_salary_changes > v_max_changes THEN
RAISE_APPLICATION_ERROR(-20104, ‘Salary Ranges for each grade must not overlap')
END IF;
END;
/

If someone could please help me with the coding, syntax etc I would be ever so grateful - am using this forum as a last resort as I have been tring to do these correctly for the past week!

Please help!

Thanks.

Reply With Quote
  #2  
Old September 7th, 2004, 09:25 AM
shafique shafique is offline
Senior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Location: Canada
Posts: 305 shafique User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 45 m 20 sec
Reputation Power: 5
I have read your triggers, you are writting in right direction but you make only few miner mistakes, You can use the Oracle online documentaion, provided by the Oracle corp.
without any cost, just free registration is required. Please visit http://tahiti.oracle.com

Q1) No new orders are to be accepted from customers in the city of BURLINGAME.

CREATE OR REPLACE TRIGGER check_custid
BEFORE INSERT ON ord -- This trigger only fires when you try to insert any record in BERLINGAMEFOR EACH ROW
WHEN (new.city = 'BURLINGAME')
BEGIN
IF :city = 'BURLINGAME' THEN -- junk code remove it
RAISE_APPLICATION_ERROR(-20101,'Cannot accept new order for city of BURLINGAME');
ELSE DISPLAY(‘Update unsuccessful&#8217; -- junk code remove it
END IF; -- junk code remove it
END;
/

Q2) No deletions can leave an order without any associated items

The best solution for this use inegrity constraint like Primary key/foreign key with ON DELETE CASCADE option

Q3) Any new department based in ‘HOUSTON’ is to have the string ‘OIL’ somewhere in the department’s name.

CREATE OR REPLACE TRIGGER check_dept
BEFORE INSERT OR UPDATE OF loc ON dept
FOR EACH ROW
WHEN(new.city = 'HOUSTON')
DECLARE
v_count number := 0;
BEGIN

SELECT '1' INTO v_count
FROM dual
WHERE :new.department like '%OIL%';

IF v_count <> 1 THEN
RAISE_APPLICATION_ERROR(-20102,'Update can not be taken place');
ELSE
DBMS_OUTPUT.PUT_LINE('Update succeeded');
END IF;
END;
/
Q5) No individual user is allowed to update the products associated with a particular order more than twice

twice within a day, week, month, year or for ever?????

Q6) Any update or insert into the SALGRADE table has to ensure that salary ranges for each grade do not overlap.

CREATE OR REPLACE TRIGGER check_salary_count
AFTER UPDATE OR INSERT ON salgrade
FOR EACH ROW
DECLARE
v_count number := 0
BEGIN
SELECT 1 INTO v_count
FROM SALGRADE
WHERE EXISTS (SELECT '1' FROM SALGRADE
WHERE losal = :new.losal
AND hisal = :new.hisal);
IF v_salary_changes > v_max_changes THEN
RAISE_APPLICATION_ERROR(-20104, ‘Salary Ranges for each grade must not overlap')
END IF;
END;
/

You could make this code more efficient.

Reply With Quote
  #3  
Old September 7th, 2004, 09:30 AM
shafique shafique is offline
Senior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Location: Canada
Posts: 305 shafique User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 45 m 20 sec
Reputation Power: 5
sorry, correct the following queries.

Q6) Any update or insert into the SALGRADE table has to ensure that salary ranges for each grade do not overlap.

CREATE OR REPLACE TRIGGER check_salary_count
AFTER UPDATE OR INSERT ON salgrade
FOR EACH ROW
DECLARE
v_count number := 0
BEGIN
SELECT 1 INTO v_count
FROM SALGRADE
WHERE EXISTS (SELECT '1' FROM SALGRADE
WHERE losal = :new.losal
AND hisal = :new.hisal);
IF v_count = 1 THEN
RAISE_APPLICATION_ERROR(-20104, ‘Salary Ranges for each grade must not overlap')
END IF;
END;
/

Q3) Any new department based in ‘HOUSTON’ is to have the string ‘OIL’ somewhere in the department’s name.

CREATE OR REPLACE TRIGGER check_dept
BEFORE INSERT OR UPDATE OF loc ON dept
FOR EACH ROW
WHEN(new.city = 'HOUSTON')
DECLARE
v_count number := 0;
BEGIN

SELECT 1 INTO v_count
FROM dual
WHERE :new.department like '%OIL%';

IF v_count <> 1 THEN
RAISE_APPLICATION_ERROR(-20102,'Update can not be taken place');
ELSE
DBMS_OUTPUT.PUT_LINE('Update succeeded');
END IF;
END;
/

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesOracle Development > Major Trigger Confusion


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

 Free IT White Papers!
 
Accelerating Trading Partner Performance
One in five. That's how many partner transactions have at least one error. That is an amazing statistic, particularly given the extraordinary leaps in innovation across the global supply chain during the past two decades. Download this white paper to learn more.

 
Competing on Analytics
This Tech Analysis is designed to help identify characteristics shared by analytics competitors, and includes information about 32 organizations that have made a commitment to quantitative, fact-based analysis.

 
Cost Effective Scaling with Virtualization and Coyote Point Systems
An overview of the industry trend toward virtualization, how server consolidation has increased the importance of application uptime and the steps being taken to integrate load balancing technology with virtualized servers.

 
Five Checkpoints to Implementing IP Telephony
Implementation planning for IP PBX software and IP telephony has become vital as businesses replace discontinued legacy PBX phone systems. This informative whitepaper outlines five "checkpoints" for any implementation plan that will help make IP communications a successful proposition.

 
Hosted Email Security: Staying Ahead of New Threats
In the last two years, email has become a fierce battleground between the nefarious forces of spam and malware, and the heroes of messaging protection. The spam volumes increased alarmingly every month, bringing clever new forms of phishing and virus propagation attacks.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway