Solved

Optional Foreign Key Constraints?

Posted on 2013-12-31
2
361 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:
Kent Olsen 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Why will this INSERT statement not insert the date? 2 21
MVC - procedural PHP 10 38
check mysql insert 12 27
Could you point a way to eliminate an array unexpected element? 8 24
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 Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

828 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