HiveBrain v1.2.0
Get Started
← Back to all entries
patternsqlMinor

Can a column be referenced from multiple table as a foreign key

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
canreferencedcolumnforeignmultiplefromtablekey

Problem

Suppose I have a table named Service.
But I have other tables that have a similarities with that table
like RoomService, ConsultatonService, etc.

Can the Id of RoomService and ConsultationService keep a foreign key reference to Service table's ServiceId column but being identifiable by the Type column in the Service table?

Solution

Yes, you can do it. Your services table will store all the common attributes between your sub-classes.

If you intend to avoid 1:1 relationships, you'll have to create, for example, an ServiceId column and do the relationship with it, using Id as PK of RoomService. By the way, there is no problems with 1:1 relationships, specially in your case, where you are creating specialized sub-classes.

In addition, the type column will be used by your application to determine which subclass the object is. Also no problem on that.

Context

StackExchange Database Administrators Q#205171, answer score: 4

Revisions (0)

No revisions yet.