Preferring Database Normalization vs. Schema Transparency?

A new requirement has surfaced on an old codebase, that basically enables direct (internal) communication between two formerly not-directly related classes of users (stored in different tables with completely different schema and, sadly, the code is barely OO-aware, much less designed, so there’s no parent class). Since we’re out to hang a bag on this old setup that never considered this functionality, there’s no guarantee that there are no PK collisions — given the dataset in use, it’s practically guaranteed that there ARE.

So, the solution seems obvious: kill it with fire and rewrite the whole mess A mapping table. I’ve gotten two directions for the possible ways to implement the map, but I’m not a DBA, so I’m uncertain if there are any pros and cons I’ve missed.

For the sake of clarifying the abstraction, consider three groups of disparate user data: Professors, Administration, Students (No, this isn’t a homework assignment. Promise!)

Mapping 1

(professor_id, admin_id, and student_id are foreign keys to their respective tables)

| mailing_id (KEY) | professor_id | admin_id | student_id | 
-------------------------------------------------------
| 1001             |     NULL     |    87    |  NULL      |
| 1002             |     123      |   NULL   |  NULL      |
| 1003             |     NULL     |   NULL   |  123       |

The +/- to this approach seem pretty heavy on the cons:

  • Two “wasted” fields per row
  • Violates 2NF
  • Vulnerable to insert/update anomalies (a row with only 0-1 field set NULL, e.g.)

The pros are not without their own merits, though:

  • The mapping can be accomplished with a single lookup
  • Easily determine the “source” data for a given user from the mailing_id

Truth be told, in my gut, I don’t like this idea at all.

Mapping 2

(assume MSG_* are defined constants, enum types, or another suitable identifier)

| mailing_id (KEY)  | user_type (UNIQUE1) | internal_id (UNIQUE2)| 
------------------------------------------------------------------
| 1001              | MSG_ADMIN          | 87                    |
| 1002              | MSG_PROF           | 123                   |
| 1003              | MSG_STUDENT        | 123                   |

With this setup, and a unique composite index of {user_type, internal_id} things become much cleaner, 3NF is maintained, and the application code doesn’t have to check for I/U anomalies.

On the downside, there’s a bit of a loss of transparency in determining the user source tables that has to be handled outside of the DB, basically amounting to an application-level mapping of user_type values to tables. Right now, I’m (rather strongly) leaning toward this 2nd mapping, since the downside is rather minor.

BUT I’m painfully aware of my own limitations, and am sure I’ve probably missed advantages or stumbling blocks in both directions, so I turn to wiser minds than mine.

5

Your second idea is the correct one. This approach lets you do all of the mapping that you need to do in order to integrate your three colliding key spaces.

Importantly, it lets the database impose most of the consistency that you need to have using declarative constraints.

You already have more code than you want to have, so don’t add more code than absolutely necessary to keep your integrated key list consistent. Let your database engine do what it was built to do.

The “problem child” that is giving you discomfort in Mapping 2 is the USER_TYPE column. This column is important because you need it to ensure that INTERNAL_ID only appears at most once per user type. The only time you need any code that is even aware of USER_TYPE is the code that inserts and deletes from your mapping table. This can be localized pretty well. I would assume that you’ll create a single point in your code where the mapping table content is maintained. An extra column in this one spot where data is written is not a big deal. What you really want to avoid is adding the extra column everywhere the data is read.

The code in your sub-applications that needs to use the mapping can be blissfully ignorant of the USER_TYPE simply by giving each sub-application a view that filters the mappings down to the one application-specific user type.

From experience my recommendation is to choose consistency over elegance or ‘best practice’. That is to match the existing design and go with THREE mailing tables (one for each role) with a simple mailing_id, user_id field structure.

It’s inelegant but it has a few advantages…

  1. Matching the existing structure will be easier for anyone
    else who will work on this schema before it’s put out to pasture.
  2. You have no wasted fields and you’re not asking the db to match up things
    that won’t exist.
  3. Because each table will only to one other and it’ll be relatively easy to make
    a view which ties up all the data for your routines to use.

I’m sure many others will disagree with this approach, but primary aims of normalisation and best practices are to make code more consistent so it’s easier to follow and debug… and obviously bringing the whole codebase up to scratch is probably not feasible.

3

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật