Results 1 to 4 of 4
  1. #1
    Join Date
    Jun 2004
    Posts
    1

    Unanswered: MySQl foreign Keys

    Hi,

    I'm trying to create a primary key with 2 foreign keys as follows :

    create Table Achats(
    idContrat INT NOT NULL,
    idProduit VARCHAR(12) NOT NULL,
    quantite INT NOT NULL,
    PRIMARY KEY(idContrat,idProduit),
    INDEX (idContrat,idProduit),
    FOREIGN KEY(idContrat) REFERENCES Contrats(IdContrat) ON UPDATE CASCADE ON DELETE CASCADE,
    FOREIGN KEY(idProduit) REFERENCES Produit(idProduit) ON UPDATE CASCADE ON DELETE CASCADE) TYPE=INNODB;

    If anyone knows what's wrong ???

    thank you all.

  2. #2
    Join Date
    Mar 2004
    Posts
    480
    Perhaps you could tell us what errors you are getting? And of course you already created the other tables that the foreign keys reference?

  3. #3
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    the foreign key fields need to be indexed

    yo uhave two indexes on this table --

    PRIMARY KEY(idContrat,idProduit)
    INDEX (idContrat,idProduit)

    each foreign key needs an index

    i think (not sure, don't have time to test) that either of the above compound indexes will suffice for the index for the fk on idContrat, but the fk on idProduit does not have an index that it can use
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

  4. #4
    Join Date
    Dec 2003
    Location
    Houston, TX
    Posts
    21
    Here is some more good information about Foreign Keys.

    http://dev.mysql.com/doc/mysql/en/In...nstraints.html
    I do not fear computers. I fear the lack of them.
    -Isaac Asimov (1920 - 1992)

Posting Permissions

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