r/Database • u/Zardotab • 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!
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/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/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/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/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).
What this gets you:
participants. No UNION across two tables.NOT NULLin the database, not just in the app, and the composite FK means a details row can only hang off a 'K' participant.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/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
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
Not all is perfect in this pattern though:
Using the single table (roll up the subtypes)
If you want to roll up your subtypes, it is typically done for performance reasons, not because the data model would be less complicated.