Solved

Optional Foreign Key Constraints?

Posted on 2013-12-31
2
358 Views
Last Modified: 2014-01-07
There are three tables: bugs, comments and attachments.

A comment is always associated with a bug. (bugs have comments).

An attachment will always be associated with a comment (attachments belong to specific comments)

BUT a comment will not always have an attachment. (Like cookies, attachments are a sometimes food).

How do I setup the foreign key constraints here?

Pretty sure this won't work because if I post a comment without an attachment, it will fail...

CONSTRAINT `fk_comments_bugs1`
    FOREIGN KEY (`comments_bug`)
    REFERENCES `bugreport`.`bugs` (`bugs_id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_comments_attachments1`
    FOREIGN KEY (`comments_attachment`)
    REFERENCES `bugreport`.`attachments` (`attachments_id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)

Open in new window

0
Comment
Question by:DrDamnit
2 Comments
 
LVL 12

Expert Comment

by:North2Alaska
ID: 39748080
No, the FK is to prevent you from entering an attachment without a comment.  You should have no problem creating a comment without an attachment.
0
 
LVL 45

Accepted Solution

by:
Kdo earned 500 total points
ID: 39748114
Hi DrDamnit,

It's tough to tell from your example, but it sounds like the comments table has a column that contains the attachment ID.  That would be upside down.  The hierarchy should be:

--  Bug Table
--  Comment Table, with the ID of the associated bug.
--  Attachment Table, with the ID of the associated Comment.

That makes both comments and attachments optional, and allows for multiple comments and/or attachments per bug.


Good Luck,
Kent
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now