473,386 Members | 1,997 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

foreign key contraints problem

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
Feb 28 '09 #1
1 2119
wizardry
201 100+
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.
Mar 8 '09 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Jeremiah Jacks | last post by:
I just upgraded to MySQL 4.0.14-standard for RedHat Linux and am using = the pre-compiled binaries. I have a database with INNODB tables. When I insert a row into one of the child tables, I get...
1
by: Andrew DeFaria | last post by:
I created the following .sql file to demonstrate a problem I'm having. According to the manual: If |ON DELETE CASCADE| is specified, and a row in the parent table is deleted, then InnoDB...
2
by: Gunnar Vøyenli | last post by:
Hi! For the sake of simplicity, I have three tables, Employee, Department and Work Employee >---- Department \ / \ / ^ ^ Work
10
by: Bodza Bodza | last post by:
I'm having an argument with an incumbent self-taught programmer that it is OK to use null foreign keys in database design. My take is the whole point of a foreign key is that it's not supposed...
0
by: Sajid | last post by:
Hi, I am trying to read the constrains over any database. Iam using DataAdapter.MissingSchemaAction and DataAdapter.FillSchema methods to get it and is sucessful to read the primary key, unique...
0
by: Scott Ribe | last post by:
I've got a problem which I think may be a bug in Postgres, but I wonder if I'm missing something. Two tables, A & B have foreign key relations to each other. A 3rd table C, inherits from A. A...
10
by: Shawn Chisholm | last post by:
Hi, I am trying to deal with a deadlock situation caused by foreign key references on insert and I was wondering if anyone knows what order the foreign keys are locked (or evaluated) in for a...
2
by: Ian Davies | last post by:
I have created a database with about 17 tables. I have been creating foreign keys some of which have worked but when creating others I get the message below ************************* 1005...
2
by: paragpdoke | last post by:
Hello Everyone. I'm new to XML and was trying to get my first DTD configured. I could not find an example of a constraint on the value of a node using the DTD. I'm trying to build a very simple...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.