Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Having multiple tables is really not necessary, unless you plan on having millions of stories... I usually go along with two tables; one for the <strong>item</strong> and another for the <strong>localized item</strong></p> <p>For example, a story would be like</p> <p><strong>table "story"</strong></p> <pre><code>id INTEGER (Primary Key) creation_date DATE_TIME author VAR_CHAR(32) ..other general columns.. </code></pre> <p><strong>table "story_localized"</strong></p> <pre><code>story_id INTEGER (Foreign Key of story.id) \ lang CHAR(2) -- (Indexed, unique -- story_id+lang) content TEXT ..other localized columns.. </code></pre> <p>Performing the query is simply a matter of <code>JOIN</code>ing the two tables :</p> <pre><code>SELECT s.*, sl.* FROM story s JOIN story_localized sl ON s.id = sl.story_id WHERE s.id = 1 -- the story you're looking for here AND sl.lang = 'en' -- your language here -- other conditions here </code></pre> <p>This configuration gives a few advantages :</p> <ul> <li>all your data is in the same table, no need to synchronizing CRUD operations</li> <li>you can add new languages to any story without the need to create yet more tables</li> <li>etc.</li> </ul> <p>** <strong>EDIT</strong> **</p> <p>Just as a bonus, here is a "trick" to retrieve a story easily, regardless of the language it's been written into</p> <pre><code>SELECT * FROM (SELECT * FROM story s JOIN story_localized sl ON s.id = sl.story_id WHERE s.id = {storyId} AND sl.lang = {desiredLanguage} -- example : 'es' UNION SELECT * FROM story s JOIN story_localized sl ON s.id = sl.story_id WHERE s.id = {storyId} AND sl.lang = {defaultLanguage} -- example : 'en' UNION SELECT * FROM story s JOIN story_localized sl ON s.id = sl.story_id WHERE s.id = {storyId} LIMIT 1 -- ..get the first language found ) story_l LIMIT 1 -- only get the first SELECTed row found </code></pre> <p>Will try to fetch the story in the <code>{desiredLanguage}</code>, if the story is not available in that language, try to find it in <code>{defaultLanguage}</code> (ie. the site's default language), and if still nothing found, it doesn't matter which language to fetch the story, then, so fetch the first one found. All in one query, and all you need are 3 arguments: the story id, the desired language, and a default fallback language.</p> <p>Also, you can easily find out in what language the story is available into with a simple query :</p> <pre><code>SELECT sl.lang FROM story_localized sl WHERE sl.story_id = {storyId} </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload