Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Foreign key relationships with a condition

Good morning,

I have a master table with an ID and a type. Depending of the type, i have children tables using this ID as a foreign key to ensure integrity. Eg. for the master table:

master_ID, type
11, A
12, B
13, A

For the child table named Child_A, which stores additional data for type A ;

Child_A_ID, FK_master_ID, ....
1, 11, ....
2, 13, ....

How can I prevent the type in my master table to be changed to a different value when there is a corresponding record in my child table. My referential integrity is currently kept but it has no sense to store in the Child_A information of type A while, the record in the master table is of different type.

edit:

Would having a foreign key with the 2 attributes (ID and type) and repeating the type in each child tables be the only solution? Eg. for the child_A table;

Child_A_ID, FK_master_ID, type, ....
1, 11, A, ....
2, 13, A, ....

Hope it's clear enough.

like image 682
Sylvain C. Avatar asked May 04 '15 04:05

Sylvain C.


People also ask

What is foreign key relationships?

A foreign key relationship is defined between the Orders table and the Customers table to ensure that an order can't be created unless there is a corresponding customer. A foreign key relationship between the Orders table and the Products table ensures that an order can't be created for a product that doesn't exist.

How do you identify foreign key relationships?

Using SQL Server Management Studio Open the Table Designer for the table containing the foreign key you want to view, right-click in the Table Designer, and choose Relationships from the shortcut menu. In the Foreign Key Relationships dialog box, select the relationship with properties you want to view.

What is the purpose of defining a foreign key in a relation?

Foreign keys put the “relational” in “relational database” – they help define the relationship between tables. They allow developers to maintain referential integrity across their database.

How do I fix foreign key constraint failure?

The error message itself showing there is a foreign key constraint error, which means you are deleting a parent table where the child table contains the Primary table identifier as a foreign key. To avoid this error, you need to delete child table records first and after that the parent table record.


1 Answers

You can create a check constraint that uses a user defined function to determine if the id value is contained in the relevant type table.

ALTER TABLE MasterTable
  ADD CONSTRAINT CHK_MasterTable_Type
  CHECK(dbo.fn_check_IdBelongsToType(master_ID, type) = 1)

and in the function itself you do something like this:

CREATE FUNCTION fn_check_IdBelongsToType (
    @master_ID int, 
    @type char(1)
)
RETURNS int
AS
BEGIN
   IF @Type = 'A' AND EXISTS (
       SELECT 1
       FROM Child_A
       WHERE FK_master_ID = @master_ID 
   ) RETURN 1

   IF @Type = 'B' AND EXISTS (
       SELECT 1
       FROM Child_B
       WHERE FK_master_ID = @master_ID 
   ) RETURN 1

   IF @Type = 'C' AND EXISTS (
       SELECT 1
       FROM Child_C
       WHERE FK_master_ID = @master_ID 
   ) RETURN 1

   -- after testing all child tables, return 0 to indicate that the value was not found
   RETURN 0

END
like image 53
Zohar Peled Avatar answered Sep 27 '22 23:09

Zohar Peled