You are on page 1of 13

c 

   

O  
    

     


!  "#$ % &      
 '(  ( #

   )  *   


'+  ,   *    #$*
& ,    -.$/0 120$* 1$ $0$
 3#

       

¢V  )  2.0$ )  -.$/0120$*1$ $0$


¢V  )  1$$ )  -.$/0120$*1$ $0$
¢V  ) +  &(  !
&  "
¢V  ) +     ! 4
&  "

$* )   (      


#

CREATE TRIGGER nombre { BEFORE | AFTER } { INSERT | UPDATE | DELETE [ OR


... ] }
ON tabla [ FOR [ EACH ] { ROW | STATEMENT } ]
EXECUTE PROCEDURE nombre de funcion ( argumentos )

2    )   ) 


+  ,     &#

$           
 )   +     
     #$ +)  ( 
+,  #   *  
 % 5 '  5

-

   
2 *   %' ,  
     '*    
) &' (     

#V $ ) &' (    


      #

#V  ) &' (       
  ') && 5  5#

6#V    (  7  
 #

8#V   (   ) +  
&(  !
&") &&  . #

9#V   (   ) +  &(
   ! 4
&" &&  
#

#V   (   ) +  &(
   ! 4
&"'2.0$+  
 ) (*  
#V /  .    *#
#V :&&  !/$;</1"

=#V   (   ) + 1$$
+   ) (* &   ) 
    .  #

>#V $     
  ( 3  ) && *
. * & /$;</1  ) ) (*
3  #

#V  ,     &
!-.$/0120$1$ $0$"+  , ? 
   #$  2.0$ 4
&
     &   # 
  .  * ,  #

#V  (    +  
 )  &( &    #$
  #.@% 7   ) 
       &    *
)     
 &#
< ) )   )    ) 
   ,) +   *3  #
$     *&  &
  #       
  ' 3      
  *#

U    

;   *        
& 3 *#$&   

.$A
0/$;</1BC )  &  
-.$/0120$   4
&#$& . 
  
&#

< 1
0/$;</1BC )   
 120$1$ $0$   4
&#$& . 
  
&#

0DE.2F$
0B& )    ) , 
 * #

0DEAG$.
0@B @& H$I</$2I0$/
  ) ,  * 3

0DE $C$ 
0@B @& /<A020$F$.0
  ) ,  * 3

0DE<
0@B @& -.$/0120$1$ $0$
 *) 3&  ) ,  *
 #

0DE/$ -1
0B +) 3&  ) 
,  * #

0DE/$ .2F$
0B ) 3&  ) , 
 * #$& '     # 
0DE02H $E.2F$#

0DE02H $E.2F$
0B ) 3&  ) , 
 * #

0DE02H $E;G$F2
0B 3) 3&  ) 
,  * #

0DE.2/D
0 B7    
;/$20$0/-DD$/#

0DE2/DCJK
0@ 'B  ;/$20$0/-DD$/#$%
( #-&,! ) *'  ) 
E "  &  #

ï 


&() 3& ,  + )   


+ , &  '    #
!+3   >#6#="#

;    (   + 

Êostgres@server:~$ Êsql
Welcome to Êsql 8.3.7, the PostgreSQL interactive terminal.

TyÊe: \coÊyright for distribution terms


\h for helÊ with SQL commands
\? for helÊ with Êsql commands
\g or terminate with semicolon to execute query
\q to quit

Êostgres=# CREATE DATABASE test001;


CREATE DATABASE

Êostgres=# \c test001
You are now connected to database "test001".

test001=#

 % ) ) 3   +)


#

CREATE PROCEDURAL LANGUAGE ÊlÊgsql;


23            

CREATE TABLE numeros(


numero bigint NOT NULL,
cuadrado bigint,
cubo bigint,
raiz2 real,
raiz3 real,
PRIMARY KEY (numero)
);

1 ?)     *        
  #.     *,)   '7) 
3 , ,&& & . 

CREATE OR REPLACE FUNCTION Êroteger_datos() RETURNS TRIGGER AS


$Êroteger_datos$
DECLARE
BEGIN

--
-- Esta funcion es usada Êara Êroteger datos en un tabla
-- No se Êermitira el borrado de filas si la usamos
-- en un disÊarador de tiÊo BEFORE / row-level
--

RETURN NULL;
END;
$Êroteger_datos$ LANGUAGE ÊlÊgsql;

2 *÷    H$I</$ 4


&
  *1$ $0$#F,&   

CREATE TRIGGER Êroteger_datos BEFORE DELETE


ON numeros FOR EACH ROW
EXECUTE PROCEDURE Êroteger_datos();

*  3)  

test001=# \d numeros
Table "Êublic.numeros"
Column | TyÊe | Modifiers
----------+--------+-----------
numero | bigint | not null
cuadrado | bigint |
cubo | bigint |
raiz2 | real |
raiz3 | real |
Indexes:
"numeros_Ê ey" PRIMARY KEY, btree (numero)
Triggers:
Êroteger_datos BEFORE DELETE ON numeros
FOR EACH ROW EXECUTE PROCEDURE Êroteger_datos()
23 &   & * ,'  &  
  ÷  

CREATE OR REPLACE FUNCTION rellenar_datos() RETURNS TRIGGER AS


$rellenar_datos$
DECLARE
BEGIN

NEW.cuadrado := Êower(NEW.numero,2);
NEW.cubo := Êower(NEW.numero,3);
NEW.raiz2 := sqrt(NEW.numero);
NEW.raiz3 := cbrt(NEW.numero);

RETURN NEW;
END;
$rellenar_datos$ LANGUAGE ÊlÊgsql;
CREATE TRIGGER rellenar_datos BEFORE INSERT OR UPDATE
ON numeros FOR EACH ROW
EXECUTE PROCEDURE rellenar_datos();

*  3)  

test001=# \d numeros
Table "Êublic.numeros"
Column | TyÊe | Modifiers
----------+--------+-----------
numero | bigint | not null
cuadrado | bigint |
cubo | bigint |
raiz2 | real |
raiz3 | real |
Indexes:
"numeros_Ê ey" PRIMARY KEY, btree (numero)
Triggers:
Êroteger_datos BEFORE DELETE ON numeros
FOR EACH ROW EXECUTE PROCEDURE Êroteger_datos()
rellenar_datos BEFORE INSERT OR UPDATE ON numeros
FOR EACH ROW EXECUTE PROCEDURE rellenar_datos()

23 &&   ) 3÷ 


  

test001=#    
 
numero | cuadrado | cubo | raiz2 | raiz3
--------+----------+------+-------+-------
(0 rows)

test001=#  


 
 
INSERT 0 1

test001=#    
 
numero | cuadrado | cubo | raiz2 | raiz3
--------+----------+------+---------+---------
2 | 4 | 8 | 1.41421 | 1.25992
(1 rows)
test001=#  
 
 
INSERT 0 1

test001=# SELECT * from numeros;


numero | cuadrado | cubo | raiz2 | raiz3
--------+----------+------+---------+---------
2 | 4 | 8 | 1.41421 | 1.25992
3 | 9 | 27 | 1.73205 | 1.44225
(2 rows)

test001=#  
  
  

UPDATE 1

test001=#    
 
numero | cuadrado | cubo | raiz2 | raiz3
--------+----------+------+---------+---------
2 | 4 | 8 | 1.41421 | 1.25992
4 | 16 | 64 | 2 | 1.5874
(2 rows)

G (-.$/0'120$#$)   


3+  *÷ 
  &( 
' ( ÷ #

;   3 ( ÷  


   &  !    
'"& #

1 (*3  *÷ 


    
  #C( ) 3 * 

NEW.cuadrado := Êower(NEW.numero,2);
NEW.cubo := Êower(NEW.numero,3);
NEW.raiz2 := sqrt(NEW.numero);
NEW.raiz3 := cbrt(NEW.numero);

RETURN NEW;

¢V ; +   -.$/0!  L"  ÷ 



 *÷ 
  &(#

¢V $& & .$A( +  ÷ 
   L
  L.  L.  (L.  (6L. #

¢V .  & #

¢V 2 *     (  ' ( 
'& .$A#  .$A# .$A# ('
.$A# (6#

¢V $& & .$A/$0/..$A3 
  L  L8 L> (L#88 (6L#9 #

¢V ;/$0/..$A  !/$;</1"
& .$A' *÷ 
 #$
/$;</1.$A÷ 

;&  '*#

1    


 
 +  
1$ $0$#2  +  , *
 
 #

$ *  & . '  #


 % )  +  ,1$ $0$# ' 
   ,  ÷ #

test001=#     !
 
DELETE 0

test001=#    
 
numero | cuadrado | cubo | raiz2 | raiz3
--------+----------+------+---------+---------
2 | 4 | 8 | 1.41421 | 1.25992
4 | 16 | 64 | 2 | 1.5874
(2 rows)

C  #  &   


  
 
'÷ 
#

test001=# DROP TRIGGER Êroteger_datos ON numeros;


DROP TRIGGER

test001=# DROP TRIGGER rellenar_datos ON numeros;


DROP TRIGGER

2 *     7 -.$/0120$'


1$ $0$#$ &   ( ,  & *)  )  
 ) 3&  )     & 
 #  ( & 0DE<#

CREATE OR REPLACE FUNCTION Êroteger_y_rellenar_datos() RETURNS TRIGGER AS


$Êroteger_y_rellenar_datos$
DECLARE
BEGIN

IF (TG_OP = 'INSERT' OR TG_OP = 'UPDATE' ) THEN

NEW.cuadrado := Êower(NEW.numero,2);
NEW.cubo := Êower(NEW.numero,3);
NEW.raiz2 := sqrt(NEW.numero);
NEW.raiz3 := cbrt(NEW.numero);
RETURN NEW;

ELSEIF (TG_OP = 'DELETE') THEN


RETURN NULL;

END IF;
END;
$Êroteger_y_rellenar_datos$ LANGUAGE ÊlÊgsql;
CREATE TRIGGER Êroteger_y_rellenar_datos BEFORE INSERT OR UPDATE OR
DELETE
ON numeros FOR EACH ROW
EXECUTE PROCEDURE Êroteger_y_rellenar_datos();

*  3)  

test001=# \d numeros
Table "Êublic.numeros"
Column | TyÊe | Modifiers
----------+--------+-----------
numero | bigint | not null
cuadrado | bigint |
cubo | bigint |
raiz2 | real |
raiz3 | real |
Indexes:
"numeros_Ê ey" PRIMARY KEY, btree (numero)
Triggers:
rellenar_datos BEFORE INSERT OR DELETE OR UPDATE ON numeros
FOR EACH ROW EXECUTE PROCEDURE Êroteger_y_rellenar_datos()

M  ,  )   


( 

test001=#    
 
numero | cuadrado | cubo | raiz2 | raiz3
--------+----------+------+---------+---------
2 | 4 | 8 | 1.41421 | 1.25992
4 | 16 | 64 | 2 | 1.5874
(2 rows)

test001=#  


 
 "
INSERT 0 1

test001=#  


 
 #
INSERT 0 1

test001=#    
 
numero | cuadrado | cubo | raiz2 | raiz3
--------+----------+------+---------+---------
2 | 4 | 8 | 1.41421 | 1.25992
4 | 16 | 64 | 2 | 1.5874
5 | 25 | 125 | 2.23607 | 1.70998
6 | 36 | 216 | 2.44949 | 1.81712
(4 rows)
test001=#  
  
$%  
#
UPDATE 1

test001=#    
 
numero | cuadrado | cubo | raiz2 | raiz3
--------+----------+------+---------+---------
2 | 4 | 8 | 1.41421 | 1.25992
4 | 16 | 64 | 2 | 1.5874
5 | 25 | 125 | 2.23607 | 1.70998
10 | 100 | 1000 | 3.16228 | 2.15443
(4 rows)

test001=#     !
 &'  
$%
DELETE 0

test001=#    
 
numero | cuadrado | cubo | raiz2 | raiz3
--------+----------+------+---------+---------
2 | 4 | 8 | 1.41421 | 1.25992
4 | 16 | 64 | 2 | 1.5874
5 | 25 | 125 | 2.23607 | 1.70998
10 | 100 | 1000 | 3.16228 | 2.15443
(4 rows)

 7'  &    


&) 
+    -.$/0120$'1$ $0$#  *
+      ,+ * !
 &  3&  + ) &
 "

    ( &   & 

CREATE TABLE cambios(


timestamÊ_ TIMESTAMP WITH TIME ZONE default NOW(),
nombre_disÊarador text,
tiÊo_disÊarador text,
nivel_disÊarador text,
comando text
);

 *  

CREATE OR REPLACE FUNCTION grabar_oÊeraciones() RETURNS TRIGGER AS


$grabar_oÊeraciones$
DECLARE
BEGIN

INSERT INTO cambios (


nombre_disÊarador,
tiÊo_disÊarador,
nivel_disÊarador,
comando)
VALUES (
TG_NAME,
TG_WHEN,
TG_LEVEL,
TG_OP
);

RETURN NULL;
END;
$grabar_oÊeraciones$ LANGUAGE ÊlÊgsql;

M      

CREATE TRIGGER grabar_oÊeraciones AFTER INSERT OR UPDATE OR DELETE


ON numeros FOR EACH STATEMENT
EXECUTE PROCEDURE grabar_oÊeraciones();

*  )   

test001=# \d numeros;
Table "Êublic.numeros"
Column | TyÊe | Modifiers
----------+--------+-----------
numero | bigint | not null
cuadrado | bigint |
cubo | bigint |
raiz2 | real |
raiz3 | real |
Indexes:
"numeros_Ê ey" PRIMARY KEY, btree (numero)
Triggers:
grabar_oÊeraciones AFTER INSERT OR DELETE OR UPDATE ON numeros
FOR EACH STATEMENT EXECUTE PROCEDURE grabar_oÊeraciones()
Êroteger_y_rellenar_datos BEFORE INSERT OR DELETE OR UPDATE ON
numeros
FOR EACH ROW EXECUTE PROCEDURE Êroteger_y_rellenar_datos()

2 *&    

test001=#    () *+ 


timestamÊ_ | nombre_disÊarador | tiÊo_disÊarador | nivel_disÊarador |
comando
------------+-------------------+-----------------+------------------+---
------
(0 rows)

test001=#  


 
 $%%
INSERT 0 1

test001=#    
 
numero | cuadrado | cubo | raiz2 | raiz3
--------+----------+---------+---------+---------
2 | 4 | 8 | 1.41421 | 1.25992
4 | 16 | 64 | 2 | 1.5874
5 | 25 | 125 | 2.23607 | 1.70998
10 | 100 | 1000 | 3.16228 | 2.15443
100 | 10000 | 1000000 | 10 | 4.64159
(5 rows)

test001=#    () *+ 


timestamÊ_ | nombre_disÊarador | tiÊo_disÊarador |
nivel_disÊarador | comando
-------------------------------+--------------------+-----------------+--
----------------+---------
2009-06-11 23:05:29.794534+02 | grabar_oÊeraciones | AFTER |
STATEMENT | INSERT
(1 row)

test001=#  
  
$%%%  
$%%
UPDATE 1

test001=#    
 
numero | cuadrado | cubo | raiz2 | raiz3
--------+----------+------------+---------+---------
2 | 4 | 8 | 1.41421 | 1.25992
4 | 16 | 64 | 2 | 1.5874
5 | 25 | 125 | 2.23607 | 1.70998
10 | 100 | 1000 | 3.16228 | 2.15443
1000 | 1000000 | 1000000000 | 31.6228 | 10
(5 rows)

test001=#    () *+ 


timestamÊ_ | nombre_disÊarador | tiÊo_disÊarador |
nivel_disÊarador | comando
-------------------------------+--------------------+-----------------+--
----------------+---------
2009-06-11 23:05:29.794534+02 | grabar_oÊeraciones | AFTER |
STATEMENT | INSERT
2009-06-11 23:06:08.259421+02 | grabar_oÊeraciones | AFTER |
STATEMENT | UPDATE
(2 rows)

test001=#     !
 &'  
$%%%
DELETE 0

test001=#    
 
numero | cuadrado | cubo | raiz2 | raiz3
--------+----------+------------+---------+---------
2 | 4 | 8 | 1.41421 | 1.25992
4 | 16 | 64 | 2 | 1.5874
5 | 25 | 125 | 2.23607 | 1.70998
10 | 100 | 1000 | 3.16228 | 2.15443
1000 | 1000000 | 1000000000 | 31.6228 | 10
(5 rows)

test001=#    () *+ 


timestamÊ_ | nombre_disÊarador | tiÊo_disÊarador |
nivel_disÊarador | comando
-------------------------------+--------------------+-----------------+--
----------------+---------
2009-06-11 23:05:29.794534+02 | grabar_oÊeraciones | AFTER |
STATEMENT | INSERT
2009-06-11 23:06:08.259421+02 | grabar_oÊeraciones | AFTER |
STATEMENT | UPDATE
2009-06-11 23:06:26.568632+02 | grabar_oÊeraciones | AFTER |
STATEMENT | DELETE
(3 rows)

M7+  %      #


)     *'  &  *#