Welcome to Dream.In.Code
Become an Expert!

Join 149,986 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,400 people online right now. Registration is fast and FREE... Join Now!




MySQL constraints

 
Reply to this topicStart new topic

MySQL constraints

dedman
8 Dec, 2006 - 08:11 AM
Post #1

D.I.C Head
**

Joined: 22 Apr, 2005
Posts: 52


My Contributions
I have created a table to store phone, fax, pager and email addys.
I have a column for storing a contact_type.
This contact_type can be a PK from any one of 3 tables (customer contact, supplier contact or a customer).
Can I make 3 seperate constraints with contact_type as the FK?

CODE

CONSTRAINT `communications_fk1` FOREIGN KEY (`contact_type`)
REFERENCES  `contacts` (`contact_ID`) ON DELETE CASCADE ON UPDATE CASCADE,

CONSTRAINT `communications_fk2` FOREIGN KEY (`contact_type`)
REFERENCES `customers` (`customer_ID`) ON DELETE CASCADE ON UPDATE CASCADE,

CONSTRAINT `communications_fk3` FOREIGN KEY (`contact_type`)
REFERENCES `suppliers` (`supplier_ID`) ON DELETE CASCADE ON UPDATE CASCADE


Will it throw an error on the first constraint if is not in the contacts table? Or will it check all 3 before throwing an error?

Does this even make sense to do? Or should I just store this within each of the 3 tables?

Thanks for your help!

This post has been edited by dedman: 8 Dec, 2006 - 08:16 AM
User is offlineProfile CardPM
+Quote Post

gregoryH
RE: MySQL Constraints
12 Dec, 2006 - 12:52 AM
Post #2

D.I.C Regular
Group Icon

Joined: 4 Oct, 2006
Posts: 417


Dream Kudos: 50
My Contributions
QUOTE(dedman @ 8 Dec, 2006 - 09:11 AM) *

I have created a table to store phone, fax, pager and email addys.
I have a column for storing a contact_type.
This contact_type can be a PK from any one of 3 tables (customer contact, supplier contact or a customer).
Can I make 3 seperate constraints with contact_type as the FK?

CODE

CONSTRAINT `communications_fk1` FOREIGN KEY (`contact_type`)
REFERENCES  `contacts` (`contact_ID`) ON DELETE CASCADE ON UPDATE CASCADE,

CONSTRAINT `communications_fk2` FOREIGN KEY (`contact_type`)
REFERENCES `customers` (`customer_ID`) ON DELETE CASCADE ON UPDATE CASCADE,

CONSTRAINT `communications_fk3` FOREIGN KEY (`contact_type`)
REFERENCES `suppliers` (`supplier_ID`) ON DELETE CASCADE ON UPDATE CASCADE


Will it throw an error on the first constraint if is not in the contacts table? Or will it check all 3 before throwing an error?

Does this even make sense to do? Or should I just store this within each of the 3 tables?

Thanks for your help!

Hi

Its a littl hard to understand your table structure, but it doesn't make sense to use the structure the way you have done it. The is a risk in this situation:
if a contact is also a customer and a supplier and you decide to delete, you will remove all three contacts.

It appears on the surface that the contacts will need to be managed a different way to the current mapping.
User is offlineProfile CardPM
+Quote Post

dedman
RE: MySQL Constraints
12 Dec, 2006 - 09:42 AM
Post #3

D.I.C Head
**

Joined: 22 Apr, 2005
Posts: 52


My Contributions
I have attached a text file that shows the scripts for the 4 tables concerned. It should clear up any confusion I caused with my orig post. The reference to the contact_ID is for storing people's phone numbers. The reference to supplier_ID and customer_ID is for storing company phone numbers. All contacts whether they are from a supplier or customer are stored in the contacts table.

Attached File  MySQLConstraintQuestion.txt ( 4.45k ) Number of downloads: 109

User is offlineProfile CardPM
+Quote Post

dedman
RE: MySQL Constraints
21 Dec, 2006 - 06:20 PM
Post #4

D.I.C Head
**

Joined: 22 Apr, 2005
Posts: 52


My Contributions
I spoke to a guy today who recommended taking care of this on the front end instead of using constraints in the table. So when we get to that point, I will try that.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 07:35PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month