-3

Is there a standard for how data is represented in a Saved View where a timestamp/voltatile expression is used? For example, let's say I have the SQL query:

SELECT person_id, name, NOW() as now FROM table

The NOW() function will have a different value every time it is run. When saving the above to a view, is the data stored as:

person_id          name             now
1                  Bob              2020-01-01T01:01:01
2                  Billy            2020-01-01T01:01:01
3                  Barbara          2020-01-01T01:01:01

Or is it stored as:

person_id          name             now
1                  Bob              =NOW()
2                  Billy            =NOW()
3                  Barbara          =NOW()

Or, is it dependent on the database implementation and there is no standard?

Shadow
  • 33,525
  • 10
  • 51
  • 64
David542
  • 104,438
  • 178
  • 489
  • 842
  • 4
    A view is not "saved", it's just a Select. Why don't you simply try it? – dnoeth Oct 18 '20 at 20:42
  • @dnoeth yes I tried a table and a view and the table is stored permanently and the view is calculated on-demand. What about a materialized view then? – David542 Oct 18 '20 at 20:44
  • Which dbms are you using? – jarlh Oct 18 '20 at 20:49
  • @jarlh for me it's more a conceptual question, but for testing it I was using mysql (which doesn't have a materialized view) – David542 Oct 18 '20 at 20:52
  • You store values, e.g. a timestamp, in a table (using INSERT/UPDATE). When you SELECT, you will get the value as it was stored. You can also select a function (e.g. NOW()), and you'll get the current timestamp. A view is just a pre-defined select. A materialized view is a optimization trick where the view-select is prepared and stored for faster access - but you will still get the result as the select. – jarlh Oct 18 '20 at 20:56
  • @jarlh could you please clarify the part about `A materialized view is a optimization trick where the view-select is prepared and stored for faster access`. Isn't a view then more like a saved query and a materialized view more like a table (with some maintenance done behind the scenes?) – David542 Oct 18 '20 at 20:59
  • @David542 "Isn't a view then more like a saved query and a materialized view more like a table (with some maintenance done behind the scenes?)" - No, a view is just a predefined SELECT query on a specified table. A materialized view is the exact same query but instead of requesting it everytime, it is cached for faster access. – dmuensterer Oct 18 '20 at 22:01
  • @dmuensterer cached = saved to some sort of storage, no? – David542 Oct 18 '20 at 22:18

2 Answers2

2

I can see how this would be misleading. However, a view does not "store" data (except for materialized views, which are a different store).

A view is a query which is substituted into the query where it is used. Admittedly, it could be parsed in advance, saving a small amount of effort.

However, a function such as now() is called when a query is executed. So, the value you will see is the value when the query is run.

This is actually a little confusing, because the function now() is not called for every row. Instead, it is evaluated once when the query starts execution. However, the query in question is the query that uses the view, not the command that creates the view.

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
1

Now() is only a function, which will be run at insertion time.

so the result will be

person_id          name             now
1                  Bob              2020-01-01 01:01:01
2                  Billy            2020-01-01 01:01:01
3                  Barbara          2020-01-01 01:01:01
nbk
  • 45,398
  • 8
  • 30
  • 47
  • I see, so how is the `now()` function stored then? And I assume a view then is different than `CREATE TABLE AS...` where the values are stored permanently? – David542 Oct 18 '20 at 20:42
  • it is build in, but you can add your own functions by plugin – nbk Oct 18 '20 at 20:43