MySQL Foreign Key – Can a Column Be Referenced from Multiple Tables?

database-designforeign keyMySQL

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?

Many association in a single fieldstrong text

Best Answer

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.