title: MySQL Error when adding several foreign keys of the same type to one table
body: <p>MySQL Error Code: 1822. Failed to add the foreign key constraint. Missing index for constraint 'fkUsers6' in the reference table 'users'</p>
  
  <p>Happens when there is more than one foreign key of the same type in the table. Any idea to solve this? I haven't found answers to this case yet ...</p>
  
  <p>Table in which the error occurs:</p>
  
  <pre><code>CREATE TABLE IF NOT EXISTS `profiCRM`.`opportunities` (
    `ID` INT NOT NULL AUTO_INCREMENT,
    `opportunity_belongsTo` INT NULL,
    `opportunity_description` VARCHAR(45) NOT NULL,
    `opportunity_type` INT NOT NULL,
    `opportunity_stage` INT NOT NULL,
    `opportunity_product` INT NOT NULL,
    `opportunity_volume` INT NOT NULL,
    `opportunity_dateCreated` DATETIME NOT NULL,
    `opportunity_createdFrom` INT NOT NULL,
    `opportunity_datePlannedClosure` DATE NOT NULL,
    `opportunity_dateChanged` DATETIME NULL,
    `opportunity_changedFrom` INT NULL,
    `opportunity_dateClosed` DATETIME NULL,
    `opportunity_closedFrom` INT NULL,
    `opportunity_dateDeleted` DATETIME NULL,
    `opportunity_deletedFrom` INT NULL,
    PRIMARY KEY (`ID`));
  </code></pre>
  
  <p>Creation of the foreign keys:</p>
  
  <pre><code>ALTER TABLE `profiCRM`.`opportunities`
    ADD INDEX `fkAccounts_idx` (`opportunity_belongsTo` ASC),
    ADD INDEX `fkOpportunityStage_idx` (`opportunity_stage` ASC),
    ADD INDEX `fkOpportunityType_idx` (`opportunity_type` ASC),
    ADD INDEX `fkProducts_idx` (`opportunity_product` ASC),
    ADD INDEX `fkUsers_idx` (`opportunity_createdFrom` ASC, `opportunity_changedFrom` ASC, `opportunity_closedFrom` ASC, `opportunity_deletedFrom` ASC),
    ADD CONSTRAINT `fkAccounts6`
      FOREIGN KEY (`opportunity_belongsTo`)
      REFERENCES `profiCRM`.`accounts` (`ID`)
      ON DELETE NO ACTION
      ON UPDATE NO ACTION,
    ADD CONSTRAINT `fkOpportunityStage6`
      FOREIGN KEY (`opportunity_stage`)
      REFERENCES `profiCRM`.`opportunityStage` (`ID`)
      ON DELETE NO ACTION
      ON UPDATE NO ACTION,
    ADD CONSTRAINT `fkOpportunityType6`
      FOREIGN KEY (`opportunity_type`)
      REFERENCES `profiCRM`.`opportunityTypes` (`ID`)
      ON DELETE NO ACTION
      ON UPDATE NO ACTION,
    ADD CONSTRAINT `fkProducts6`
      FOREIGN KEY (`opportunity_product`)
      REFERENCES `profiCRM`.`products` (`ID`)
      ON DELETE NO ACTION
      ON UPDATE NO ACTION,
    ADD CONSTRAINT `fkUsers6`
      FOREIGN KEY (`opportunity_createdFrom` , `opportunity_changedFrom` , `opportunity_closedFrom` , `opportunity_deletedFrom`)
      REFERENCES `profiCRM`.`users` (`ID` , `ID` , `ID` , `ID`)
      ON DELETE NO ACTION
      ON UPDATE NO ACTION;
  </code></pre>
  
  <p>Table of which the foreign key is from:</p>
  
  <pre><code>CREATE TABLE IF NOT EXISTS `profiCRM`.`users` (
    `ID` INT NOT NULL AUTO_INCREMENT,
    `user_belongsTo` INT NOT NULL,
    `user_firstName` VARCHAR(45) NOT NULL,
    `user_lastName` VARCHAR(45) NOT NULL,
    `user_dateOfBirth` DATE NOT NULL,
    `user_adress1` VARCHAR(45) NOT NULL,
    `user_adress2` VARCHAR(45) NULL,
    `user_department` VARCHAR(45) NOT NULL,
    `user_zipcode` VARCHAR(10) NOT NULL,
    `user_city` VARCHAR(45) NOT NULL,
    `user_state` VARCHAR(45) NULL,
    `user_country` INT NOT NULL,
    `user_email` VARCHAR(45) NOT NULL,
    `user_telephoneFixed` VARCHAR(45) NULL,
    `user_telephoneMobile` VARCHAR(45) NULL,
    `user_rightViewData` INT NOT NULL,
    `user_rightCreateData` TINYINT NULL,
    `user_rightCreateUser` TINYINT NULL,
    `user_rightCreateAttribute` TINYINT NULL,
    `user_rightCreateList` TINYINT NULL,
    `user_rightEditData` TINYINT NULL,
    `user_rightEditUser` TINYINT NULL,
    `user_rightEditAttribute` TINYINT NULL,
    `user_rightEditList` TINYINT NULL,
    `user_rightDeleteData` TINYINT NULL,
    `user_rightDeleteUser` TINYINT NULL,
    `user_rightDeleteAttribute` TINYINT NULL,
    `user_rightDeleteList` TINYINT NULL,
    `user_dateCreated` DATETIME NOT NULL,
    `user_createdFrom` INT NULL,
    `user_dateChanged` DATETIME NULL,
    `user_changedFrom` INT NULL,
    `user_dateDeleted` DATETIME NULL,
    `user_deletedFrom` INT NULL,
    PRIMARY KEY (`ID`));
  </code></pre>
  
  <blockquote>
    <p>Here the solution: (I had to define an own foreign key for every
    single list item, the possibility to select several items for one key
    in the MySQL Workbench is not such purposeful)</p>
  </blockquote>
  
  <pre><code>-- MySQL Script generated by MySQL Workbench
  -- Wed Oct 25 14:20:25 2017
  -- Model: New Model    Version: 1.0
  -- MySQL Workbench Forward Engineering
  
  SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
  SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
  SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='TRADITIONAL,ALLOW_INVALID_DATES';
  
  -- -----------------------------------------------------
  -- Schema profiCRM
  -- -----------------------------------------------------
  
  -- -----------------------------------------------------
  -- Schema profiCRM
  -- -----------------------------------------------------
  CREATE SCHEMA IF NOT EXISTS `profiCRM` DEFAULT CHARACTER SET utf8 ;
  SHOW WARNINGS;
  USE `profiCRM` ;
  
  -- -----------------------------------------------------
  -- Table `profiCRM`.`countries`
  -- -----------------------------------------------------
  CREATE TABLE IF NOT EXISTS `profiCRM`.`countries` (
    `ID` INT NOT NULL AUTO_INCREMENT,
    `countryName` VARCHAR(45) NULL,
    PRIMARY KEY (`ID`),
    UNIQUE INDEX `ID_UNIQUE` (`ID` ASC))
  ENGINE = InnoDB;
  
  SHOW WARNINGS;
  
  -- -----------------------------------------------------
  -- Table `profiCRM`.`users`
  -- -----------------------------------------------------
  CREATE TABLE IF NOT EXISTS `profiCRM`.`users` (
    `ID` INT NOT NULL AUTO_INCREMENT,
    `user_belongsTo` INT NOT NULL,
    `user_firstName` VARCHAR(45) NOT NULL,
    `user_lastName` VARCHAR(45) NOT NULL,
    `user_dateOfBirth` DATE NOT NULL,
    `user_adress1` VARCHAR(45) NOT NULL,
    `user_adress2` VARCHAR(45) NULL,
    `user_department` VARCHAR(45) NOT NULL,
    `user_zipcode` VARCHAR(10) NOT NULL,
    `user_city` VARCHAR(45) NOT NULL,
    `user_state` VARCHAR(45) NULL,
    `user_country` INT NOT NULL,
    `user_email` VARCHAR(45) NOT NULL,
    `user_telephoneFixed` VARCHAR(45) NULL,
    `user_telephoneMobile` VARCHAR(45) NULL,
    `user_rightViewData` INT NOT NULL,
    `user_rightCreateData` TINYINT NULL,
    `user_rightCreateUser` TINYINT NULL,
    `user_rightCreateAttribute` TINYINT NULL,
    `user_rightCreateList` TINYINT NULL,
    `user_rightEditData` TINYINT NULL,
    `user_rightEditUser` TINYINT NULL,
    `user_rightEditAttribute` TINYINT NULL,
    `user_rightEditList` TINYINT NULL,
    `user_rightDeleteData` TINYINT NULL,
    `user_rightDeleteUser` TINYINT NULL,
    `user_rightDeleteAttribute` TINYINT NULL,
    `user_rightDeleteList` TINYINT NULL,
    `user_dateCreated` DATETIME NOT NULL,
    `user_createdFrom` INT NULL,
    `user_dateChanged` DATETIME NULL,
    `user_changedFrom` INT NULL,
    `user_dateDeleted` DATETIME NULL,
    `user_deletedFrom` INT NULL,
    PRIMARY KEY (`ID`),
    INDEX `fkCountries_idx` (`user_country` ASC),
    INDEX `fkUsers_idx` (`user_rightViewData` ASC),
    INDEX `fkCorporateAccounts_idx` (`user_belongsTo` ASC),
    UNIQUE INDEX `ID_UNIQUE` (`ID` ASC),
    INDEX `fkUsersToCreatedFrom_idx` (`user_createdFrom` ASC),
    INDEX `fkUsersToChangedFrom_idx` (`user_changedFrom` ASC),
    INDEX `fkUsersToDeletedFrom_idx` (`user_deletedFrom` ASC),
    CONSTRAINT `fkCountries2`
      FOREIGN KEY (`user_country`)
      REFERENCES `profiCRM`.`countries` (`ID`)
      ON DELETE NO ACTION
      ON UPDATE NO ACTION,
    CONSTRAINT `fkUsersToRightViewData2`
      FOREIGN KEY (`user_rightViewData`)
      REFERENCES `profiCRM`.`users` (`ID`)
      ON DELETE NO ACTION
      ON UPDATE NO ACTION,
    CONSTRAINT `fkCorporateAccounts2`
      FOREIGN KEY (`user_belongsTo`)
      REFERENCES `profiCRM`.`corporateAccounts` (`ID`)
      ON DELETE NO ACTION
      ON UPDATE NO ACTION,
    CONSTRAINT `fkUsersToCreatedFrom2`
      FOREIGN KEY (`user_createdFrom`)
      REFERENCES `profiCRM`.`users` (`ID`)
      ON DELETE NO ACTION
      ON UPDATE NO ACTION,
    CONSTRAINT `fkUsersToChangedFrom2`
      FOREIGN KEY (`user_changedFrom`)
      REFERENCES `profiCRM`.`users` (`ID`)
      ON DELETE NO ACTION
      ON UPDATE NO ACTION,
    CONSTRAINT `fkUsersToDeletedFrom2`
      FOREIGN KEY (`user_deletedFrom`)
      REFERENCES `profiCRM`.`users` (`ID`)
      ON DELETE NO ACTION
      ON UPDATE NO ACTION)
  ENGINE = InnoDB;
  
  SHOW WARNINGS;
  
  -- -----------------------------------------------------
  -- Table `profiCRM`.`corporateAccounts`
  -- -----------------------------------------------------
  CREATE TABLE IF NOT EXISTS `profiCRM`.`corporateAccounts` (
    `ID` INT NOT NULL AUTO_INCREMENT,
    `corporateAccount_name1` VARCHAR(45) NOT NULL,
    `corporateAccount_name2` VARCHAR(45) NULL,
    `corporateAccount_adress1` VARCHAR(45) NOT NULL,
    `corporateAccount_adress2` VARCHAR(45) NULL,
    `corporateAccount_department` VARCHAR(45) NULL,
    `corporateAccount_zipcode` VARCHAR(10) NOT NULL,
    `corporateAccount_city` VARCHAR(45) NOT NULL,
    `corporateAccount_state` VARCHAR(45) NULL,
    `corporateAccount_country` INT NOT NULL,
    `corporateAccount_parentTo` INT NULL,
    `corporateAccount_childOf` INT NULL,
    `corporateAccount_dateCreated` DATETIME NOT NULL,
    `corporateAccount_dateChanged` DATETIME NULL,
    `corporateAccount_changedFrom` INT NULL,
    `corporateAccount_dateDeleted` DATETIME NULL,
    `corporateAccount_deletedFrom` INT NULL,
    PRIMARY KEY (`ID`),
    INDEX `fkCountries_idx` (`corporateAccount_country` ASC),
    INDEX `fkCorporateAccounts_idx` (`corporateAccount_parentTo` ASC),
    INDEX `fkUsers_idx` (`corporateAccount_changedFrom` ASC),
    UNIQUE INDEX `ID_UNIQUE` (`ID` ASC),
    INDEX `fkUsersDeletedFrom_idx` (`corporateAccount_deletedFrom` ASC),
    INDEX `fkCorporateAccountsToChildOf1_idx` (`corporateAccount_childOf` ASC),
    CONSTRAINT `fkCountries1`
      FOREIGN KEY (`corporateAccount_country`)
      REFERENCES `profiCRM`.`countries` (`ID`)
      ON DELETE NO ACTION
      ON UPDATE NO ACTION,
    CONSTRAINT `fkCorporateAccountsToParentTo1`
      FOREIGN KEY (`corporateAccount_parentTo`)
      REFERENCES `profiCRM`.`corporateAccounts` (`ID`)
      ON DELETE NO ACTION
      ON UPDATE NO ACTION,
    CONSTRAINT `fkUsersToChangedFrom1`
      FOREIGN KEY (`corporateAccount_changedFrom`)
      REFERENCES `profiCRM`.`users` (`ID`)
      ON DELETE NO ACTION
      ON UPDATE NO ACTION,
    CONSTRAINT `fkUsersToDeletedFrom1`
      FOREIGN KEY (`corporateAccount_deletedFrom`)
      REFERENCES `profiCRM`.`users` (`ID`)
      ON DELETE NO ACTION
      ON UPDATE NO ACTION,
    CONSTRAINT `fkCorporateAccountsToChildOf1`
      FOREIGN KEY (`corporateAccount_childOf`)
      REFERENCES `profiCRM`.`corporateAccounts` (`ID`)
      ON DELETE NO ACTION
      ON UPDATE NO ACTION)
  ENGINE = InnoDB;
  
  SHOW WARNINGS;
  
  -- -----------------------------------------------------
  -- Table `profiCRM`.`accountTypes`
  -- -----------------------------------------------------
  CREATE TABLE IF NOT EXISTS `profiCRM`.`accountTypes` (
    `ID` INT NOT NULL AUTO_INCREMENT,
    `accountType_description` VARCHAR(45) NOT NULL,
    PRIMARY KEY (`ID`),
    UNIQUE INDEX `ID_UNIQUE` (`ID` ASC))
  ENGINE = InnoDB;
  
  SHOW WARNINGS;
  
  -- -----------------------------------------------------
  -- Table `profiCRM`.`competition`
  -- -----------------------------------------------------
  CREATE TABLE IF NOT EXISTS `profiCRM`.`competition` (
    `ID` INT NOT NULL AUTO_INCREMENT,
    `competition_description` VARCHAR(45) NOT NULL,
    PRIMARY KEY (`ID`),
    UNIQUE INDEX `ID_UNIQUE` (`ID` ASC))
  ENGINE = InnoDB;
  
  SHOW WARNINGS;
  
  -- -----------------------------------------------------
  -- Table `profiCRM`.`opportunityStage`
  -- -----------------------------------------------------
  CREATE TABLE IF NOT EXISTS `profiCRM`.`opportunityStage` (
    `ID` INT NOT NULL AUTO_INCREMENT,
    `opportunityStage_description` VARCHAR(45) NOT NULL,
    PRIMARY KEY (`ID`),
    UNIQUE INDEX `ID_UNIQUE` (`ID` ASC))
  ENGINE = InnoDB;
  
  SHOW WARNINGS;
  
  -- -----------------------------------------------------
  -- Table `profiCRM`.`opportunityTypes`
  -- -----------------------------------------------------
  CREATE TABLE IF NOT EXISTS `profiCRM`.`opportunityTypes` (
    `ID` INT NOT NULL AUTO_INCREMENT,
    `opportunityType_description` VARCHAR(45) NOT NULL,
    PRIMARY KEY (`ID`),
    UNIQUE INDEX `ID_UNIQUE` (`ID` ASC))
  ENGINE = InnoDB;
  
  SHOW WARNINGS;
  
  -- -----------------------------------------------------
  -- Table `profiCRM`.`products`
  -- -----------------------------------------------------
  CREATE TABLE IF NOT EXISTS `profiCRM`.`products` (
    `ID` INT NOT NULL AUTO_INCREMENT,
    `product_description` VARCHAR(45) NOT NULL,
    PRIMARY KEY (`ID`),
    UNIQUE INDEX `ID_UNIQUE` (`ID` ASC))
  ENGINE = InnoDB;
  
  SHOW WARNINGS;
  
  -- -----------------------------------------------------
  -- Table `profiCRM`.`opportunities`
  -- -----------------------------------------------------
  CREATE TABLE IF NOT EXISTS `profiCRM`.`opportunities` (
    `ID` INT NOT NULL AUTO_INCREMENT,
    `opportunity_belongsTo` INT NULL,
    `opportunity_description` VARCHAR(45) NOT NULL,
    `opportunity_type` INT NOT NULL,
    `opportunity_stage` INT NOT NULL,
    `opportunity_product` INT NOT NULL,
    `opportunity_volume` INT NOT NULL,
    `opportunity_dateCreated` DATETIME NOT NULL,
    `opportunity_createdFrom` INT NOT NULL,
    `opportunity_datePlannedClosure` DATE NOT NULL,
    `opportunity_dateChanged` DATETIME NULL,
    `opportunity_changedFrom` INT NULL,
    `opportunity_dateClosed` DATETIME NULL,
    `opportunity_closedFrom` INT NULL,
    `opportunity_dateDeleted` DATETIME NULL,
    `opportunity_deletedFrom` INT NULL,
    PRIMARY KEY (`ID`),
    INDEX `fkAccounts_idx` (`opportunity_belongsTo` ASC),
    INDEX `fkOpportunityStage_idx` (`opportunity_stage` ASC),
    INDEX `fkOpportunityType_idx` (`opportunity_type` ASC),
    INDEX `fkProducts_idx` (`opportunity_product` ASC),
    INDEX `fkUsers_idx` (`opportunity_createdFrom` ASC),
    UNIQUE INDEX `ID_UNIQUE` (`ID` ASC),
    INDEX `fkUsersToChangedFrom6_idx` (`opportunity_changedFrom` ASC),
    INDEX `fkUsersToClosedFrom6_idx` (`opportunity_closedFrom` ASC),
    INDEX `fkUsersToDeletedFrom6_idx` (`opportunity_deletedFrom` ASC),
    CONSTRAINT `fkAccounts6`
      FOREIGN KEY (`opportunity_belongsTo`)
      REFERENCES `profiCRM`.`accounts` (`ID`)
      ON DELETE NO ACTION
      ON UPDATE NO ACTION,
    CONSTRAINT `fkOpportunityStage6`
      FOREIGN KEY (`opportunity_stage`)
      REFERENCES `profiCRM`.`opportunityStage` (`ID`)
      ON DELETE NO ACTION
      ON UPDATE NO ACTION,
    CONSTRAINT `fkOpportunityType6`
      FOREIGN KEY (`opportunity_type`)
      REFERENCES `profiCRM`.`opportunityTypes` (`ID`)
      ON DELETE NO ACTION
      ON UPDATE NO ACTION,
    CONSTRAINT `fkProducts6`
      FOREIGN KEY (`opportunity_product`)
      REFERENCES `profiCRM`.`products` (`ID`)
      ON DELETE NO ACTION
      ON UPDATE NO ACTION,
    CONSTRAINT `fkUsersToCreatedFrom6`
      FOREIGN KEY (`opportunity_createdFrom`)
      REFERENCES `profiCRM`.`users` (`ID`)
      ON DELETE NO ACTION
      ON UPDATE NO ACTION,
    CONSTRAINT `fkUsersToChangedFrom6`
      FOREIGN KEY (`opportunity_changedFrom`)
      REFERENCES `profiCRM`.`users` (`ID`)
      ON DELETE NO ACTION
      ON UPDATE NO ACTION,
    CONSTRAINT `fkUsersToClosedFrom6`
      FOREIGN KEY (`opportunity_closedFrom`)
      REFERENCES `profiCRM`.`users` (`ID`)
      ON DELETE NO ACTION
      ON UPDATE NO ACTION,
    CONSTRAINT `fkUsersToDeletedFrom6`
      FOREIGN KEY (`opportunity_deletedFrom`)
      REFERENCES `profiCRM`.`users` (`ID`)
      ON DELETE NO ACTION
      ON UPDATE NO ACTION)
  ENGINE = InnoDB;
  
  SHOW WARNINGS;
  
  -- -----------------------------------------------------
  -- Table `profiCRM`.`accounts`
  -- -----------------------------------------------------
  CREATE TABLE IF NOT EXISTS `profiCRM`.`accounts` (
    `ID` INT NOT NULL AUTO_INCREMENT,
    `account_belongsTo` INT NOT NULL,
    `account_kindOf` INT NOT NULL,
    `account_name1` VARCHAR(45) NOT NULL,
    `account_name2` VARCHAR(45) NULL,
    `account_adress1` VARCHAR(45) NOT NULL,
    `account_adress2` VARCHAR(45) NULL,
    `account_zipcode` VARCHAR(45) NOT NULL,
    `account_city` VARCHAR(45) NOT NULL,
    `account_state` VARCHAR(45) NULL,
    `account_country` INT NOT NULL,
    `account_email` VARCHAR(45) NULL,
    `account_telephoneFixed` VARCHAR(45) NULL,
    `account_telephoneMobile` VARCHAR(45) NULL,
    `account_fax` VARCHAR(45) NULL,
    `account_internet` VARCHAR(45) NULL,
    `account_childOf` INT NULL,
    `account_parentTo` INT NULL,
    `account_turnover` INT NULL,
    `account_numberEmployees` INT NULL,
    `account_numberSubsidaries` VARCHAR(45) NULL,
    `account_generalInfo` VARCHAR(2000) NULL,
    `account_competition` INT NULL,
    `account_opportunities` INT NULL,
    `account_dateCreated` DATETIME NULL,
    `account_createdFrom` INT NULL,
    `account_dateChanged` DATETIME NULL,
    `account_changedFrom` INT NULL,
    `account_dateDeleted` DATETIME NULL,
    `account_deletedFrom` INT NULL,
    PRIMARY KEY (`ID`),
    INDEX `fkUsers_idx` (`account_belongsTo` ASC),
    INDEX `fkCountries_idx` (`account_country` ASC),
    INDEX `fkAccounts_idx` (`account_childOf` ASC),
    INDEX `fkAccountTypes_idx` (`account_kindOf` ASC),
    INDEX `fkCompetition_idx` (`account_competition` ASC),
    INDEX `fkOpportunities_idx` (`account_opportunities` ASC),
    UNIQUE INDEX `ID_UNIQUE` (`ID` ASC),
    INDEX `FKUsersToCreatedFrom3_idx` (`account_createdFrom` ASC),
    INDEX `fkUsersToChangedFrom3_idx` (`account_changedFrom` ASC),
    INDEX `fkUsersToDeletedFrom3_idx` (`account_deletedFrom` ASC),
    INDEX `fkAccountsToParentTo3_idx` (`account_parentTo` ASC),
    CONSTRAINT `fkUsersToBelongsTo3`
      FOREIGN KEY (`account_belongsTo`)
      REFERENCES `profiCRM`.`users` (`ID`)
      ON DELETE NO ACTION
      ON UPDATE NO ACTION,
    CONSTRAINT `fkCountries3`
      FOREIGN KEY (`account_country`)
      REFERENCES `profiCRM`.`countries` (`ID`)
      ON DELETE NO ACTION
      ON UPDATE NO ACTION,
    CONSTRAINT `fkAccountsToChildOf3`
      FOREIGN KEY (`account_childOf`)
      REFERENCES `profiCRM`.`accounts` (`ID`)
      ON DELETE NO ACTION
      ON UPDATE NO ACTION,
    CONSTRAINT `fkAccountTypes3`
      FOREIGN KEY (`account_kindOf`)
      REFERENCES `profiCRM`.`accountTypes` (`ID`)
      ON DELETE NO ACTION
      ON UPDATE NO ACTION,
    CONSTRAINT `fkCompetition3`
      FOREIGN KEY (`account_competition`)
      REFERENCES `profiCRM`.`competition` (`ID`)
      ON DELETE NO ACTION
      ON UPDATE NO ACTION,
    CONSTRAINT `fkOpportunities3`
      FOREIGN KEY (`account_opportunities`)
      REFERENCES `profiCRM`.`opportunities` (`ID`)
      ON DELETE NO ACTION
      ON UPDATE NO ACTION,
    CONSTRAINT `fkUsersToCreatedFrom3`
      FOREIGN KEY (`account_createdFrom`)
      REFERENCES `profiCRM`.`users` (`ID`)
      ON DELETE NO ACTION
      ON UPDATE NO ACTION,
    CONSTRAINT `fkUsersToChangedFrom3`
      FOREIGN KEY (`account_changedFrom`)
      REFERENCES `profiCRM`.`users` (`ID`)
      ON DELETE NO ACTION
      ON UPDATE NO ACTION,
    CONSTRAINT `fkUsersToDeletedFrom3`
      FOREIGN KEY (`account_deletedFrom`)
      REFERENCES `profiCRM`.`users` (`ID`)
      ON DELETE NO ACTION
      ON UPDATE NO ACTION,
    CONSTRAINT `fkAccountsToParentTo3`
      FOREIGN KEY (`account_parentTo`)
      REFERENCES `profiCRM`.`accounts` (`ID`)
      ON DELETE NO ACTION
      ON UPDATE NO ACTION)
  ENGINE = InnoDB;
  
  SHOW WARNINGS;
  
  -- -----------------------------------------------------
  -- Table `profiCRM`.`formOfAdress`
  -- -----------------------------------------------------
  CREATE TABLE IF NOT EXISTS `profiCRM`.`formOfAdress` (
    `ID` INT NOT NULL AUTO_INCREMENT,
    `formOfAdress_description` VARCHAR(45) NOT NULL,
    PRIMARY KEY (`ID`),
    UNIQUE INDEX `ID_UNIQUE` (`ID` ASC))
  ENGINE = InnoDB;
  
  SHOW WARNINGS;
  
  -- -----------------------------------------------------
  -- Table `profiCRM`.`contacts`
  -- -----------------------------------------------------
  CREATE TABLE IF NOT EXISTS `profiCRM`.`contacts` (
    `ID` INT NOT NULL AUTO_INCREMENT,
    `contact_belongsTo` INT NOT NULL,
    `contact_formOfAdress` INT NOT NULL,
    `contact_title` VARCHAR(45) NULL,
    `contact_firstName` VARCHAR(45) NOT NULL,
    `contact_lastName` VARCHAR(45) NOT NULL,
    `contact_function` VARCHAR(45) NOT NULL,
    `contact_department` VARCHAR(45) NOT NULL,
    `contact_telephoneFixed` VARCHAR(45) NULL,
    `contact_telephoneMobile` VARCHAR(45) NULL,
    `contact_email` VARCHAR(45) NULL,
    `contact_fax` VARCHAR(45) NULL,
    `contact_dateOfBirth` DATE NULL,
    `contact_childOf` INT NULL,
    `contact_parentTo` INT NULL,
    `contact_generalInfo` VARCHAR(2000) NULL,
    `contact_dateCreated` DATETIME NULL,
    `contact_createdFrom` INT NULL,
    `contact_dateChanged` DATETIME NULL,
    `contact_changedFrom` INT NULL,
    `contact_dateDeleted` DATETIME NULL,
    `contact_deletedFrom` INT NULL,
    PRIMARY KEY (`ID`),
    INDEX `fkAccounts_idx` (`contact_belongsTo` ASC),
    INDEX `fkFormOfAdress_idx` (`contact_formOfAdress` ASC),
    INDEX `fkContacts_idx` (`contact_childOf` ASC),
    INDEX `fkUsers_idx` (`contact_createdFrom` ASC),
    UNIQUE INDEX `ID_UNIQUE` (`ID` ASC),
    INDEX `fkContactsToParentTo4_idx` (`contact_parentTo` ASC),
    INDEX `fkUsersToChangedFrom4_idx` (`contact_changedFrom` ASC),
    INDEX `fkUsersToDeletedFrom4_idx` (`contact_deletedFrom` ASC),
    CONSTRAINT `fkAccounts4`
      FOREIGN KEY (`contact_belongsTo`)
      REFERENCES `profiCRM`.`accounts` (`ID`)
      ON DELETE NO ACTION
      ON UPDATE NO ACTION,
    CONSTRAINT `fkFormOfAdress4`
      FOREIGN KEY (`contact_formOfAdress`)
      REFERENCES `profiCRM`.`formOfAdress` (`ID`)
      ON DELETE NO ACTION
      ON UPDATE NO ACTION,
    CONSTRAINT `fkContactsToChildOf4`
      FOREIGN KEY (`contact_childOf`)
      REFERENCES `profiCRM`.`contacts` (`ID`)
      ON DELETE NO ACTION
      ON UPDATE NO ACTION,
    CONSTRAINT `fkUsersToCreatedFrom4`
      FOREIGN KEY (`contact_createdFrom`)
      REFERENCES `profiCRM`.`users` (`ID`)
      ON DELETE NO ACTION
      ON UPDATE NO ACTION,
    CONSTRAINT `fkContactsToParentTo4`
      FOREIGN KEY (`contact_parentTo`)
      REFERENCES `profiCRM`.`contacts` (`ID`)
      ON DELETE NO ACTION
      ON UPDATE NO ACTION,
    CONSTRAINT `fkUsersToChangedFrom4`
      FOREIGN KEY (`contact_changedFrom`)
      REFERENCES `profiCRM`.`users` (`ID`)
      ON DELETE NO ACTION
      ON UPDATE NO ACTION,
    CONSTRAINT `fkUsersToDeletedFrom4`
      FOREIGN KEY (`contact_deletedFrom`)
      REFERENCES `profiCRM`.`users` (`ID`)
      ON DELETE NO ACTION
      ON UPDATE NO ACTION)
  ENGINE = InnoDB;
  
  SHOW WARNINGS;
  
  -- -----------------------------------------------------
  -- Table `profiCRM`.`activityType`
  -- -----------------------------------------------------
  CREATE TABLE IF NOT EXISTS `profiCRM`.`activityType` (
    `ID` INT NOT NULL AUTO_INCREMENT,
    `activityType_description` VARCHAR(45) NOT NULL,
    PRIMARY KEY (`ID`),
    UNIQUE INDEX `ID_UNIQUE` (`ID` ASC))
  ENGINE = InnoDB;
  
  SHOW WARNINGS;
  
  -- -----------------------------------------------------
  -- Table `profiCRM`.`activities`
  -- -----------------------------------------------------
  CREATE TABLE IF NOT EXISTS `profiCRM`.`activities` (
    `ID` INT NOT NULL AUTO_INCREMENT,
    `activity_belongsToContact` INT NOT NULL,
    `activity_belongsToUser` INT NULL,
    `activity_type` INT NOT NULL,
    `activity_dateStart` DATETIME NOT NULL,
    `activity_dateEnde` DATETIME NOT NULL,
    `activity_childOf` INT NULL,
    `activity_goal` VARCHAR(2000) NOT NULL,
    `activity_result` VARCHAR(2000) NULL,
    `activity_todo` VARCHAR(2000) NULL,
    `activity_todoTimeframe` DATE NULL,
    `activity_dateCreated` DATETIME NOT NULL,
    `activity_createdFrom` INT NOT NULL,
    `activity_dateChanged` DATETIME NULL,
    `activity_changedFrom` INT NULL,
    `activity_dateClosed` DATETIME NULL,
    `activity_closedFrom` INT NULL,
    `activity_dateDeleted` DATETIME NULL,
    `activity_deletedFrom` INT NULL,
    PRIMARY KEY (`ID`),
    INDEX `fkActivityTypes_idx` (`activity_type` ASC),
    INDEX `fkContacts_idx` (`activity_belongsToContact` ASC),
    INDEX `fkUsers_idx` (`activity_belongsToUser` ASC),
    INDEX `fkActivities_idx` (`activity_childOf` ASC),
    UNIQUE INDEX `ID_UNIQUE` (`ID` ASC),
    INDEX `fkUserToCreatedFrom5_idx` (`activity_createdFrom` ASC),
    INDEX `fkUserToChangedFrom5_idx` (`activity_changedFrom` ASC),
    INDEX `fkUserToClosedFrom5_idx` (`activity_closedFrom` ASC),
    INDEX `fkUserToDeletedFrom5_idx` (`activity_deletedFrom` ASC),
    CONSTRAINT `fkActivityTypes5`
      FOREIGN KEY (`activity_type`)
      REFERENCES `profiCRM`.`activityType` (`ID`)
      ON DELETE NO ACTION
      ON UPDATE NO ACTION,
    CONSTRAINT `fkContacts5`
      FOREIGN KEY (`activity_belongsToContact`)
      REFERENCES `profiCRM`.`contacts` (`ID`)
      ON DELETE NO ACTION
      ON UPDATE NO ACTION,
    CONSTRAINT `fkUsersToBelongsToUser5`
      FOREIGN KEY (`activity_belongsToUser`)
      REFERENCES `profiCRM`.`users` (`ID`)
      ON DELETE NO ACTION
      ON UPDATE NO ACTION,
    CONSTRAINT `fkActivities5`
      FOREIGN KEY (`activity_childOf`)
      REFERENCES `profiCRM`.`activities` (`ID`)
      ON DELETE NO ACTION
      ON UPDATE NO ACTION,
    CONSTRAINT `fkUserToCreatedFrom5`
      FOREIGN KEY (`activity_createdFrom`)
      REFERENCES `profiCRM`.`users` (`ID`)
      ON DELETE NO ACTION
      ON UPDATE NO ACTION,
    CONSTRAINT `fkUserToChangedFrom5`
      FOREIGN KEY (`activity_changedFrom`)
      REFERENCES `profiCRM`.`users` (`ID`)
      ON DELETE NO ACTION
      ON UPDATE NO ACTION,
    CONSTRAINT `fkUserToClosedFrom5`
      FOREIGN KEY (`activity_closedFrom`)
      REFERENCES `profiCRM`.`users` (`ID`)
      ON DELETE NO ACTION
      ON UPDATE NO ACTION,
    CONSTRAINT `fkUserToDeletedFrom5`
      FOREIGN KEY (`activity_deletedFrom`)
      REFERENCES `profiCRM`.`users` (`ID`)
      ON DELETE NO ACTION
      ON UPDATE NO ACTION)
  ENGINE = InnoDB;
  
  SHOW WARNINGS;
  
  SET SQL_MODE=@OLD_SQL_MODE;
  SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
  SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;
  </code></pre>