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:
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;
id_c: Id of customer. It can be an enterprise. Suppose McDonald’s
lang: Contact language.
boss: Boss’ name
franchise: If not zero, it is a franchise. McDonald’s in Rome, Paris, London…
As you can see, each ENTERPRISE can have different central “shops” in each country (contact language), but also different franchises in each city (where boss’ name would be different).
I want to be able to INSERT new rows where the id_c, lang can be not-distinct (many franchises in same country). But name has to be distinct only if (id_c,lang) 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.
I was thinking about a PRIMARY KEY (lang,name), but it might not be the best way. Is this table structure just too complex?
you need to create a multiple column
UNIQUEconstraint,or set them as the primary key,