Relational database design multiple user types

Your case looks like an instance of class/subclass.

There are two classic ways to design SQL tables to deal with subclasses. Each has advantages and disadvantages.

One way is called “Single Table Inheritance”. In this design there is just one table for all types of users. If a given column doesn’t pertain to a given row, the intersection is left NULL. A column can be added to indicate the user type.

Another way is called “Class Table Inheritance”. This is much like the answer Nanego gave, with a few minor changes. There is one table for users, with all the common data, and a id field. There is one table for each subclass, with data that pertains to that subclass. The id field is often set up as a copy of the id field in the matching row back in the users table. This way the subclass key can do double duty, acting as both a primary key and as a foreign key referencing the user table. This last technique is called “Shared Primary Key”. It requires a little programming at insert time, but it’s well worth it. It enforces the one-to one nature of the relationship, and it speeds up the necessary joins.

You can look up all three of these designs as tags in SO or as articles out on the web.



Leave a Comment