<?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>Webinside - Blog d'Hervé Delvaux &#187; Tuto MySQL</title>
	<atom:link href="http://www.webinside.be/category/tutorial/mysql-tutorial/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.webinside.be</link>
	<description></description>
	<lastBuildDate>Fri, 03 Sep 2010 08:52:35 +0000</lastBuildDate>
	<language>fr</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Les vues (view) en MySQL 5</title>
		<link>http://www.webinside.be/les-vues-en-mysql-5/</link>
		<comments>http://www.webinside.be/les-vues-en-mysql-5/#comments</comments>
		<pubDate>Sun, 30 Nov 2008 21:12:15 +0000</pubDate>
		<dc:creator>Hervé</dc:creator>
				<category><![CDATA[Php / mySql]]></category>
		<category><![CDATA[Sceencast]]></category>
		<category><![CDATA[Tuto MySQL]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Screencast]]></category>
		<category><![CDATA[vue]]></category>

		<guid isPermaLink="false">http://www.webinside.be/?p=231</guid>
		<description><![CDATA[Les vues sont implémentées en version 5 de MySQL, elles sont disponibles depuis la version 5.0.1. Qu&#8217;est ce qu&#8217;une vue Les vues sont des tables virtuelles créées à l&#8217;aide d&#8217;une requête SELECT. Elles ne stockent pas les données qu&#8217;elles contiennent. Seule la requête permettant de les créer est conservée. Des vues pourquoi faire ? Une [...]]]></description>
			<content:encoded><![CDATA[<p>Les vues sont implémentées en <a href="http://dev.mysql.com/doc/refman/5.0/fr/views.html">version 5 de MySQL</a>, elles sont disponibles depuis la version 5.0.1.</p>
<p><strong>Qu&#8217;est ce qu&#8217;une vue</strong></p>
<p>Les vues sont des tables virtuelles créées à l&#8217;aide d&#8217;une requête SELECT.<br />
Elles ne stockent pas les données qu&#8217;elles contiennent. Seule la requête permettant de les créer est conservée.</p>
<div style="text-align:center;"><object width="480" height="405"><param name="movie" value="http://www.dailymotion.com/swf/kGgxoTrZBo5p2SRmGg&#038;related=1&#038;canvas=medium"></param><param name="allowFullScreen" value="true"></param><param name="allowScriptAccess" value="always"></param><embed src="http://www.dailymotion.com/swf/kGgxoTrZBo5p2SRmGg&#038;related=1&#038;canvas=medium" type="application/x-shockwave-flash" width="480" height="405" allowFullScreen="true" allowScriptAccess="always"></embed></object></div>
<p><span id="more-231"></span></p>
<p><strong>Des vues pourquoi faire ?</strong></p>
<p>Une vue peut donc être, une jointure entre différentes tables, l&#8217;extraction de certaines colonnes,&#8230;</p>
<p>Les vues peuvent être utilisées pour différentes raisons :</p>
<ul>
<li> Restreindre l&#8217;accès à certaine valeur en personnalisant l&#8217;affichage des informations suivant le type d&#8217;utilisateur.</li>
<li>Masquer la complexité d&#8217;un schéma.</li>
<li>Permettre de manipuler des valeurs calculées à partir d&#8217;autres valeurs du schéma.</li>
<li>Conserver la structure d&#8217;une table si elle doit être modifiée. pour ne pas devoir changer les requêtes du côté applicatif.</li>
</ul>
<p><strong>Syntaxe</strong></p>
<p><strong>Création d&#8217;une vue</strong> :</p>

<div class="wp_codebox"><table><tr id="p2315"><td class="line_numbers"><pre>1
2
</pre></td><td class="code" id="p231code5"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #66cc66;">&#91;</span><span style="color: #993333; font-weight: bold;">OR</span> <span style="color: #993333; font-weight: bold;">REPLACE</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#91;</span>ALGORITHM <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#123;</span>MERGE <span style="color: #66cc66;">|</span> TEMPTABLE<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#93;</span> <span style="color: #993333; font-weight: bold;">VIEW</span> view_name
<span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#40;</span>column_list<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span> <span style="color: #993333; font-weight: bold;">AS</span> select_statement <span style="color: #66cc66;">&#91;</span><span style="color: #993333; font-weight: bold;">WITH</span> <span style="color: #66cc66;">&#91;</span>CASCADED <span style="color: #66cc66;">|</span> <span style="color: #993333; font-weight: bold;">LOCAL</span><span style="color: #66cc66;">&#93;</span> <span style="color: #993333; font-weight: bold;">CHECK</span> <span style="color: #993333; font-weight: bold;">OPTION</span><span style="color: #66cc66;">&#93;</span></pre></td></tr></table></div>

<p>Cette commande crée une nouvelle vue, ou remplace une vue existante si la clause <em>OR REPLACE</em> est fournie. La clause <em>select_statement</em> est une commande SELECT qui fournit la définition de la vue. La liste optionnelle de colonnes peut être fournie pour définir explicitement les noms des colonnes.</p>
<p><strong>Modification d&#8217;une vue</strong> :</p>

<div class="wp_codebox"><table><tr id="p2316"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p231code6"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">ALTER</span> <span style="color: #993333; font-weight: bold;">VIEW</span> view_name <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#40;</span>column_list<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span> <span style="color: #993333; font-weight: bold;">AS</span> select_statement</pre></td></tr></table></div>

<p>Cette commande modifie la définition d&#8217;une vue.<br />
<em>select_statement</em> est le même que pour CREATE VIEW.</p>
<p><strong>Supprimer une vue</strong> :</p>

<div class="wp_codebox"><table><tr id="p2317"><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code" id="p231code7"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">DROP</span> <span style="color: #993333; font-weight: bold;">VIEW</span> <span style="color: #66cc66;">&#91;</span><span style="color: #993333; font-weight: bold;">IF</span> <span style="color: #993333; font-weight: bold;">EXISTS</span><span style="color: #66cc66;">&#93;</span>
    view_name <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">,</span> view_name<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">...</span>
    <span style="color: #66cc66;">&#91;</span>RESTRICT <span style="color: #66cc66;">|</span> CASCADE<span style="color: #66cc66;">&#93;</span></pre></td></tr></table></div>

<p><em>DROP VIEW</em> supprime une ou plusieurs vues. Vous devez avoir les droits de <em>DROP</em> pour chaque vue.</p>
<p><strong>Voir la définition de la vue</strong> :</p>

<div class="wp_codebox"><table><tr id="p2318"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p231code8"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SHOW</span> <span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">VIEW</span> view_name</pre></td></tr></table></div>

<p>Cette commande montre la commande <em>CREATE VIEW</em> qui créera la vue spécifiée.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.webinside.be/les-vues-en-mysql-5/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Transactions dans SGBD à l&#8217;aide de Zend Framework</title>
		<link>http://www.webinside.be/transactions-dans-sgbd-a-laide-de-zend-framework/</link>
		<comments>http://www.webinside.be/transactions-dans-sgbd-a-laide-de-zend-framework/#comments</comments>
		<pubDate>Fri, 11 Jul 2008 15:22:34 +0000</pubDate>
		<dc:creator>Hervé</dc:creator>
				<category><![CDATA[Tuto MySQL]]></category>
		<category><![CDATA[Tuto Php]]></category>
		<category><![CDATA[Tuto Zend Framework]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[php5]]></category>
		<category><![CDATA[transaction]]></category>
		<category><![CDATA[Zend Framework]]></category>

		<guid isPermaLink="false">http://www.webinside.be/?p=153</guid>
		<description><![CDATA[Ce petit tutorial qui concerne les transactions à l&#8217;aide de Zend Framework est basé sur les versions suivante : MySQL 5 Php 5 Zend Framework 1.5.2 Les transactions dans une base de données Les bases de données définissent les transactions comme étant des unités logiques de travail qui peuvent êtres validées (commited) ou annulées (rolled [...]]]></description>
			<content:encoded><![CDATA[<p>Ce petit tutorial qui concerne les transactions à l&#8217;aide de Zend Framework est basé sur les versions suivante :</p>
<ul>
<li><a href="http://www.mysql.fr/">MySQL 5</a></li>
<li><a href="http://www.php.net/">Php 5</a></li>
<li><a href="http://framework.zend.com/manual/fr">Zend Framework 1.5.2</a></li>
</ul>
<p><strong>Les transactions dans une base de données</strong></p>
<blockquote><p>Les bases de données définissent les transactions comme étant des unités logiques de travail qui peuvent êtres validées (commited) ou annulées (rolled back) en tant qu&#8217;une seule opération, même sur de multiples tables. Toutes les requêtes aux bases de données sont considérées comme faisant partie d&#8217;une transaction, même si le driver de base de données fait ceci implicitement. Ceci s&#8217;appelle le mode auto-commit, dans lequel le driver de base de données créer une transaction pour chaque requête executée et la valide. Par défaut toutes les classes Zend_Db_Adapter fonctionnent en mode auto-commit.</p></blockquote>
<p>Source : <a href="http://framework.zend.com/manual/fr/zend.db.html#zend.db.adapter.transactions">Zend Framework</a></p>
<p>Vous l&#8217;aurez compris, les transactions représentes un groupe de requêtes qui pourra être validé si tout se passe bien ou dans la cas contraire annulé.</p>
<p><span id="more-153"></span></p>
<p>Pour diverses raisons, il peut être utile d&#8217;effectuer une transaction vers une base de données.<br />
Il va de soit que pour utiliser les transactions, votre SGBD et votre moteur de stockage doivent pouvoir les gérer. Ce qui est le cas d&#8217;INNODB pour MySQL mais pas <del datetime="2008-07-11T14:09:42+00:00">encore</del> pour MyISAM.</p>
<p>Donc si votre moteur de stockage est MyISAM, vous devrez convertir les tables qui seront concernées par vos futurs transactions.</p>
<p><strong>Changement du moteur de stockage</strong><br />
On peut changer dynamiquement le moteur de stockage à l&#8217;aide de la commande ALTER TABLE.</p>
<p>Pour passer le moteur de stockage de la table leNomDeMaTable en INNODB nous effectuerons la commande sql suivante :</p>

<div class="wp_codebox"><table><tr id="p15313"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p153code13"><pre class="sql" style="font-family:monospace;">    <span style="color: #993333; font-weight: bold;">ALTER</span> <span style="color: #993333; font-weight: bold;">TABLE</span> leNomDeMaTable ENGINE<span style="color: #66cc66;">=</span>INNODB;</pre></td></tr></table></div>

<p>Pour le passer en MyISAM :</p>

<div class="wp_codebox"><table><tr id="p15314"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p153code14"><pre class="sql" style="font-family:monospace;">    <span style="color: #993333; font-weight: bold;">ALTER</span> <span style="color: #993333; font-weight: bold;">TABLE</span> maTable2 ENGINE<span style="color: #66cc66;">=</span>MYISAM;</pre></td></tr></table></div>

<p><strong>Transactions à l&#8217;aide de Zend Framework</strong><br />
Maitenant que notre SGBD gère les transactions, il va falloir spécifier manuellement le démarrage de la transaction à l&#8217;aide de <em>beginTransaction()</em>. Toutes les requêtes qui suivront feront parties de celle-ci tant que la transaction ne sera pas annulée avec <em>rollBack()</em> ou validée par un <em>commit()</em>.</p>
<p>Le canevas de base sera donc :</p>

<div class="wp_codebox"><table><tr id="p15315"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
</pre></td><td class="code" id="p153code15"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">beginTransaction</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
try <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// Essaye d'exécuter une ou plusieurs requêtes :</span>
    <span style="color: #000088;">$db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">query</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">...</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">query</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">...</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">query</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">...</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// Si toutes ont réussi, valide les changements en une seule passe.</span>
    <span style="color: #000088;">$db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">commit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
catch <span style="color: #009900;">&#40;</span>Exception <span style="color: #000088;">$e</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// Si une des requête s'est mal déroulée, alors nous voulons annuler les changements</span>
    <span style="color: #666666; font-style: italic;">// de toutes les requêtes faisant partie de la transaction, même celles qui se sont</span>
    <span style="color: #666666; font-style: italic;">// bien déroulées. Tous les changements sont annulés d'un seul coup</span>
    <span style="color: #000088;">$db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">rollBack</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$e</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getMessage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p><strong>Transactions dans en MVC avec Zend Framework</strong><br />
Imaginons que nous avons des produits divers. Ceux-ci sont enregistrer dans une la base de données.<br />
Les informations communes à tous les produits sont enregistrées dans la table Produit et toutes les données spécifiques à chaque type de produit dans la table correspondant au type : ProduitNomDuType. </p>
<p>Nous aurons donc les modèles Produit qui est une classe qui héritera de Zend_Db_Table_Abstract et Produit_Row qui héritera de Zend_Db_Table_Row_Abstract (nous aurons la même chose pour gérer les tables et ligne des ProduitNomDuType).</p>
<p>Pour ajouter un produit du type Foo nous allons donc devoir remplir la table &laquo;&nbsp;générique&nbsp;&raquo; Produit et ProduitFoo.<br />
Nous allons donc créer une transaction qui effectuera l&#8217;insertion des données dans les 2 tables.</p>
<p>Ce qui pourrait nous donner dans l&#8217;action ajouterAction() de notre controller quelque chose comme :</p>

<div class="wp_codebox"><table><tr id="p15316"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
</pre></td><td class="code" id="p153code16"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">// ...</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// On récupère l'adaptater que nous avons sauvé dans le registry dans le boostrap</span>
<span style="color: #000088;">$db</span> <span style="color: #339933;">=</span> Zend_Registry<span style="color: #339933;">::</span><span style="color: #004000;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'db'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	
&nbsp;
<span style="color: #666666; font-style: italic;">// On démarre la transaction</span>
<span style="color: #000088;">$db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">beginTransaction</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
try <span style="color: #009900;">&#123;</span>
	Zend_Loader<span style="color: #339933;">::</span><span style="color: #004000;">loadClass</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Produit'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	Zend_Loader<span style="color: #339933;">::</span><span style="color: #004000;">loadClass</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'ProduitFoo'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$produitDb</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Produit<span style="color: #009900;">&#40;</span><span style="color: #000088;">$db</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$produitDb</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">insert</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$dataProduit</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">// $dataProduit est un tableau qui représente les données générique du produit</span>
&nbsp;
	<span style="color: #000088;">$produitFooDb</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ProduitFoo<span style="color: #009900;">&#40;</span><span style="color: #000088;">$db</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$produitFooDb</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">insert</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$dataProduitFoo</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">// $dataProduitFoo est un tableau qui représente les données spécifique du produit foo</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// On valide l'ensemble des requêtes</span>
	<span style="color: #000088;">$db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">commit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
catch <span style="color: #009900;">&#40;</span>Exception <span style="color: #000088;">$e</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #666666; font-style: italic;">// Si une exception a été levée, on annule toutes les opérations du lot</span>
	<span style="color: #000088;">$db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">rollBack</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">//...</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Cette transaction permettra par exemple de ne pas se retrouver avec juste les infos génériques du produit si l&#8217;ajout des infos dans ProduitFoo a levé une exception.</p>
<p><strong>Quelques liens utiles</strong></p>
<ul>
<li><a href="http://framework.zend.com/manual/fr/">Manuel de Zend Framework</a> : <a href="http://framework.zend.com/manual/fr/zend.db.html">Zend_Db</a></li>
<li><a href="http://dev.mysql.com/doc/refman/5.0/fr/index.html">Manuel de MySql 5</a></li>
<li><a href="http://www.developpez.com/">Developpez.com</a> : <a href="http://mysql.developpez.com/">MySQL</a> et <a href="http://zend-framework.developpez.com/">Zend Framework</a></li>
<li><a href="http://www.z-f.fr/">Communauté Francophone de Zend Framework</a></li>
</ul>
<p>N&#8217;hésitez pas à poser vos questions ou remarques dans les commentaires, je me ferais un plaisir d&#8217;y répondre.</p>
<p><em>Note: je ne me suis pas étendu sur le fonctionnement du MVC avec Zend Framework vu qu&#8217;il y a énormément de chose à dire et que cela fera partie d&#8217;un futur tutorial.</em></p>
]]></content:encoded>
			<wfw:commentRss>http://www.webinside.be/transactions-dans-sgbd-a-laide-de-zend-framework/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
