Connecting Tech Pros Worldwide Help | Site Map

foreign key contraints problem

  #1  
Old February 28th, 2009, 08:00 AM
Member
 
Join Date: Nov 2006
Posts: 50
hello all
i am getting confused with foreign key constraints..
i have the following table:

Expand|Select|Wrap|Line Numbers
  1. --
  2. -- Table structure for table `reviewer`
  3. --
  4.  
  5. CREATE TABLE `reviewer` (
  6.   `ReviewerID` varchar(8) NOT NULL,
  7.   `Reviewer_fname` varchar(100) NOT NULL,
  8.   `Reviewer_lname` varchar(100) NOT NULL,
  9.   `Reviewer_address` varchar(300) NOT NULL,
  10.    etc...
  11.  ...
  12.  ...
  13.   PRIMARY KEY  (`ReviewerID`)
  14. ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
  15.  
  16.  
  17. -- Table structure for table `papers`
  18. --
  19.  
  20. CREATE TABLE `papers` (
  21.   `PaperID` varchar(8) NOT NULL,
  22.   `Title` varchar(100) NOT NULL,
  23.   `FileName` varchar(100) NOT NULL,
  24.   `FileType` varchar(50) NOT NULL,
  25.   `FileSize` int(11) NOT NULL,
  26.   `FileContent` mediumblob NOT NULL,
  27.   `FileMime` varchar(50) NOT NULL,
  28.   `AuthorID` varchar(8) NOT NULL,
  29. ect....
  30. ...
  31.  
  32.   PRIMARY KEY  (`PaperID`),
  33.   KEY `AuthorID` (`AuthorID`)
  34. ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
  35.  
  36.  
now i have another table where i have put both reviewerID and paperID as primary key. But i am getting confused about how to put the foreign keys..

Expand|Select|Wrap|Line Numbers
  1. CREATE TABLE `reviewer_paper` (
  2.   `ReviewerID` varchar(8) NOT NULL,
  3.   `PaperID` varchar(8) NOT NULL,
  4.   `Date_rated` date default NULL,
  5.   etc..
  6.    ...
  7.  
  8.   PRIMARY KEY  (`ReviewerID`,`PaperID`),
  9.   KEY `PaperID` (`PaperID`)
  10. ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
  11.  
  12. ALTER TABLE `reviewer_paper`
  13.   ADD CONSTRAINT `reviewer_paper_ibfk_1` FOREIGN KEY (`PaperID`) REFERENCES `papers` (`PaperID`) ON DELETE CASCADE ON UPDATE CASCADE;
  14.  
  15.  
  16.  
but i am not sure if i have done this right..
do i need to remove the index i placed on paperID and just put the primary key
PRIMARY KEY (`ReviewerID`,`PaperID`)

also for the foreign key, do i need to add the foreign keys for reviewerID also, i.e

Expand|Select|Wrap|Line Numbers
  1. ALTER TABLE `reviewer_paper`
  2.   ADD CONSTRAINT `reviewer_paper_ibfk_1` FOREIGN KEY (`ReviewerID`) REFERENCES `reviewer` (`ReviewerID`) ON DELETE CASCADE ON UPDATE CASCADE;
plzzz help
  #2  
Old March 8th, 2009, 05:32 AM
Member
 
Join Date: Jan 2009
Location: USA
Posts: 114

re: foreign key contraints problem


hello -

no problem, once you grasp the concepts it will come to you!

How do you want the relationships?

do you want a one to many for the reviewer_paper and reviewer_Id ?

right now the way it is designed is that only one reviewer_id can look at one reviewer_paper.

that was designed by assigning the reviewer_id on the reviewer_paper table.

to change this re label the column to reviewer_id_fk this will allow multiple reviewers per paper.

hope this helps.

other then that code looks fine.
Reply


Similar Threads
Thread Thread Starter Forum Replies Last Post
adding contraints FK refs to existing schema awebguynow answers 1 April 20th, 2006 07:25 PM
Transactions Not working ?????????? S.Kartikeyan answers 3 November 15th, 2005 10:20 PM
foreign key contraints, on delete cascade not working? Andrew DeFaria answers 5 July 20th, 2005 12:45 AM
foreign key contraints, on delete cascade not working? Andrew DeFaria answers 1 July 20th, 2005 12:45 AM