<?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>GS Design &#187; mysql</title>
	<atom:link href="http://www.gsdesign.ro/blog/category/programing/mysql-programing/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.gsdesign.ro/blog</link>
	<description>Just another developer blog</description>
	<lastBuildDate>Thu, 06 Jan 2011 22:28:20 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Database design example for a configurable product eshop</title>
		<link>http://www.gsdesign.ro/blog/database-design-example-for-a-configurable-product-eshop/</link>
		<comments>http://www.gsdesign.ro/blog/database-design-example-for-a-configurable-product-eshop/#comments</comments>
		<pubDate>Thu, 06 Jan 2011 22:28:20 +0000</pubDate>
		<dc:creator>Gabi Solomon</dc:creator>
				<category><![CDATA[mysql]]></category>
		<category><![CDATA[Programing]]></category>
		<category><![CDATA[database design]]></category>
		<category><![CDATA[eshop]]></category>

		<guid isPermaLink="false">http://www.gsdesign.ro/blog/?p=449</guid>
		<description><![CDATA[I was building an e-shop and one of the requiremens was to have configurable products. The configurable parts will need to have different prices and stocks from the main product.  Also the shop must be able to move products from one category to another. So i started thinking of a database structure that will enable [...]]]></description>
			<content:encoded><![CDATA[<p>I was building an e-shop and one of the requiremens was to have configurable products.<br />
The configurable parts will need to have different prices and stocks from the main product.  Also the shop must be able to move products from one category to another.</p>
<p>So i started thinking of a database structure that will enable me to accomplish this. As i started to research i also posted a question on <a href="http://stackoverflow.com/q/2645958/65503">stackoverflow</a> to see what others had in mind.</p>
<p><span id="more-449"></span></p>
<h3>The design</h3>
<p>After a bit of thought i came up with this design.<br />
<a href="http://www.gsdesign.ro/blog/wp-content/uploads/2010/12/screenshot_001.jpeg"><img class="aligncenter size-full wp-image-450" title="screenshot_001" src="http://www.gsdesign.ro/blog/wp-content/uploads/2010/12/screenshot_001.jpeg" alt="" width="389" height="420" /></a></p>
<h3>Design Details</h3>
<p><strong>Products have an id_configuration</strong><br />
A configurable products actually is a collection of products witch have the same id_configuration (witch is auto-generated).</p>
<p><strong>Features linked to products</strong><br />
Rather then categories, which seems more naturally to me. This enables me to easily move products between categories.</p>
<p><strong>Product features values is stored in the link table</strong><br />
I decided to store the feature value for a product in the link table. This helps with more simple queries and also it means that you don&#8217;t have to insert all the options in the feature options table (think of features that have different values for all products)</p>
<p><strong>Name and Label for features</strong><br />
The name is used internally in the admin section and Label is used in the site front-end. This is useful for features with same name. Ex: Size for clothes, size for hardisks etc.</p>
<p><strong> Filterable flag on features </strong><br />
A Yes/No flag that shows witch filters are filtrable.</p>
<h3>Examples of use</h3>
<p><strong>Selecting products</strong><br />
The most important thing is how do we select products, and how do we only show one product for the configurable ones. For this we use GROUP BY.<br />
<code class="prettyprint"><br />
SELECT * FROM `products`<br />
GROUP BY CASE `products`.`configurable` WHEN 'yes' THEN `products`.`id_configuration` ELSE `products`.`id` END<br />
</code></p>
<p><strong>Filtering products</strong><br />
The bellow example shows the query for a single filter/feature (with id 38) and 2 values selected:<br />
<code class="prettyprint"><br />
SELECT * FROM `products`<br />
WHERE `id` IN (SELECT id_product FROM `product-features` WHERE `id_feature` = 38 AND `value` IN ('Test value', 'New Value') GROUP BY id_product HAVING COUNT(*) >= 1)<br />
GROUP BY CASE `products`.`configurable` WHEN 'yes' THEN `products`.`id_configuration` ELSE `products`.`id` END<br />
</code></p>
<p><strong>Get active filters</strong><br />
After you have a list of filter products you will want to print out a list of filters that are applicable to this list, since not all are available to the current selected products.<br />
<code class="prettyprint"><br />
SELECT `id_feature` FROM `product-features`<br />
WHERE `id_product` IN (SELECT `id` FROM `products` WHERE ___YOUR_CONDITIONS____) GROUP BY `id_feature`<br />
</code><br />
<em>Notice that i left out the GROUP BY from the product subselect.</em></p>
<p><strong>Associate count of products for a filter</strong><br />
Its very useful to show the count of products for a certain value of a filter. The bellow query is how you can select that count for one filter.<br />
<code class="prettyprint"><br />
SELECT *, COUNT(*) AS `count` FROM (<br />
    SELECT *<br />
    FROM `product-features`<br />
    WHERE<br />
        `id_product` IN (SELECT `id` FROM `products` WHERE ___YOUR_CONDITIONS____) AND<br />
        `id_feature` = 36<br />
) AS `tbl` GROUP BY `value`<br />
</code><br />
<em>Notice that i left out the GROUP BY from the product subselect.</em></p>
<h3>Conclusions</h3>
<p>I think this database model works best for what i wanted and it has all the flexibility i needed:<br />
- it allows for any 2 products to group as configurable<br />
- you can add promotions and separate images for each variation<br />
- have separate stocks on each variation (witch make more sense to me)<br />
- get sales report cumulate or separate on each variation.</p>
<p>Hope you like this approach and i await you comments on it.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.gsdesign.ro/blog/database-design-example-for-a-configurable-product-eshop/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>mysql error : Incorrect information in file: ./table.frm</title>
		<link>http://www.gsdesign.ro/blog/mysql-error-incorrect-information-in-file-tablefrm/</link>
		<comments>http://www.gsdesign.ro/blog/mysql-error-incorrect-information-in-file-tablefrm/#comments</comments>
		<pubDate>Tue, 10 Mar 2009 22:24:14 +0000</pubDate>
		<dc:creator>Gabi Solomon</dc:creator>
				<category><![CDATA[mysql]]></category>
		<category><![CDATA[Programing]]></category>
		<category><![CDATA[error]]></category>

		<guid isPermaLink="false">http://www.gsdesign.ro/blog/?p=348</guid>
		<description><![CDATA[After tinkering with my VPS settings to optimize it ( it was having some low memory problems ) and after restarting the server, just when i tought everything was ok i noticed that i managed to break some websites. they all got this error: mysql error : Incorrect information in file: ./table.frm After a bit [...]]]></description>
			<content:encoded><![CDATA[<p>After tinkering with my VPS settings to optimize it ( it was having some low memory problems ) and after restarting the server, just when i tought everything was ok i noticed that i managed to break some websites. </p>
<p>they all got this error:</p>
<blockquote><p>mysql error : Incorrect information in file: ./table.frm</p></blockquote>
<p>After a bit of googling i found the problem &#8230; apparently it could be because there is no support for InnoDB &#8230; oops. In my quest for optimization i disabled the support for InnoDB tables thinking that there werent any InnoDB tables in the websites hosted on that VPS .. apparently they were <img src='http://www.gsdesign.ro/blog/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' /> .</p>
<p>Hope you get here faster then i found the solution.<br />
Cheers</p>
]]></content:encoded>
			<wfw:commentRss>http://www.gsdesign.ro/blog/mysql-error-incorrect-information-in-file-tablefrm/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Multilanguage database design approach</title>
		<link>http://www.gsdesign.ro/blog/multilanguage-database-design-approach/</link>
		<comments>http://www.gsdesign.ro/blog/multilanguage-database-design-approach/#comments</comments>
		<pubDate>Fri, 26 Dec 2008 20:57:23 +0000</pubDate>
		<dc:creator>Gabi Solomon</dc:creator>
				<category><![CDATA[mysql]]></category>
		<category><![CDATA[Programing]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database design]]></category>
		<category><![CDATA[internationalization]]></category>
		<category><![CDATA[normalize]]></category>

		<guid isPermaLink="false">http://www.gsdesign.ro/blog/?p=304</guid>
		<description><![CDATA[Preinfo Before you go right to the comment section and recommend gettext or other similar ways, know that i am talking about content that is manageable from an admin panel or is added by the user Also this article is based on personal experience and is not necessary the best way to do this. Building [...]]]></description>
			<content:encoded><![CDATA[<h3>Preinfo</h3>
<p><em>Before you go right to the comment section and recommend gettext or other similar ways, know that i am talking about content that is manageable from an admin panel or is added by the user</em></p>
<p><em>Also this article is based on personal experience and is not necessary the best way to do this.</em></p>
<p>Building a metalanguage website poses a lot of problems, and one of them is how you store the content in the database for each language.</p>
<p>If you do a google search you will find little resources about it, and most of the are on forums. This seem a bit strange to me, so after i had decided on a database schema for a metalanguage website i decided to post it here in the hope that other people might find it useful and save them some googling time.</p>
<p>As far as i searched there are more or less 4 databases schemas for metalanguage website.</p>
<h3>1. Column approach</h3>
<p>This approach is very common and basically it duplicates the column of content for each language.</p>
<blockquote><p>table <strong>pages</strong><br />
-- id (int)<br />
-- title_en (varchar)<br />
-- title_es (varchar)<br />
-- content_en (varchar)<br />
-- content_es (varchar)</p></blockquote>
<p>The way you would query it is by automatically selecting the right columns according to the language chosen:</p>
<div class="igBar"><span id="lsql-4"><a href="#" onclick="javascript:showCodeTxt('sql-4'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">SQL:</span>
<div id="sql-4">
<div class="sql">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:IG_LINE_COLOUR_1;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #ff0000;">`id`</span>, <span style="color: #ff0000;">`title_en`</span> <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #ff0000;">`title`</span>, <span style="color: #ff0000;">`content_en`</span> <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #ff0000;">`content`</span> <span style="color: #993333; font-weight: bold;">FROM</span> <span style="color: #ff0000;">`pages`</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p>
Or you could select all and do the column selection from php :</p>
<div class="igBar"><span id="lphp-5"><a href="#" onclick="javascript:showCodeTxt('php-5'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-5">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:IG_LINE_COLOUR_1;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://www.php.net/echo"><span style="color:#000066;">echo</span></a> <span style="color:#0000FF;">$rowPage</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#FF0000;">'title'</span> . <span style="color:#0000FF;">$_SESSION</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#FF0000;">'currentLanguage'</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#93;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<h4>Advantages</h4>
<ul>
<li>It doesn't have duplicate content, since there is only one row for each record, and only the language columns are duplicated</li>
<li>Easy to implement</li>
</ul>
<h4>Disadvantages</h4>
<ul>
<li>You need to build the watch what column you are working with depending on the language</li>
<li>Hard to maintain. Although this is a easy way for 2-3 languages its becomes a real drag when you have a lot of columns or a lot of languages</li>
<li>Hard to add a new language</li>
</ul>
<h3>2. Multirow approach</h3>
<p>Another approach that i saw but i have never worked with it. It is simillar to the one above but instead of duplicating the content in columns it does it in rows.</p>
<blockquote><p>table <strong>pages</strong><br />
-- id (int)<br />
-- language_id (int)<br />
-- title (varchar)<br />
-- content (varchar)
</p></blockquote>
<p>So you will basically have 3 rows for the same page if you have 3 languages. The main problem i see with this approach is that it would be a bit tricky to know witch id you will use for the table relations.</p>
<p>Sorry but since i dont really have experience with this i cant show you sql &#038; php examples.</p>
<h4>Advantages</h4>
<ul>
<li>Ease in adding a new language</li>
</ul>
<h4>Disadvantages</h4>
<ul>
<li>Need to watch the table relations</li>
<li>A lot of duplicate content. You will have duplicate content for all the columns that are not translated</li>
</ul>
<h3>3. Single Translation table approach</h3>
<p>This is an approach that becomes a little more complex then the other 2, but it is more suited for dinamic websites and which have a large number of languages or which intend to add a new language in the future and want to do it with ease.  </p>
<blockquote><p>
table <strong>languages</strong><br />
-- id (int)<br />
-- name (varchar)</p>
<p>table <strong>pages</strong><br />
-- id (int)<br />
-- language_id (int)<br />
-- title (int fk)<br />
-- content (int fk)</p>
<p>table <strong>translation</strong><br />
-- id (int)</p>
<p>table <strong>translation_entry</strong><br />
-- translation_id (int)<br />
-- language_id (int)<br />
-- content (text)
</p></blockquote>
<p>In this approach you would store the id from the translation table in the title and content columns from the pages table, and then do a join with the translation_entry table based on the language id.</p>
<h4>Advantages</h4>
<ul>
<li>Proper normalization</li>
<li>Ease in adding a new language</li>
</ul>
<h4>Disadvantages</h4>
<ul>
<li>Longer joins and query to get the content</li>
<li>All the translated content goes into one table</li>
<li>For me it just looks hard to work with and maintain</li>
</ul>
<h3>4. Coupled Translation table approach [my aproach <img src='http://www.gsdesign.ro/blog/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> ]</h3>
<p>This is a variation of the above approach that to me seems easier to maintain and work with.<br />
Instead of having just one translation table, you have one for each table. and you move the columns from the pages that need to be translated to the translation table.</p>
<blockquote><p>
table <strong>languages</strong><br />
-- id (int)<br />
-- name (varchar)</p>
<p>table <strong>pages</strong><br />
-- id (int)</p>
<p>table <strong>pages_translation</strong><br />
-- id (int)<br />
-- page_id (int)<br />
-- language_id (int)<br />
-- title (text)<br />
-- content (text)
</p></blockquote>
<p>To get your data you just do a simple join:</p>
<div class="igBar"><span id="lsql-6"><a href="#" onclick="javascript:showCodeTxt('sql-6'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">SQL:</span>
<div id="sql-6">
<div class="sql">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:IG_LINE_COLOUR_1;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #993333; font-weight: bold;">SELECT</span> * <span style="color: #993333; font-weight: bold;">FROM</span> <span style="color: #ff0000;">`pages`</span> <span style="color: #993333; font-weight: bold;">JOIN</span> <span style="color: #ff0000;">`pages_translation`</span> <span style="color: #993333; font-weight: bold;">ON</span> <span style="color: #ff0000;">`pages`</span>.<span style="color: #ff0000;">`id`</span> = <span style="color: #ff0000;">`pages_translation`</span>.<span style="color: #ff0000;">`page_id`</span> <span style="color: #993333; font-weight: bold;">WHERE</span> <span style="color: #ff0000;">`map_landmarks_translation`</span>.<span style="color: #ff0000;">`language_id`</span>=<span style="color: #ff0000;">'1'</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<h4>Advantages</h4>
<ul>
<li>Proper normalization</li>
<li>Ease in adding a new language</li>
<li>Easy to query</li>
<li>Columns keep there names</li>
</ul>
<h4>Disadvantages</h4>
<ul>
<li>You have to create translation tables for all your tables that have columns that need to be translated</li>
</ul>
<h2>Conclusion</h2>
<p>I am sure that there are other methods of doing a multilingual website, this are just the ones that i thought are most commonly used. My solution is the best, its just the best for me, because it works for my project and its easier for me to work with compared to other approaches.<br />
In the end the best approach is the one that is the best  for you. The one that you find the most easier to work with and maintain.</p>
<p>Cheers,<br />
and good coding.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.gsdesign.ro/blog/multilanguage-database-design-approach/feed/</wfw:commentRss>
		<slash:comments>29</slash:comments>
		</item>
		<item>
		<title>MYSQL: make a combination of 2 columns unique</title>
		<link>http://www.gsdesign.ro/blog/mysql-make-a-combination-of-2-columns-unique/</link>
		<comments>http://www.gsdesign.ro/blog/mysql-make-a-combination-of-2-columns-unique/#comments</comments>
		<pubDate>Tue, 19 Aug 2008 12:55:50 +0000</pubDate>
		<dc:creator>Gabi Solomon</dc:creator>
				<category><![CDATA[mysql]]></category>
		<category><![CDATA[Programing]]></category>
		<category><![CDATA[column]]></category>
		<category><![CDATA[combination]]></category>
		<category><![CDATA[unique]]></category>

		<guid isPermaLink="false">http://www.gsdesign.ro/blog/?p=154</guid>
		<description><![CDATA[Here is a recently situation i came across, what if you have a table and you want to make a combination of 2 columns unique. The columns Individually are not unique, but combination is. To alter 1 column to a unique key you would use : PLAIN TEXT SQL: ALTER TABLE user MODIFY COLUMN id [...]]]></description>
			<content:encoded><![CDATA[<p>Here is a recently situation i came across, what if you have a table and you want to make a combination of 2 columns unique. The columns Individually are not unique, but combination is. </p>
<p>To alter 1 column to a unique key you would use :</p>
<div class="igBar"><span id="lsql-9"><a href="#" onclick="javascript:showCodeTxt('sql-9'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">SQL:</span>
<div id="sql-9">
<div class="sql">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:IG_LINE_COLOUR_1;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #993333; font-weight: bold;">ALTER</span> <span style="color: #993333; font-weight: bold;">TABLE</span> user <span style="color: #993333; font-weight: bold;">MODIFY</span> <span style="color: #993333; font-weight: bold;">COLUMN</span> id INT <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;">UNIQUE</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Well for a combination of 2 or more columns to be unique you would use:</p>
<div class="igBar"><span id="lsql-10"><a href="#" onclick="javascript:showCodeTxt('sql-10'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">SQL:</span>
<div id="sql-10">
<div class="sql">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:IG_LINE_COLOUR_1;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #993333; font-weight: bold;">ALTER</span> <span style="color: #993333; font-weight: bold;">TABLE</span> user <span style="color: #993333; font-weight: bold;">ADD</span> <span style="color: #993333; font-weight: bold;">UNIQUE</span> <span style="color:#006600; font-weight:bold;">&#40;</span>id,domain<span style="color:#006600; font-weight:bold;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Hope this helps you.<br />
Cheers</p>
]]></content:encoded>
			<wfw:commentRss>http://www.gsdesign.ro/blog/mysql-make-a-combination-of-2-columns-unique/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

