Robert said:
I almost
never use [views/queries] to represent entities...tables should do that
in
their own right
Not necessarily.
Here's a usage example where exposing an entity via a view/query is
useful in Access. Say you wanted to maintain a 'date inserted' column
for a table:
CREATE TABLE MyTable (
key_col INTEGER NOT NULL UNIQUE,
data_col INTEGER NOT NULL,
effective_date DATETIME DEFAULT NOW() NOT NULL
);
Obviously you don't want the effective_date column to be written to
directly e.g. a user could accidentally (or otherwise) enter a far
future date; let's also say this metadata need not be exposed either,
so you wrap it in a view/query:
CREATE VIEW MyFakedTable
AS
SELECT key_col, data_col
FROM MyTable;
Revoke all permissions from the table and grant them to the view/query.
Users can now insert into the view/query and the default 'timestamp'
gets automatically applied:
INSERT INTO MyFakedTable (key_col, data_col) VALUES (1, 1);
Of course, you are more likely to want a 'last modified' date i.e. the
effective_date column is additionally maintained for updates. For
Access, a stored proc/parameterized query is required e.g.
CREATE PROCEDURE SetMyTableData (
arg_key_col INTEGER,
arg_data_col INTEGER
) AS
UPDATE MyTable
SET data_col = arg_data_col,
effective_date = NOW()
WHERE key_col = arg_key_col;
Obviously you would now revoke update permissions from the view/query.
Note that on other platforms there may be alternatives to procs e.g. in
SQL Server updates can be controlled via an INSTEAD OF trigger on the
view or, for such a simple example, a regular trigger on the table.
This may make the system easier to use than one view/query for inserts
and another proc/query for updates (I'm remain uncomfortable about
referring to an SQL DML UPDATE *command* as a 'query' i.e. it does not
return a resultset).
Which leads me to a question: is it common practice in Access to
differentiate between the 'view' flavour of Query (e.g. prefix of 'vw')
and the 'proc' flavour of Query (e.g. prefix of 'sp') or is it almost
ubiquitous to use a single prefix (e.g. prefix of 'qry') to reflect the
fact that both flavours are lumped together on the same tab in the
Access GUI?
Jamie.