Antipattern: Specify Values in the Column Definition

Many people choose to specify the valid data values when they define the column. The column definition is part of the metadata—the definition of the table structure itself.

For example, you could define a check constraint on the column. This constraint disallows any insert or update that would make the constraint false.

​ ​CREATE​ ​TABLE​ Bugs (
​  ​-- other columns​
​  ​status​ ​VARCHAR​(20) ​CHECK​ (​status​ ​IN​ (​'NEW'​, ​'IN PROGRESS'​, ​'FIXED'​))
​ );

MySQL supports a nonstandard data type called ENUM that restricts the column to a specific set of values.

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.