I have a SQL script for triggers. It was automatically generated by a great software for database modelling called "Win'Design".
So, after I created the database with all the tables (0 data) I wanted to copy/paste the script on the pgAdmin query editor.
Here's an extract:
-- Trigger de modification ----------------------------------------------
CREATE TRIGGER TU_SOURCE_OPEN_DATA
AFTER UPDATE ON SOURCE_OPEN_DATA FOR EACH ROW
EXECUTE PROCEDURE
-- Interdire la modification de la clé étrangère référençant la table
-- SOURCE_PARAMETRE.
if
:OLD.ID_SOURCE_PARAMETRE <> :NEW.ID_SOURCE_PARAMETRE
then
raise_application_error(
-20008,
'Modification de la clé étrangère référençant "SOURCE_PARAMETRE" interdite.');
end if;
/
DROP TRIGGER TI_SOURCE_OPEN_DATA;
-- Trigger d'insertion ----------------------------------------------
CREATE TRIGGER TI_SOURCE_OPEN_DATA
AFTER INSERT ON SOURCE_OPEN_DATA FOR EACH ROW
EXECUTE PROCEDURE
-- Sauf valeur nulle autorisée, interdire la création d'une occurrence de SOURCE_OPEN_DATA
-- s'il n'existe pas d'occurrence correspondante dans la table SOURCE_PARAMETRE.
select count(*) into numrows
from SOURCE_PARAMETRE
where
:NEW.ID_SOURCE_PARAMETRE = SOURCE_PARAMETRE.ID_SOURCE_PARAMETRE;
if
(
numrows = 0
)
then
raise_application_error(
-20002,
'Impossible d''ajouter "SOURCE_OPEN_DATA" car "SOURCE_PARAMETRE" n''existe pas.');
end if;
/
-- -------------------------------------------------------------------------------
-- Table : SECTEUR
-- -------------------------------------------------------------------------------
DROP TRIGGER TD_SECTEUR;
-- Trigger de suppression ----------------------------------------------
CREATE TRIGGER TD_SECTEUR
AFTER DELETE ON SECTEUR FOR EACH ROW
EXECUTE PROCEDURE
-- Supprimer les occurrences correspondantes de la table INTERESSE.
delete from INTERESSE
where
INTERESSE.ID_SECTEUR = :OLD.ID_SECTEUR;
/
and here's the message error :
ERROR: syntax error at or near ":"
LINE 9: :OLD.ID_SOURCE_PARAMETRE <> :NEW.ID_SOURCE_PARAMET...
^
********** Error **********
ERROR: syntax error at or near ":"
SQL state: 42601
Character: 304
i tried deleting until "Trigger d'insertion" and still got an error
ERROR: syntax error at or near "select"
LINE 9: select count(*) into numrows
^
********** Error **********
ERROR: syntax error at or near "select"
SQL state: 42601
Character: 369
1st time using triggers... please help
EDIT: As @Laurenz Albe and @pozs correctly pointed out, it is indeed oracle syntax. This is odd, as i specified during extraction PostgreSQL. So i tried with another Postgre9.1 and it generated this script:
--
-------------------------------------------------------------------------------
-- Table : SOURCE_OPEN_DATA
-- -------------------------------------------------------------------------------
DROP TRIGGER TU_SOURCE_OPEN_DATA;
-- TRIGGER DE MODIFICATION --------------------------------
CREATE TRIGGER TU_SOURCE_OPEN_DATA
AFTER UPDATE ON SOURCE_OPEN_DATA
REFERENCING OLDROW, NEWROW
FOR EACH ROW
IMPORT
import java.sql.* ;
BEGIN
-- Interdire la modification de la clé étrangère référençant la table
-- SOURCE_PARAMETRE.
if
OLDROW.getValue(10, CHAR);.ID_SOURCE_PARAMETRE <> NEWROW.getValue(10, CHAR);.ID_SOURCE_PARAMETRE
then
raise_application_error(
-20008,
'Modification de la clé étrangère référençant "SOURCE_PARAMETRE" interdite.');
end if;
END;
DROP TRIGGER TI_SOURCE_OPEN_DATA;
-- TRIGGER D'INSERTION --------------------------------------
CREATE TRIGGER TI_SOURCE_OPEN_DATA
AFTER INSERT ON SOURCE_OPEN_DATA
REFERENCING OLDROW, NEWROW
FOR EACH ROW
IMPORT
import java.sql.* ;
BEGIN
END;
Again, this is just an extract. Now I get
ERROR: syntax error at or near ";"
LINE 11: DROP TRIGGER TU_SOURCE_OPEN_DATA;
^
********** Error **********
ERROR: syntax error at or near ";"
RETURNS trigger) and use that function inCREATE TRIGGER.raise_application_errorfunction calls, it seems that it could be generated for Oracle. If you can, generate these scripts for PostgreSQL specifically.... interdire la création d'une occurrence de SOURCE_OPEN_DATA -- s'il n'existe pas d'occurrence correspondante dans la table SOURCE_PARAMETREThat is what foreign key constraints are intended for.