I have a database design and I'm having trouble with two tables. I have a Users table, and a Properties table. The Properties table may only have 0, 1, or 2 users, let's call them owner and tenant, and only one of each (per property), and a User may be an owner or a tenant in many Properties.
So, here are my two options according to the case:
- Have two foreign keys in the Properties table (since the maximum of users referenced by a property are 2 and not N), called owner_id and tenant_id which reference the Users table.
- Have a N:M intermediate table, with the user_id and property_id, along with another column to indicate the type of user in that property (say boolean 1 = owner, 0 = tenant).
Which is the best design option?
Clarifications:
- A user has a name and email, and a property a street and its number as attributes.
- A user may be only an owner or tenant in a property, not both, since an owner implies he owns the property (wether he lives there or not), and a tenant is another user actually living there, so there needs to exist an owner before a tenant is added.
- A property may have no users assigned.
Best Answer
Business rules
So, (1) after we engaged in some deliberations via comments and chat, and (2) once you discussed the possibilities with your customer and colleagues, it has been defined that, in fact:
Therefore —in your business context— among other aspects, there are two distinct conceptual-level associations (or relationships) of cardinality many-to-many (M:N) between the entity types Property and User.
Illustrative IDEF1X diagram
Consequently, I have created an IDEF1X1 diagram that consolidates the business rules formulated above, which is shown in Figure 1:
As demonstrated, every entity type —be it associative or independent— is portrayed by means of its individual box.
I depicted (a)
ownership
and (b)tenancy
as two different associative entity types since, although very similar, they represent the prototypes of two distinct things of interest in you business domain, so it is very convenient to manage them separately because, e.g.:user
is thetenant
orowner
of aproperty
by way of true or false attributes —named, say,is_owner
andis_tenant
—,you might as well be interested in
ownership
andtenancy
instances where registered in the system, thus establishing a time-related attribute —entitled, say,created_datetime
— for said entity types would be very advantageous.In the future, one of these entity types may entail keeping track of certain attributes that do not apply to the other, hence administering them discretely yields benefits from the beginning, making up a more extendable and versatile conceptual schema.
It is worth to mention that, in the diagram, the
user_id
attribute migrates from (i) theuser
entity type to (ii)ownership
and (ii)tenancy
asowner_id
andtenant_id
respectively —both quite descriptive role names that you provided in the question—.Expository logical SQL-DDL design
Then, based on the IDEF1X diagram detailed above, I wrote the expository DDL layout that is declared as follows:
Hence, in such logical-level design:
All of this in agreement with the stipulations by Dr. Edgar Frank Codd in his relational model (as far as the capabilities of the Structured Query Language permit at the time of writing).
Of course, with this logical layout you can still, e.g., derive in BOOLEAN form whether a given User is the Owner of a particular Property or not, with the aid of a SELECT operation that produces a scalar value that evaluates to either 'True' or 'False', which can be accessed from one or more (present or future) application programs sharing this database.
Covering the initial business rules with the same logical-level arrangement
These aspects are now mostly anecdotical, but some of the initial business rules were:
And they can be coped with using the same logical design previously discussed declaring the
property_id
column as an ALTERNATE KEY in both theownership
and thetenancy
tables, via two more constraints, i.e.:This shows other facet (that can be of help for a future visitor) of the versatility offered by the arrangement proposed in this answer.
Endnote
1 Integration Definition for Information Modeling (IDEF1X) is a highly recommendable data modeling technique that was established as a standard in december 1993 by the United States National Institute of Standards and Technology (NIST). It is solidly based on (a) some of the theoretical works authored by the originator of the relational model, i.e., Dr. E. F. Codd; on (b) the entity-relationship view, developed by Dr. P. P. Chen; and also on (c) the Logical Database Design Technique, created by Robert G. Brown.