-- Add Date Tracking -- c##435uyaw3z_435zv5z7m.people
--------------------------------------------------------------------------------
-- Add Columns
ALTER TABLE c##435uyaw3z_435zv5z7m.people
ADD(tracking_data_date_created DATE DEFAULT SYSDATE NOT NULL,
tracking_data_date_updated DATE DEFAULT SYSDATE NOT NULL
)
;
--INDEX THEM
CREATE
INDEX people_dc
ON people
(tracking_data_date_created)
NOLOGGING
;
CREATE
INDEX people_du
ON people
(tracking_data_date_updated)
NOLOGGING
;
--Create trigger
CREATE TRIGGER people_bu
BEFORE UPDATE
ON people
FOR EACH ROW
BEGIN
IF((:NEW.PERSON_ID IS NULL AND :OLD.PERSON_ID IS NOT NULL) OR (:NEW.PERSON_ID IS
NOT NULL AND :OLD.PERSON_ID IS NULL) OR (:NEW.PERSON_ID<>:OLD.PERSON_ID))
OR((:NEW.FIRST_NAME IS NULL AND :OLD.FIRST_NAME IS NOT NULL) OR (:NEW.FIRST_NAME
IS NOT NULL AND :OLD.FIRST_NAME IS NULL) OR (:NEW.FIRST_NAME<>:OLD.FIRST_NAME))
OR((:NEW.LAST_NAME IS NULL AND :OLD.LAST_NAME IS NOT NULL) OR (:NEW.LAST_NAME IS
NOT NULL AND :OLD.LAST_NAME IS NULL) OR (:NEW.LAST_NAME<>:OLD.LAST_NAME))
OR((:NEW.EMAIL IS NULL AND :OLD.EMAIL IS NOT NULL) OR (:NEW.EMAIL IS NOT NULL
AND :OLD.EMAIL IS NULL) OR (:NEW.EMAIL<>:OLD.EMAIL))
OR((:NEW.PHONE_NUMBER IS NULL AND :OLD.PHONE_NUMBER IS NOT NULL) OR
(:NEW.PHONE_NUMBER IS NOT NULL AND :OLD.PHONE_NUMBER IS NULL) OR
(:NEW.PHONE_NUMBER<>:OLD.PHONE_NUMBER))
THEN
:new.tracking_data_date_updated := SYSDATE;
END IF;
END;
/
--------------------------------------------------------------------------------
-- INSERT -- c##435uyaw3z_435zv5z7m.people
--------------------------------------------------------------------------------
INSERT
INTO c##435uyaw3z_435zv5z7m.people
(person_id, first_name, last_name, email, phone_number)
SELECT person_id, first_name, last_name, email, phone_number
FROM <source table> imp
WHERE NOT EXISTS (SELECT * FROM c##435uyaw3z_435zv5z7m.people tgt WHERE
tgt.person_id = imp.person_id)
;
--------------------------------------------------------------------------------
-- INSERT -- c##435uyaw3z_435zv5z7m.people
--------------------------------------------------------------------------------
INSERT
INTO c##435uyaw3z_435zv5z7m.people
(person_id, first_name, last_name, email, phone_number)
SELECT person_id, first_name, last_name, email, phone_number
FROM <source table> imp
WHERE NOT EXISTS (SELECT * FROM c##435uyaw3z_435zv5z7m.people tgt WHERE
tgt.person_id = imp.person_id)
;