Antipattern: Assume You Must Use Files

Conceptually, an image is an attribute in a table. For example, the Accounts table may have a portrait_image column.

​ ​CREATE​ ​TABLE​ Accounts (
​  account_id ​SERIAL​ ​PRIMARY​ ​KEY​,
​  account_name ​VARCHAR​(20),
​  portrait_image ​BLOB​
​ );

Likewise, you can store multiple images of the same type in a dependent table. For example, a bug may have multiple screenshots that illustrate it.

​ ​CREATE​ ​TABLE​ Screenshots (
​  image_id ​SERIAL​ ​NOT​ ​NULL​,
​  bug_id ​BIGINT​ ​UNSIGNED​ ​NOT​ ​NULL​,
​  screenshot_image ​BLOB​,
​  caption ...

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.