Home >Database >Mysql Tutorial >When Is Using `SELECT *` in SQL Queries Justifiable?

When Is Using `SELECT *` in SQL Queries Justifiable?

Barbara Streisand
Barbara StreisandOriginal
2024-12-30 08:58:12848browse

When Is Using `SELECT *` in SQL Queries Justifiable?

Can Select * Usage Ever Be Justified?

The use of "SELECT *" in SQL queries has long been a subject of debate among developers. While some strictly oppose its usage, others believe it can be justified in certain scenarios.

Potential Use Cases:

Despite its general discouragement, "SELECT *" may be permissible in limited situations:

  • Audit Triggers: In audit triggers, selecting all columns ensures that any new columns added to the base table will automatically be audited, prompting necessary updates to the audit trigger and table structure.
  • Derived Tables and Column Table Expressions: When creating derived tables or column table expressions, using "SELECT *" can be more concise and readable. The optimizer can recognize that only specific columns are required, avoiding unnecessary overhead. However, this practice should be handled carefully, as views in SQL Server may not automatically update their column metadata when underlying tables change. In such cases, "sp_refreshview" must be executed to avoid incorrect results.

Caution:

It's important to use "SELECT *" judiciously and only when there's a clear justification for its use. In production code, its application should be restricted to specific scenarios where it provides a benefit and avoids potential pitfalls.

The above is the detailed content of When Is Using `SELECT *` in SQL Queries Justifiable?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn