Note that there are some explanatory texts on larger screens.

plurals
  1. POWhich columns should I add to a PRIMARY KEY
    primarykey
    data
    text
    <p>I want to create a table and avoid duplicated entries, by creating a PRIMARY KEY. The problem is I don't know which columns I should add to this KEY. Consider the next table:</p> <pre><code>CREATE TABLE `customers` ( `id_c` int(11) unsigned NOT NULL, `lang` tinyint(2) unsigned NOT NULL, `name` varchar(80) collate utf8_unicode_ci NOT NULL, `franchise` int(11) unsigned NOT NULL, KEY `id_c` (`id_c`), KEY `lang` (`lang`), KEY `franchise` (`franchise`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; </code></pre> <p><strong>id_c</strong>: Id of customer. It can be an enterprise. Suppose <em>McDonald's</em><br> <strong>lang</strong>: Contact language.<br> <strong>boss</strong>: Boss' name<br> <strong>franchise</strong>: If not zero, it is a franchise. McDonald's in Rome, Paris, London...<br><br> As you can see, each <strong>ENTERPRISE</strong> can have different <strong>central</strong> "shops" in each country (<em>contact language</em>), but also different <strong><em>franchises</em></strong> in each city (where boss' name would be different).<br><br> I want to be able to INSERT new rows where the <strong>id_c</strong>, <strong>lang</strong> can be not-distinct (many franchises in same country). But <strong>name</strong> has to be distinct only if (<strong>id_c,lang</strong>) is the same (for other id_c,lang combination... name could be the same). And franchise can be the same too only if it has not been assigned in the same (id_c,lang) pair. <br><br> I was thinking about a PRIMARY KEY (<code>lang</code>,<code>name</code>), but it might not be the best way. Is this table structure just too complex?</p>
    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. 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