<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Java, Programming, Struts2, jQuery, Linux and more &#187; plpgsql</title>
	<atom:link href="http://www.jgeppert.com/tag/plpgsql/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.jgeppert.com</link>
	<description>Johannes Geppert</description>
	<lastBuildDate>Thu, 09 Feb 2012 16:38:30 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>Erstellen von Triggern in PostgreSQL</title>
		<link>http://www.jgeppert.com/2008/07/erstellen-von-triggern-in-postgresql/</link>
		<comments>http://www.jgeppert.com/2008/07/erstellen-von-triggern-in-postgresql/#comments</comments>
		<pubDate>Mon, 28 Jul 2008 12:45:48 +0000</pubDate>
		<dc:creator>jogep</dc:creator>
				<category><![CDATA[datenbanken]]></category>
		<category><![CDATA[pgsql]]></category>
		<category><![CDATA[plpgsql]]></category>
		<category><![CDATA[postgres]]></category>
		<category><![CDATA[postgresql]]></category>
		<category><![CDATA[trigger]]></category>

		<guid isPermaLink="false">http://www.jgeppert.com/?p=139</guid>
		<description><![CDATA[<div class="addthis_toolbox addthis_default_style " addthis:url='http://www.jgeppert.com/2008/07/erstellen-von-triggern-in-postgresql/' addthis:title='Erstellen von Triggern in PostgreSQL '  ><a class="addthis_button_facebook_like" fb:like:layout="button_count"></a><a class="addthis_button_tweet"></a><a class="addthis_counter addthis_pill_style"></a></div>Das folgende Beispiel zeigt einen Trigger, der bei jedem INSERT in die Tabelle test, den Wert in der Tabelle statistik erhöht. Trigger können in Postgres in verschiedenen Sprachen geschrieben werden, in diesem Beispiel werden wir die prozedurale Sprache PGSQL verwenden. Dazu müssen wir die Sprache der Datenbank bekannt machen. CREATE TRUSTED PROCEDURAL LANGUAGE 'plpgsql' HANDLER [...]<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://www.jgeppert.com/2008/07/erstellen-von-triggern-in-postgresql/' addthis:title='Erstellen von Triggern in PostgreSQL ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></description>
			<content:encoded><![CDATA[<div class="addthis_toolbox addthis_default_style " addthis:url='http://www.jgeppert.com/2008/07/erstellen-von-triggern-in-postgresql/' addthis:title='Erstellen von Triggern in PostgreSQL '  ><a class="addthis_button_facebook_like" fb:like:layout="button_count"></a><a class="addthis_button_tweet"></a><a class="addthis_counter addthis_pill_style"></a></div>Das folgende Beispiel zeigt einen Trigger, der bei jedem INSERT in die Tabelle <strong>test</strong>, den Wert in der Tabelle <strong>statistik</strong> erhöht.

Trigger können in <a href="http://www.postgresql.org/">Postgres</a> in verschiedenen Sprachen geschrieben werden, in diesem Beispiel werden wir die prozedurale Sprache <em>PGSQL</em> verwenden. Dazu müssen wir die Sprache der Datenbank bekannt machen.


<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"> <span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TRUSTED</span> <span style="color: #993333; font-weight: bold;">PROCEDURAL</span> <span style="color: #993333; font-weight: bold;">LANGUAGE</span> <span style="color: #ff0000;">'plpgsql'</span>
  HANDLER plpgsql_call_handler
  VALIDATOR plpgsql_validator;</pre></div></div>



Nach dem erstellen unserer Test Tabelle und der Statistik Tabelle


<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> test
<span style="color: #66cc66;">&#40;</span>
  id serial <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  <span style="color: #993333; font-weight: bold;">CONSTRAINT</span> <span style="color: #ff0000;">&quot;pTest&quot;</span> <span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #66cc66;">&#40;</span>id<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> statistik
<span style="color: #66cc66;">&#40;</span>
  tag <span style="color: #993333; font-weight: bold;">DATE</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  test <span style="color: #993333; font-weight: bold;">INTEGER</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">DEFAULT</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">,</span>
  <span style="color: #993333; font-weight: bold;">CONSTRAINT</span> <span style="color: #ff0000;">&quot;pStatistik&quot;</span> <span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #66cc66;">&#40;</span>tag<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span></pre></div></div>



können wir unsere Triggerfunktion schreiben.


<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">OR</span> <span style="color: #993333; font-weight: bold;">REPLACE</span> <span style="color: #993333; font-weight: bold;">FUNCTION</span> count_test<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #993333; font-weight: bold;">RETURNS</span> <span style="color: #ff0000;">&quot;trigger&quot;</span> <span style="color: #993333; font-weight: bold;">AS</span>
$BODY$
   <span style="color: #993333; font-weight: bold;">DECLARE</span>
    <span style="color: #993333; font-weight: bold;">COUNT</span> <span style="color: #993333; font-weight: bold;">INTEGER</span>;
    right_now <span style="color: #993333; font-weight: bold;">DATE</span>;
   <span style="color: #993333; font-weight: bold;">BEGIN</span>
&nbsp;
	right_now :<span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'now'</span>;
&nbsp;
	<span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #993333; font-weight: bold;">INTO</span> <span style="color: #993333; font-weight: bold;">COUNT</span> test <span style="color: #993333; font-weight: bold;">FROM</span> statistik <span style="color: #993333; font-weight: bold;">WHERE</span> tag <span style="color: #66cc66;">=</span> right_now;
&nbsp;
        <span style="color: #993333; font-weight: bold;">IF</span> <span style="color: #993333; font-weight: bold;">COUNT</span> ISNULL <span style="color: #993333; font-weight: bold;">THEN</span>
		<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> statistik <span style="color: #66cc66;">&#40;</span>tag<span style="color: #66cc66;">,</span> test<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span>right_now<span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">1</span>  <span style="color: #66cc66;">&#41;</span>;
	<span style="color: #993333; font-weight: bold;">ELSE</span>
		<span style="color: #993333; font-weight: bold;">COUNT</span> :<span style="color: #66cc66;">=</span> <span style="color: #993333; font-weight: bold;">COUNT</span> <span style="color: #66cc66;">+</span> <span style="color: #cc66cc;">1</span>;
		<span style="color: #993333; font-weight: bold;">UPDATE</span> statistik <span style="color: #993333; font-weight: bold;">SET</span> test <span style="color: #66cc66;">=</span> <span style="color: #993333; font-weight: bold;">COUNT</span> <span style="color: #993333; font-weight: bold;">WHERE</span> tag <span style="color: #66cc66;">=</span> right_now;
        <span style="color: #993333; font-weight: bold;">END</span> <span style="color: #993333; font-weight: bold;">IF</span>;
&nbsp;
        <span style="color: #993333; font-weight: bold;">RETURN</span> <span style="color: #993333; font-weight: bold;">NEW</span>;
    <span style="color: #993333; font-weight: bold;">END</span>;
$BODY$
  <span style="color: #993333; font-weight: bold;">LANGUAGE</span> <span style="color: #ff0000;">'plpgsql'</span> VOLATILE;</pre></div></div>




Jetzt müssen wir die Triggerfunktion nur noch unserer Tabelle <strong>test</strong> zuweisen, und schon wird bei jedem Eintrag in die Tabelle <strong>test</strong> der Wert in der Tabelle <strong>statistik</strong> erhöht.


<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TRIGGER</span> insert_test
  AFTER <span style="color: #993333; font-weight: bold;">INSERT</span>
  <span style="color: #993333; font-weight: bold;">ON</span> test
  <span style="color: #993333; font-weight: bold;">FOR</span> EACH <span style="color: #993333; font-weight: bold;">ROW</span>
  <span style="color: #993333; font-weight: bold;">EXECUTE</span> <span style="color: #993333; font-weight: bold;">PROCEDURE</span> count_test<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>


<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://www.jgeppert.com/2008/07/erstellen-von-triggern-in-postgresql/' addthis:title='Erstellen von Triggern in PostgreSQL ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></content:encoded>
			<wfw:commentRss>http://www.jgeppert.com/2008/07/erstellen-von-triggern-in-postgresql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

