What is the best practice for querying in banco de dados
, performing a direct query on table
or view
of it? What are the advantages and disadvantages between them?
What is the best practice for querying in banco de dados
, performing a direct query on table
or view
of it? What are the advantages and disadvantages between them?
Views are, roughly speaking, a summary for some query that you'll need to use in different scripts. This makes it easy to maintain, for example, when you need to modify something, it would only change in the view instead of wobbling across all application scripts that use the same query.
If it has floated, let's take a practical example:
SELECT firula FROM tabela_algo
WHERE foo LIKE '%blah';
This is a SQL query that you use in several system scripts
In the query_firula.hph page you have a query that invokes this query
SELECT x, y, z FROM tabela_qualquer WHERE id = 5 AND firula NOT IN (SELECT firula FROM tabela_algo
WHERE foo LIKE '%blah')
So far, okay. No need to create a view.
But suppose you use the same query on another page, otherpage.hph
SELECT x, y, z FROM tabela_diferente WHERE firula IN (SELECT firula FROM tabela_algo
WHERE foo LIKE '%blah')
Now imagine this in 5, 10, 20 different places and you need to modify something in the query. You will have to modify all the pages that depend on this query. It is laborious and at risk of forgetting something, causing bugs in the system.
In this situation, you could simplify by creating a VIEW.
With the view, the above examples would look like:
#exemplo 1
SELECT x, y, z FROM tabela_qualquer WHERE id = 5 AND firula NOT IN (SELECT firula FROM tabela_view)
#exemplo 2
SELECT x, y, z FROM tabela_diferente WHERE firula IN (SELECT firula FROM tabela_view)
For a deeper understanding, see this link: What are views in SQL? What are the advantages and disadvantages to using?
The above examples are purely didactic .. dãhnn ~~~