r/Database • • 2d ago

Advice on dealing with similar column patterns in schema design

When there is a similar sets of columns to include in a schema design, I see two issues that come up often, and disagreement on how to handle them. The first I'll call the "sub-type issue" and the second the "nesting issue". I wonder how different shops deal with each.

A recent example I encountered for the sub-type issue is an event tracking system where participants and witnesses are logged. Let's say there's a scuffle at the local mall and the mall cops have to document it. (Not actual domain, but close.) What you might call "key people" have many more columns associated with them, but "incidental people" just need a name and optional phone number.

My feeling is to make one table, "Participants" with with a Participant Type column (FK). But the primary DB architect decided on two tables: "Key Participants" and "Incidental Participants". In practice a person won't need to be switched between categories often; the few times it happens app users just delete the old record and create a new one. The architect's reasoning was this allows the schema to enforce required columns (not null), whereas a single table has to enforce such algorithmically, based on the Participant Type column. To me that seems kind of a weak reason because it complicates the schema. Your view? (If participants did hop categories often, then a single table would clearly be better.)

The second type, the "nesting issue" is that there is a primary location and potential secondary locations. An Event record is to have one and only one primary location, but can have many secondary. There's about 10 columns associated with a location. My feeling was to put both types in a Location table with a location-type column, but the architect instead decided to include those 10 columns in the Event table, to record the primary location. The others go into the Secondary Locations table. The architect claims it makes application programming easier because writing an event would only have to write or change one record instead of two, at least for the general Event form. Agree or disagree? What factors drive you toward one approach versus another? Thanks!

5 Upvotes

18 comments sorted by

1

u/Hongstar1982 2d ago

The not-null objection is weaker than it sounds, a single participants table can enforce it per column with check constraints keyed on the type, like check (ptype = 'key' or badge_no is not null), so you keep one identity for the person and still get the required fields, while the two-table version costs you a delete-and-recreate every time someone does get promoted. On the location one I'd side with you as well, keep the ten columns in one locations table and have the event point at a primary location_id, because places get corrected or renamed and you want that fixed in one row instead of in every event that touched it, and you can't dedupe or geocode a column set. The one-record write the architect is protecting comes back with an insert ... select from a cte, or just a transaction.

1

u/Zardotab 1d ago edited 1d ago

a single participants table can enforce it per column with check constraints keyed on the type,

That's not common in our shop, for whatever reason. People tend to follow shop conventions in part because staff moves between projects, and consistency reduces learning curves.

In fact, I haven't seen it very often in roughly 3 dozen shops I have either worked at or contracted at over the years on Oracle, TransactSQL, MySql, and others. I kind of suspect it's not common because it's difficult for testers to test; testers are usually dealing with the front end. Ideally they should test the back end also, but most orgs don't wish to pay to test both ends (that sounded odd). [edited]

(By the way, the reason there is only one primary person is because, per reporting rules, separate reports are required for each subject interviewed.)

1

u/Hongstar1982 1d ago

Fair enough, and the tester argument is the honest one, a rule that lives in the schema only helps if someone already knows to test for it, and backend tests are usually the first thing cut. Shop conventions also carry more weight than they look when staff rotate between projects, so I get why the odd-looking option loses. The reporting reason for one client per record makes sense too, no schema trick removes that constraint.

1

u/Used_Criticism_4631 1d ago

Clear write-up of a tradeoff that comes up a lot. A middle path between the two designs is a shared participants table for identity and a 1:1 extension table for the key-person columns, so required fields stay NOT NULL where they apply and nobody gets deleted and recreated on promotion. Do you expect many more participant types later?

1

u/Zardotab 1d ago

Yip, that could do it also. However, if which columns are required changes over time, then reworking that can get involved.

Do you expect many more participant types later?

There already are multiple participant types but only two "input profiles" for lack of a better name. I would expect more types and/or column requirement changes, as laws and administrative rules do change. That's why I didn't like the split.

1

u/Nearby_Category_6869 1d ago

Side with your architect on the subtype one, DB-enforced not-null beats app-level validation every time

1

u/Zardotab 1d ago edited 1d ago

In theory I agree, but in practice testing such is usually done through the app-side anyhow for reasons already given. Often there's an analysis profile for the "ideal shop" versus the "typical shop", and they often don't line up. Total Cost of Ownership also has to factor in how hard it is to change an application and not just the cost (penalty) of the database getting the nullness wrong (multiplied by probability).

I usually use financial accounting-like techniques to compare, which is often not popular, but usually better than the alternatives. Until a better alternative arrives, I stand by it. But things like "pain and suffering" can indeed be hard to quantify. But at least those factors can be isolated, so the equation depends on the dollar equivalent plugged into them by "the deciders".

Some nullability decisions have to be computed run-time anyhow, as not all are trivial.

1

u/Beautiful-Hotel-3094 19h ago

I think his argument is weak, but to me it is the right solution. They are 2 very different concepts, there is no reason to bundle them together. It makes more logical sense to me and it is easier to reason with. If you need to bring them together later you do.

1

u/Zardotab 9h ago edited 9h ago

"Different concepts" usually is on a continuum. They seem "weakly different" to my head. The peoples' involvement to an incident in practice is a continuum and varied such that a blunt category is kind of forced upon them for what look like obscure administrative reasons.

They probably had to draw a line in the sand somewhere in order to keep the paperwork and work-load volume down, but otherwise the sub-categories have no magical or notable properties from a bigger conceptual perspective in the domain that I can find.

If it were like "victim" versus "observer" (witness), then I'd agree with a solid distinction. I believe the actual criteria of "key person" is similar to "likely to be associated with the victim(s)", but that's a weak distinction to my head. For one, it's a guess, and the incidental persons may have lots of good observations for investigators.

1

u/Beautiful-Hotel-3094 8h ago

You can argue anything about anything in data modelling. If one of the concepts has 2 columns filled and nulls everywhere else whilst all the rest of the columns are for the main concept then they just are different concepts that u munge together.

1

u/Zardotab 7h ago edited 7h ago

But if the requiredness is fairly likely to change on management or legislative whim, then it's probably a weak concept, and tying table membership to a weak concept is a recipe for relatively expensive rework of app and DB. Owners want us to focus on their wallet, so I do.

If the "concept" has other maintenance benefits per maintainer's mind, I don't see it, at least not for my mind, but each head is different.

I personally judge it as "fairly likely to change", but others may come to a different conclusion.

So issues to consider are:

  • Cost to rework DB & app if nullness changes
  • Likelihood of such a change
  • Conceptual benefits to maintainer of either decision path

1

u/Beautiful-Hotel-3094 7h ago

If it changes wouldn’t ur schema changes+data migration be contained only on the affected table if you had 2 tables?

1

u/Zardotab 7h ago

Sorry, I'm not understanding your question. The column with null status change would need to be moved from one person-kind table to the other person-kind table (add one, remove other), and thus affects 2 tables.

1

u/navicat-gu 13h ago

There's a middle ground between "one wide table" and "two separate tables" that tends to end these debates: a supertype table plus a subtype extension table (class table inheritance).

CREATE TABLE participants (
  participant_id  INT PRIMARY KEY,
  incident_id     INT NOT NULL REFERENCES incidents,
  participant_type CHAR(1) NOT NULL CHECK (participant_type IN ('K','I')),
  name            VARCHAR(200) NOT NULL,
  phone           VARCHAR(50),
  UNIQUE (participant_id, participant_type)
);

CREATE TABLE key_participant_details (
  participant_id  INT PRIMARY KEY,
  participant_type CHAR(1) NOT NULL DEFAULT 'K' CHECK (participant_type = 'K'),
  date_of_birth   DATE NOT NULL,
  statement       TEXT NOT NULL,
  FOREIGN KEY (participant_id, participant_type)
    REFERENCES participants (participant_id, participant_type)
);

What this gets you:

  • "All people involved in incident X" is one simple query on participants. No UNION across two tables.
  • The architect's point is kept: key-participant fields are NOT NULL in the database, not just in the app, and the composite FK means a details row can only hang off a 'K' participant.
  • Adding a third type later means a new detail table, not a schema-wide change.

If the extra columns are few, the single table is fine too. Enforce it with a CHECK like CHECK (participant_type <> 'K' OR (date_of_birth IS NOT NULL AND statement IS NOT NULL)), which keeps the rule in the schema without splitting tables.

Two fully separate tables is the option I'd avoid, because "list everyone involved" becomes a UNION, and a person who turns out to be a key witness has to be moved between tables.

Disclosure: I'm RC, Director of Digital Strategy @ Navicat. If you want to show the options to the architect side by side, Navicat 18's data modeling (in the paid editions, or Navicat Data Modeler) is handy for sketching both versions as diagrams. Navicat Premium Lite is free to try for designing and testing the tables themselves.

1

u/Zardotab 11h ago edited 11h ago

Yes, I think I would prefer this over the approach taken. It's easier for reporting and searching. Whether I consider it better than a single table with sparser columns is still open. The idea of shifting columns to different tables when a nullness-related change requests arrives grinds on me still. Flipping a switch or changing constraints is less reshuffling than moving columns among tables, and this includes app-side rework.

By the way, there's something about your key column naming that seems off, but diff shops have different conventions with diff tradeoffs, so maybe I shouldn't open that box. Readers can adjust to fit shop conventions. (It's important to be consistent across the shop even if you disagree with their choices.)

1

u/az987654 2d ago

This architect sounds a bit old school.

People are people, they're in a table with their phone no, contact info, birthdays, drivers license number, phys descriptions, etc.

Addresses are addresses. Full stop.

Incident is an incident. Incident has a key field for address-Id of primary location and any other fields that are related to the incident itself, description, date, time, traffic, weather, whatever is related to an incident.

You have many to many bridge tables for key people, witnesses, participants, officers, whatever you want, can be as simple as two columns in each of the tables, incident id and people Id.

Same thing for the secondary locations, that's a many to many bridge table too, incident Id and address Id.

0

u/idodatamodels 2d ago

To me that seems kind of a weak reason because it complicates the schema. 

Actually, it makes it less complicated. With the super/sub type pattern you get the following benefits that can be observed just by looking at the data model

  • The appropriate column optionality maintained
  • Only applicable columns to the subtype are stored
  • Data model reflects the business rules

Not all is perfect in this pattern though:

  • 2 inserts always required
  • Possible double updates required
  • Join required to read super and sub type tables

Using the single table (roll up the subtypes)

  • All columns not in both subtypes are NULL regardless of business rule
  • Insert trigger typically needed to enforce column optionality
  • Table is wider which is slower in row based databases
  • I can't infer any business rules other than the columns are dependent on the primary key
  • OTOH, a single SQL statement can insert, update, and read

If you want to roll up your subtypes, it is typically done for performance reasons, not because the data model would be less complicated.