Antipattern: Create Multiple Columns
Explore the problems caused by creating multiple columns to store multiple values in SQL attributes. Understand how this antipattern complicates searching, updating, and maintaining data. Learn the challenges with query complexity, ensuring uniqueness, handling nulls, and expanding columns. Gain insights into why storing single values per column is crucial for effective, scalable database design and how to avoid this common pitfall.
We'll cover the following...
We still have to account for multiple values in the attribute, but we know that the new solution must store only a single value in each column. It may seem natural to create multiple columns in this table, each containing a single tag.
As we assign tags to a given bug, we’d put values in one of these three columns. Unused columns would remain null.
Let’s try to retrieve the Bugs table data after updating the value of tag2. Press “RUN” in the following playground and see the output.
Tasks we could do easily with a normal attribute are now more complex.
Searching for values
When searching for bugs with a given tag, we must search all three columns because the tag string could occupy any of these columns.
If we’re searching for a given value across multiple columns, it is a clue that the multiple columns should be stored as a single logical attribute.
For example, to retrieve bugs that reference ...