PostgreSQL - como crear trigger?

 
Vista:

como crear trigger?

Publicado por sergio (2 intervenciones) el 23/03/2008 23:27:36
Que tal amigos del foro, necesito saber como crear triggers en postgre, trate de hacerlo desplegando una tabla de la bd, donde tiene varias opcione suna de ellas es crear trigger, luego desplega una pantallita, en donde tiene varias opciones, donde hay que elegir la funcion, los parametros etc.
La otra forma que intente fue es haciendo clic en la opcion de la barra principal (tiene dibujo de lentes) creo que un analizador de consultas, pero al darle execute a un trigger que saque de eejmplo del help no hace nada ni desplega ningun mensaje.
otra duda es si un trigger tiene que estar asociado si o si a una funcion.
Como se daran cuenta soy novato en el tema.
Como puedo comenzar? desde ya muchas gracias.
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

RE:como crear trigger?

Publicado por rtb (7 intervenciones) el 24/03/2008 21:17:23
Hola Sergio,
la creación de un trigger asociado a una entidad/Tabla tiene basicamente 2 pasos.
1-Crear la funcion-disparadora que vas a ejcutar al realizar una determinada acción en tu tabla
2-Asociar esa funcion-disparadora al disparador de tu tabla creada.-
te puedo enviar un ejemplo sencillo sobre una tabla si quieres...
Saludos
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

RE:como crear trigger?

Publicado por RTB (7 intervenciones) el 28/03/2008 16:12:47
Hola SErgio ahi va un ejemplo probado con la version PostgresQl 8.2----
Espero te sirva:
###crear las tablas ###
CREATE TABLE emp (
empname text,
salary integer,
last_date timestamp,
last_user text
);

CREATE TABLE emp_audit(
operation char(1) NOT NULL,
stamp timestamp NOT NULL,
userid text NOT NULL,
empname text NOT NULL,
salary integer
);

##crear la funcion disparadora
###########################

CREATE FUNCTION emp_stamp() RETURNS trigger AS $emp_stamp$
BEGIN
-- Check that empname and salary are given
IF NEW.empname IS NULL THEN
RAISE EXCEPTION 'empname cannot be null';
END IF;
IF NEW.salary IS NULL THEN
RAISE EXCEPTION '% cannot have null salary', NEW.empname;
END IF;

-- Who works for us when she must pay for it?
IF NEW.salary < 0 THEN
RAISE EXCEPTION '% cannot have a negative salary', NEW.empname;
END IF;

-- Remember who changed the payroll when
NEW.last_date := current_timestamp;
NEW.last_user := current_user;

IF (TG_OP = 'DELETE') THEN
INSERT INTO emp_audit SELECT 'D', now(), user, OLD.empname,OLD.salary;
RETURN OLD;
ELSIF (TG_OP = 'UPDATE') THEN
INSERT INTO emp_audit SELECT 'U', now(), user, NEW.empname,NEW.salary;
RETURN NEW;
ELSIF (TG_OP = 'INSERT') THEN
INSERT INTO emp_audit SELECT 'I', now(), user, NEW.empname,NEW.salary;
RETURN NEW;
END IF;

RETURN NULL;
END;
$emp_stamp$ LANGUAGE plpgsql;

##crear el trigger
###############
CREATE TRIGGER emp_stamp BEFORE INSERT OR UPDATE ON emp
FOR EACH ROW EXECUTE PROCEDURE emp_stamp();

##un ejemplo para probar...
##en la consola de comandos podes probar este insert

insert into emp(empname,salary) values('nuevo01',1000)

Saludos
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

RE:como crear trigger?

Publicado por sergio (2 intervenciones) el 27/03/2008 22:46:15
si me gustaria que me envies un ejemplo si tienes tiempo!!!! muchas gracias!!
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

RE:como crear trigger?

Publicado por rtb (7 intervenciones) el 28/03/2008 16:14:56
Hola SErgio ahi va un ejemplo probado con la version PostgresQl 8.2----
Espero te sirva:
###crear las tablas ###
CREATE TABLE emp (
empname text,
salary integer,
last_date timestamp,
last_user text
);

CREATE TABLE emp_audit(
operation char(1) NOT NULL,
stamp timestamp NOT NULL,
userid text NOT NULL,
empname text NOT NULL,
salary integer
);

##crear la funcion disparadora
###########################

CREATE FUNCTION emp_stamp() RETURNS trigger AS $emp_stamp$
BEGIN
-- Check that empname and salary are given
IF NEW.empname IS NULL THEN
RAISE EXCEPTION 'empname cannot be null';
END IF;
IF NEW.salary IS NULL THEN
RAISE EXCEPTION '% cannot have null salary', NEW.empname;
END IF;

-- Who works for us when she must pay for it?
IF NEW.salary < 0 THEN
RAISE EXCEPTION '% cannot have a negative salary', NEW.empname;
END IF;

-- Remember who changed the payroll when
NEW.last_date := current_timestamp;
NEW.last_user := current_user;

IF (TG_OP = 'DELETE') THEN
INSERT INTO emp_audit SELECT 'D', now(), user, OLD.empname,OLD.salary;
RETURN OLD;
ELSIF (TG_OP = 'UPDATE') THEN
INSERT INTO emp_audit SELECT 'U', now(), user, NEW.empname,NEW.salary;
RETURN NEW;
ELSIF (TG_OP = 'INSERT') THEN
INSERT INTO emp_audit SELECT 'I', now(), user, NEW.empname,NEW.salary;
RETURN NEW;
END IF;

RETURN NULL;
END;
$emp_stamp$ LANGUAGE plpgsql;

##crear el trigger
###############
CREATE TRIGGER emp_stamp BEFORE INSERT OR UPDATE ON emp
FOR EACH ROW EXECUTE PROCEDURE emp_stamp();

##un ejemplo para probar...
##en la consola de comandos podes probar este insert

insert into emp(empname,salary) values('nuevo01',1000)

Saludos
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar