Antipattern: Create Multiple Columns

You still have to account for multiple values in the attribute, but the new solution must store only a single value in each column. It might seem natural to create multiple columns in this table, each containing a single tag.

​ ​CREATE​ ​TABLE​ Bugs (
​  bug_id ​SERIAL​ ​PRIMARY​ ​KEY​,
​  description ​VARCHAR​(1000),
​  tag1 ​VARCHAR​(20),
​  tag2 ​VARCHAR​(20),
​  tag3 ​VARCHAR​(20)
​ );

As you assign tags to a given bug, you’d put values in one of these three columns. Unused columns remain null.

​ ​UPDATE​ Bugs ​SET​ tag2 = ​'performance'​ ​WHERE​ bug_id = 3456;

bug_id

Get SQL Antipatterns, Volume 1 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.