Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do these database design styles (or anti-pattern) have names?

Consider a database with tables Products and Employees. There is a new requirement to model current product managers, being the sole employee responsible for a product, noting that some products are simple or mature enough to require no product manager. That is, each product can have zero or one product manager.

Approach 1: alter table Product to add a new NULLable column product_manager_employee_ID so that a product with no product manager is modelled by the NULL value.

Approach 2: create a new table ProductManagers with non-NULLable columns product_ID and employee_ID, with a unique constraint on product_ID, so that a product with no product manager is modelled by the absence of a row in this table.

There are other approaches but these are the two I seem to encounter most often.

Assuming these are both legitimate design choices (as I'm inclined to believe) and merely represent differing styles, do they have names? I prefer approach 2 and find it hard to convey the difference in style to someone who prefers approach 1 without employing an actual example (as I have done here!) I'd would be nice if I could say, "I'm prefer the inclination-towards-6NF (or whatever) style myself."

Assuming one of these approaches is in fact an anti-pattern (as I merely suspect may be the case for approach 1 by modelling a relationship between two entities as an attribute of one of those entities) does this anti-pattern have a name?

like image 338
onedaywhen Avatar asked Jun 05 '09 09:06

onedaywhen


People also ask

What is anti-pattern in database?

This antipattern typically occurs because: The database is viewed as a service rather than a repository. An application might use the database server to format data (for example, converting to XML), manipulate string data, or perform complex calculations.

What is an anti-pattern in design patterns?

An anti-pattern is the opposite side of the design pattern. You can also call it design smell which is caused by bad software design. They are usually ineffective or obscure fixes. The existence of an anti-pattern in your code can create a lot of bugs and you may have to fix it later properly.

What is the difference between a pattern and an anti-pattern?

An anti-pattern is the evil twin of a good design pattern. A good design pattern is a reusable requirement, design, or implementation that solves hard problems in a standard way, which is well designed, well documented, easy to maintain, and easy to extend.

What is meant by anti-pattern?

“An anti-pattern is a common response to a recurring problem that is usually ineffective and risks being highly counterproductive.” Note the reference to “a common response.” Anti-patterns are not occasional mistakes, they are common ones, and are nearly always followed with good intentions.


2 Answers

Well the first is nothing more than a one-to-many relationship (one employee to many products). This is sometimes referred to as a O:M relationship (zero to many) because it's optional (not every product has a product manager). Also not every employee is a product manager so its optional on the other side too.

The second is a join table, usually used for a many-to-many relationship. But since one side is only one-to-one (each product is only in the table once) it's really just a convoluted one-to-many relationship.

Personally I prefer the first one but neither is wrong (or bad).

The second would be used for two reasons that come to mind.

  1. You envision the possibility that a product will have more than one manager; or
  2. You want to track the history of who the product manager is for a product. You do this with, say a current_flag column set to 'Y' (or similar) where only one at a time can be current. This is actually a pretty common pattern in database-centric applications.
like image 58
cletus Avatar answered Nov 05 '22 11:11

cletus


It looks to me like the two model different behaviour. In the first example, you can have one product manager per product and one employee can be product manager for more than one product (one to many). The second appears to allow for more than one product manager per product (many to many). This would suggest the two solutions are equally valid in different situations and which one you use would depend on the business rule.

like image 31
1800 INFORMATION Avatar answered Nov 05 '22 11:11

1800 INFORMATION