1

I have started working on a new database and found something that stands out as abnormal.

I have a table that holds contacts similar to below

contacts
+----+-------------+-------------+----------------------+
| id | first_name  | second_name |    email_address     |
+----+-------------+-------------+----------------------+
|  1 | jane        | Doe         | [email protected] |
|  2 | John        | Doe         | [email protected] |
+----+-------------+-------------+----------------------+

Each user has access to specific users on the website. The current way our system handles this is to duplicate the table and append the users id to the end of the table name, it then only inserts records relevant to the user.

contacts_201
+----+-------------+-------------+----------------------+
| id | first_name  | second_name |    email_address     |
+----+-------------+-------------+----------------------+
|  1 | jane        | Doe         | [email protected] |
+----+-------------+-------------+----------------------+

I always thought in situations like this a link table would be used like this to resolve what is a many to many relationship.

contactMemLink
+------------+--------+
| contact_id | mem_id |
+------------+--------+
|          1 |    201 |
+------------+--------+

Extra information:

  • There are around 24000 records in the contact table
  • There are around 100 users on the website
  • Each user has the possibility of having every user assigned to it.

    My Question

Is there any reason on why the database would have been designed this way based on best practices?

1
  • 4
    2.5kk records in joining table is not a problem. Whereas a table per user is a problem itself.
    – Akina
    Dec 5, 2019 at 16:23

1 Answer 1

2

IMHO

The original layout looks like it is a self-built implementation of Row Level Security (RLS). RLS allows the developer to limit the available rows based on who is logged in (and other pieces of information).

The reason for the original design could have been done because:

  1. The designer was unaware of RLS
  2. The RDBMS did not support RLS
  3. DBA Stack Exchange wasn't available to the developer at the time of implementation
  4. The developer copy+pasted code unaware of the potential problems
    • constructing the SQL statements for contacts_201 screams "Possible SQL Injection"

The solution for (2) would be based on these words of wisdom:

use the right tool for the job.

In other words, the RDBMS (that doesn't support RLS) should have been changed to an RDBS that supports the Business Requirement (ie one that does support RLS). This could be a simple as doing an upgrade.

note "Database Agnostic Code is a Myth" could have been the major reason to not fix (2).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.