-- @@@ START COPYRIGHT @@@
--
-- Licensed to the Apache Software Foundation (ASF) under one
-- or more contributor license agreements.  See the NOTICE file
-- distributed with this work for additional information
-- regarding copyright ownership.  The ASF licenses this file
-- to you under the Apache License, Version 2.0 (the
-- "License"); you may not use this file except in compliance
-- with the License.  You may obtain a copy of the License at
--
--   http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing,
-- software distributed under the License is distributed on an
-- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-- KIND, either express or implied.  See the License for the
-- specific language governing permissions and limitations
-- under the License.
--
-- @@@ END COPYRIGHT @@@
-- RIs - triggers integration
-- 'NO-ACTION RI cascading from an after trigger

------------------------------------------------------------------
-- SET ENVIRONMENT
------------------------------------------------------------------


obey TEST_7_1_1(clean_up);
obey TEST_7_1_1(set_up);
log   LOG_7_1_1 clear;
obey TEST_7_1_1(tests);
LOG;
obey TEST_7_1_1(clean_up);
exit;

?section clean_up
set schema cat1.schm_alt;
DROP TRIGGER atrig711;
ALTER TABLE tab1A DROP CONSTRAINT ref_constr;

------------------------------------------------------------------
obey clearTables;

?section set_up
set schema cat1.schm_alt;
ALTER TABLE tab1A ADD CONSTRAINT ref_constr 
	FOREIGN KEY (b) REFERENCES tab1B;  

INSERT INTO tab1B VALUES (1,2);
INSERT INTO tab1B VALUES (2,3);
INSERT INTO tab1B VALUES (3,4);

INSERT INTO tab1A VALUES (1, 1, 1, 1);
INSERT INTO tab1A VALUES (2, 1, 1, 1);
INSERT INTO tab1A VALUES (3, 2, 3, 3);

---------------------------------
-- DEFINE TRIGGER atrig711
---------------------------------

CREATE TRIGGER atrig711
AFTER DELETE ON tab1A
REFERENCING OLD AS oldR
FOR EACH ROW
DELETE FROM tab1B 
WHERE a = oldR.b;
?section tests

------------------------------------------------------------------
--          TEST CASE
------------------------------------------------------------------

DELETE FROM tab1A WHERE a=3;

SELECT * FROM tab1A ORDER BY a; -- check result

SELECT * FROM tab1B ORDER BY a; -- check result

-----------------------------------------------------------

DELETE FROM tab1A WHERE a=2;

SELECT * FROM tab1A ORDER BY a; -- check result

SELECT * FROM tab1B ORDER BY a; -- check result

------------------------------------------------------------------
--           END
------------------------------------------------------------------
