Results 1 to 3 of 3
  1. #1
    Join Date
    Jan 2010
    Posts
    2

    Unanswered: Multiple Foreign Keys to Multiple references

    Hi,

    Im trying to make this table (orders) in which i have 2 different companies (taken from a single company table) and 4 different contacts (taken from a single contacts table)... what am i doing wrong with the references?

    Thank you in advance

    CREATE TABLE IF NOT EXISTS `db`.`orders` (
    `id_order` INT(11) NOT NULL AUTO_INCREMENT ,
    `id_company` INT(11) NOT NULL ,
    `id_related_company` INT(11) NULL DEFAULT NULL ,
    `id_contact_1` INT(11) NOT NULL ,
    `id_contact_2` INT(11) NULL DEFAULT NULL ,
    `id_contact_3` INT(11) NOT NULL ,
    `id_contact_4` INT(11) NOT NULL ,
    PRIMARY KEY (`id_order`) ,
    CONSTRAINT `orders_ibfk_1`
    FOREIGN KEY (`id_company` , `id_related_company` )
    REFERENCES `db`.`companies` (`id_company` , `id_company` ),
    CONSTRAINT `ordenes_ibfk_2`
    FOREIGN KEY (`id_contact_1` , `id_contact_2` , `id_contact_3` , `id_contact_4` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
    ENGINE = InnoDB
    AUTO_INCREMENT = 1
    DEFAULT CHARACTER SET = utf8;

    CREATE INDEX `id_related_company` ON `db`.`orders` (`id_related_company` ASC) ;

    CREATE INDEX `id_company` ON `db`.`orders` (`id_company` ASC) ;

    CREATE INDEX `id_contact_1` ON `db`.`orders` (`id_contact_1` ASC) ;

    CREATE INDEX `id_contact_2` ON `db`.`orders` (`id_contact_2` ASC) ;

    CREATE INDEX `id_contact_3` ON `db`.`orders` (`id_contact_3` ASC) ;

    CREATE INDEX `id_contact_4` ON `db`.`orders` (`id_contact_4` ASC) ;

  2. #2
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    Code:
    CREATE TABLE IF NOT EXISTS db.orders 
    ( id_order INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY
    , id_company INTEGER NOT NULL 
    , id_related_company INTEGER NULL DEFAULT NULL 
    , id_contact_1 INTEGER NOT NULL 
    , id_contact_2 INTEGER NULL DEFAULT NULL 
    , id_contact_3 INTEGER NOT NULL 
    , id_contact_4 INTEGER NOT NULL 
    , CONSTRAINT orders_ibfk_1a
         FOREIGN KEY (id_company ) REFERENCES db.companies (id_company)
    , CONSTRAINT orders_ibfk_1b
         FOREIGN KEY (id_related_company ) REFERENCES db.companies (id_company)
    , CONSTRAINT ordenes_ibfk_2a
         FOREIGN KEY (id_contact_1) REFERENCES db.contacts (id_contact)
           ON DELETE NO ACTION ON UPDATE NO ACTION
    , CONSTRAINT ordenes_ibfk_2b
         FOREIGN KEY (id_contact_2) REFERENCES db.contacts (id_contact)
           ON DELETE NO ACTION ON UPDATE NO ACTION
    , CONSTRAINT ordenes_ibfk_2c
         FOREIGN KEY (id_contact_3) REFERENCES db.contacts (id_contact)
           ON DELETE NO ACTION ON UPDATE NO ACTION
    , CONSTRAINT ordenes_ibfk_2d
         FOREIGN KEY (id_contact_4) REFERENCES db.contacts (id_contact)
           ON DELETE NO ACTION ON UPDATE NO ACTION
    );
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

  3. #3
    Join Date
    Jan 2010
    Posts
    2
    Thank you so much

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •