If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > MySQl foreign Keys

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-04-04, 14:57
jorgeQA jorgeQA is offline
Registered User
 
Join Date: Jun 2004
Posts: 1
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.
Reply With Quote
  #2 (permalink)  
Old 06-05-04, 01:54
guelphdad guelphdad is offline
Registered User
 
Join Date: Mar 2004
Posts: 440
Perhaps you could tell us what errors you are getting? And of course you already created the other tables that the foreign keys reference?
Reply With Quote
  #3 (permalink)  
Old 06-05-04, 07:31
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
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
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #4 (permalink)  
Old 06-08-04, 15:34
Leaden Leaden is offline
Registered User
 
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)
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On