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 > Primary key and foreign key shares the same field?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-26-04, 08:24
InHeaven InHeaven is offline
Registered User
 
Join Date: Jan 2004
Posts: 4
Smile Primary key and foreign key shares the same field?

Im creating a table using mySQL with two primary keys and two foreign keys using the same field.

itemA and itemB is a Primary key of another table but also used as the Primary key of table Recommendation. Is it possible to create foreign key and primary key using the same field? I had an error when creating this table. Please help me. Thanx a lot!

===========================================
CREATE TABLE Recommendation
(
itemA INT NOT NULL,
itemB INT NOT NULL,
PRIMARY KEY(itemA),
PRIMARY KEY(itemB),
INDEX(itemA),
INDEX(itemB),
FOREIGN KEY(itemA) REFERENCES Items(itemID) ON UPDATE CASCADE ON DELETE CASCADE,
FOREIGN KEY(itemB) REFERENCES Items(itemID) ON UPDATE CASCADE ON DELETE CASCADE) TYPE=INNODB;
===========================================
Reply With Quote
  #2 (permalink)  
Old 01-26-04, 09:17
aus aus is offline
Registered User
 
Join Date: Oct 2003
Location: Denver, Colorado
Posts: 137
Re: Primary key and foreign key shares the same field?

You can't define multiple PRIMARY keys. You can define PRIMARY keys with multiple columns: PRIMARY KEY(itemA, itemB).
Reply With Quote
  #3 (permalink)  
Old 01-26-04, 09:26
InHeaven InHeaven is offline
Registered User
 
Join Date: Jan 2004
Posts: 4
Smile Re: Primary key and foreign key shares the same field?

i had tried this mtd but also cannot work

===========================
CREATE TABLE Recommendation
(
itemA INT NOT NULL ,
itemB INT NOT NULL ,
PRIMARY KEY(itemA,itemB),
INDEX(itemA),
INDEX(itemB),
FOREIGN KEY(itemA) REFERENCES Items(itemID) ON UPDATE CASCADE ON DELETE CASCADE,
FOREIGN KEY(itemB) REFERENCES Items(itemID) ON UPDATE CASCADE ON DELETE CASCADE) TYPE=INNODB;
===========================
Reply With Quote
  #4 (permalink)  
Old 01-26-04, 10:11
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
syntax looks okay; what doesn't work? what error message did you get?

just guessing... perhaps the fk constraints need explicit names...

, CONSTRAINT itemA_fk FOREIGN KEY itemA_fkindex (itemA) REFERENCES Items(itemID) ON UPDATE CASCADE ON DELETE CASCADE
, CONSTRAINT itemB_fk FOREIGN KEY itemB_fkindex (itemB) REFERENCES Items(itemID) ON UPDATE CASCADE ON DELETE CASCADE
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
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