<?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>Blog de CinéPhil &#187; MySQL</title>
	<atom:link href="https://blog.developpez.com/cinephil/pcategory/bases-de-donnees/mysql/feed" rel="self" type="application/rss+xml" />
	<link>https://blog.developpez.com/cinephil</link>
	<description>Ce qui se conçoit bien s&#039;énonce clairement...</description>
	<lastBuildDate>Tue, 11 Dec 2012 15:58:32 +0000</lastBuildDate>
	<language>fr-FR</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.1.42</generator>
	<item>
		<title>Trigger pour incrémentation relative</title>
		<link>https://blog.developpez.com/cinephil/p10269/bases-de-donnees/trigger_pour_incrementation_relative</link>
		<comments>https://blog.developpez.com/cinephil/p10269/bases-de-donnees/trigger_pour_incrementation_relative#comments</comments>
		<pubDate>Thu, 08 Sep 2011 13:18:49 +0000</pubDate>
		<dc:creator><![CDATA[CinePhil]]></dc:creator>
				<category><![CDATA[Bases de données]]></category>
		<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[Lorsqu&#8217;on pratique l&#8217;identification relative, il est impossible d&#8217;utiliser l&#8217;AUTO_INCREMENT pour numéroter la seconde colonne de la clé primaire relativement à la première qui est une clé étrangère. Par exemple, une chaîne hôtelière pourra avoir, dans sa base de données, une &#8230; <a href="https://blog.developpez.com/cinephil/p10269/bases-de-donnees/trigger_pour_incrementation_relative">Lire la suite <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>Lorsqu&rsquo;on pratique l&rsquo;identification relative, il est impossible d&rsquo;utiliser l&rsquo;AUTO_INCREMENT pour numéroter la seconde colonne de la clé primaire relativement à la première qui est une clé étrangère. Par exemple, une chaîne hôtelière pourra avoir, dans sa base de données, une table &laquo;&nbsp;Hotel&nbsp;&raquo; et une table &laquo;&nbsp;Chambre&nbsp;&raquo; identifiée relativement à &laquo;&nbsp;Hotel&nbsp;&raquo;. C&rsquo;est à dire que pour chaque identifiant d&rsquo;hôtel dans la table &laquo;&nbsp;Chambre&nbsp;&raquo;, le numéro de la chambre repart de 1.</p>
<p>Je décris ci-après la numérotation automatique des chambres, tout en SQL avec MySQL mais sans doute facilement adaptable dans le langage procédural de n&rsquo;importe quel autre SGBD et pour d&rsquo;autres besoins tels que les lignes de commande, de facture ou de livraison ou (ce qui m&rsquo;a amené à étudier ce sujet) les numéros d&rsquo;exercice d&rsquo;un examen.<br />
<span id="more-6"></span></p>
<p><strong>1. Le modèle de données</strong><br />
MCD :<br />
Hotel -1,n&#8212;-Avoir&#8212;-(1,1)- Chambre</p>
<p><em>Note : les parenthèses autour des cardinalités 1,1 signifient que l&rsquo;entité type Chambre est identifiée relativement à Hotel.</em></p>
<p>Tables :<br />
<strong>Hotel</strong> (<u>htl_id</u>, htl_nom&#8230;)<br />
<strong>Chambre</strong> (<u><em>chb_id_hotel</em>, chb_numero</u>&#8230;)</p>
<p><em>Note : On voit ci-dessus que l&rsquo;identifiant de l&rsquo;hôtel participe à la clé primaire de la chambre.</em></p>
<div class="codecolorer-container sql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br /></div></td><td><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">-- Table Hotel </span><br />
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> Hotel &nbsp;<br />
<span style="color: #66cc66;">&#40;</span> <br />
&nbsp; htl_id <span style="color: #993333; font-weight: bold;">INT</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;">AUTO_INCREMENT</span> <span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #66cc66;">,</span> <br />
&nbsp; htl_nom <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span> <span style="color: #cc66cc;">50</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #66cc66;">,</span> <br />
&nbsp; <span style="color: #993333; font-weight: bold;">UNIQUE</span> <span style="color: #66cc66;">&#40;</span>htl_nom<span style="color: #66cc66;">&#41;</span> <br />
<span style="color: #66cc66;">&#41;</span> ENGINE <span style="color: #66cc66;">=</span> InnoDB &nbsp;<br />
<span style="color: #993333; font-weight: bold;">CHARACTER</span> <span style="color: #993333; font-weight: bold;">SET</span> utf8 <span style="color: #993333; font-weight: bold;">COLLATE</span> utf8_general_ci; <br />
&nbsp;<br />
<span style="color: #808080; font-style: italic;">-- Table Chambre </span><br />
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> Chambre &nbsp;<br />
<span style="color: #66cc66;">&#40;</span> <br />
&nbsp; chb_id_hotel <span style="color: #993333; font-weight: bold;">INT</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #66cc66;">,</span> <br />
&nbsp; chb_numero <span style="color: #993333; font-weight: bold;">INT</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #66cc66;">,</span> <br />
&nbsp; <span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #66cc66;">&#40;</span> chb_id_hotel <span style="color: #66cc66;">,</span> chb_numero <span style="color: #66cc66;">&#41;</span> <br />
<span style="color: #66cc66;">&#41;</span> ENGINE <span style="color: #66cc66;">=</span> InnoDB &nbsp; <br />
<span style="color: #993333; font-weight: bold;">CHARACTER</span> <span style="color: #993333; font-weight: bold;">SET</span> utf8 <span style="color: #993333; font-weight: bold;">COLLATE</span> utf8_general_ci; <br />
&nbsp;<br />
<span style="color: #993333; font-weight: bold;">ALTER</span> <span style="color: #993333; font-weight: bold;">TABLE</span> Chambre &nbsp;<br />
<span style="color: #993333; font-weight: bold;">ADD</span> <span style="color: #993333; font-weight: bold;">FOREIGN</span> <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #66cc66;">&#40;</span> chb_id_hotel <span style="color: #66cc66;">&#41;</span> &nbsp;<br />
&nbsp; <span style="color: #993333; font-weight: bold;">REFERENCES</span> Hotel <span style="color: #66cc66;">&#40;</span>htl_id<span style="color: #66cc66;">&#41;</span> &nbsp;<br />
&nbsp; <span style="color: #993333; font-weight: bold;">ON</span> <span style="color: #993333; font-weight: bold;">DELETE</span> CASCADE ;</div></td></tr></tbody></table></div>
<p><strong>2. Le trigger d&rsquo;incrémentation relative</strong><br />
Je pars du principe que le numéro de la chambre peut être saisi ou non par l&rsquo;utilisateur de l&rsquo;application qui va insérer les nouvelles chambres dans la table. Lorsque le numéro de chambre est laissé vide, c&rsquo;est le SGBD qui doit numéroter automatiquement la chambre en fonction de l&rsquo;identifiant de l&rsquo;hôtel concerné, c&rsquo;est à dire en affectant le plus haut numéro de chambre de cet hôtel déjà présente dans la table + 1 à la colonne chb_numero.</p>
<p>Commençons par insérer quelques hôtels puis quelques chambres avec leur numéro :</p>
<div class="codecolorer-container sql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br /></div></td><td><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> Hotel <span style="color: #66cc66;">&#40;</span>htl_nom<span style="color: #66cc66;">&#41;</span> <br />
<span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Hôtel des flots bleus'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Hôtel de la gare'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Hôtel du parc'</span><span style="color: #66cc66;">&#41;</span>; <br />
&nbsp;<br />
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> Chambre <span style="color: #66cc66;">&#40;</span>chb_id_hotel<span style="color: #66cc66;">,</span> chb_numero<span style="color: #66cc66;">&#41;</span> <br />
<span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;</div></td></tr></tbody></table></div>
<p>Nous avons les données suivantes dans la table Chambre :<br />
chb_id_hotel / chb_numero<br />
1 / 1<br />
1 / 2<br />
1 / 3<br />
2 / 1</p>
<p>Voici maintenant le trigger :</p>
<div class="codecolorer-container sql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br /></div></td><td><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">DELIMITER <span style="color: #66cc66;">//</span> <br />
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TRIGGER</span> tre_chambre_numero &nbsp;<br />
&nbsp; <span style="color: #993333; font-weight: bold;">BEFORE</span> <span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">ON</span> Chambre &nbsp;<br />
&nbsp; <span style="color: #993333; font-weight: bold;">FOR</span> EACH <span style="color: #993333; font-weight: bold;">ROW</span> <br />
<span style="color: #993333; font-weight: bold;">BEGIN</span> <br />
&nbsp; <span style="color: #993333; font-weight: bold;">DECLARE</span> num <span style="color: #993333; font-weight: bold;">INTEGER</span>; <br />
&nbsp; <span style="color: #993333; font-weight: bold;">IF</span> <span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">NEW</span><span style="color: #66cc66;">.</span>chb_numero <span style="color: #993333; font-weight: bold;">IS</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">OR</span> <span style="color: #993333; font-weight: bold;">NEW</span><span style="color: #66cc66;">.</span>chb_numero <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">THEN</span> <br />
&nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">SET</span> num <span style="color: #66cc66;">=</span> &nbsp;<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#40;</span> <br />
&nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #993333; font-weight: bold;">COALESCE</span><span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">MAX</span><span style="color: #66cc66;">&#40;</span>chb_numero<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">+</span> <span style="color: #cc66cc;">1</span> <br />
&nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">FROM</span> Chambre <br />
&nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">WHERE</span> chb_id_hotel <span style="color: #66cc66;">=</span> <span style="color: #993333; font-weight: bold;">NEW</span><span style="color: #66cc66;">.</span>chb_id_hotel <br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#41;</span>; <br />
&nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">SET</span> <span style="color: #993333; font-weight: bold;">NEW</span><span style="color: #66cc66;">.</span>chb_numero <span style="color: #66cc66;">=</span> num; <br />
&nbsp; <span style="color: #993333; font-weight: bold;">END</span> <span style="color: #993333; font-weight: bold;">IF</span>; <br />
<span style="color: #993333; font-weight: bold;">END</span><span style="color: #66cc66;">//</span></div></td></tr></tbody></table></div>
<p>Insérons maintenant quelques chambres aux 3 hôtels sans préciser le numéro :</p>
<div class="codecolorer-container sql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br /></div></td><td><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> Chambre <span style="color: #66cc66;">&#40;</span>chb_id_hotel<span style="color: #66cc66;">&#41;</span> <br />
<span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#41;</span></div></td></tr></tbody></table></div>
<p>Nous avons maintenant les données suivantes dans la table Chambre :<br />
chb_id_hotel / chb_numero<br />
1 / 1<br />
1 / 2<br />
1 / 3<br />
1 / 4 &#8212; Nouvelle chambre avec le bon numéro<br />
2 / 1<br />
2 / 2 &#8212; Nouvelle chambre avec le bon numéro<br />
3 / 1 &#8212; Nouvelle chambre avec le bon numéro<br />
3 / 2 &#8212; Nouvelle chambre avec le bon numéro</p>
]]></content:encoded>
			<wfw:commentRss></wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
	</channel>
</rss>
