Home / Learn Secure SQL / Dynamic Table and Column Names in Practice

Dynamic Table and Column Names in Practice

Multi-tenant tables, pivoted reports, and admin "pick a column" features.

In shortMulti-tenant tables, pivoted reports, and admin "pick a column" features.

What's actually going on

A safer design for "pick a column"user requests "price"looked up in a fixedmap of allowed…map returns thetrusted, exact colum…that trusted name isused in the query
A safer design for "pick a column"

What it is

Some real features genuinely need to choose a table or column at runtime: a report where the user picks which column to show, or a system with one table per tenant.

Why it matters

These are exactly the features most likely to tempt a developer into string-joining a name directly from user input, because there seems to be no other way.

Mechanically speaking

Map the requested name to a known-safe value through a lookup (a dictionary/hash map in your code, or a database table of permitted names), and only ever use the looked-up, trusted value in the query, never the raw request.

Shows up in

  • Web application code review
  • Backend development
  • Database administration

Try it

Looking up a trusted column nameSQL: edit and run

The GROUP BY column came from a lookup table in the application code, not directly from the request.

Check yourself

1. For "pick a column to group by" features, the safe approach is...