views:

162

answers:

5

A SQL VIEW is a global, logical table that may or may not be persisted. But it's still a table. Therefore, should a VIEW always adhere to first normal form (1NF)? i.e. no duplicate rows, scalar types only, no top-to-bottom or left-to-right ordering, etc. What about the higher normal forms?

For me, my applications 'consume' the results of stored procs, my VIEWs are 'consumed' by SQL queries, and these two usages are mutually exclusive (i.e. I don’t query the resultsets of stored procs using SQL and my applications do not contain SQL code). I've seen others use a VIEW to 'concatenate' multiple values in a column into a single row, usually comma-separated format. Writing predicates in a SQL query against such a column requires a kludges similar to this:

',' + concat_col + ',' LIKE '%' + ',' + search_value + ',' + '%'

So it seems to me reasonable to expect all tables that can be queried to consist of only scalar types. Am I being too 'purist' by thinking this?

+7  A: 

No - I create views to match the output that my program requires.

Galwegian
My VIEWs are 'consumed' by SQL queries only. If my program needs a resultset in a 'special' format then I would either do this in a stored proc or the middle tier. I'm not suggesting the output of every stored proc should be in 1NF, only output that is in the form of a *table* (and I guess that would include table variables where applicable).
onedaywhen
You've obviously created rules for your own application (no SQL in clients, e.g.) which work for you. They are more restrictive that what I would consider to be best practices, but the nice thing about being too restrictive is that it's always easy to change your mind later and be more relaxed - not so easy to go the other way. But generally, the output of views can violate 1NF (although dupe rows are useless, AFAIK). In fact, using ugly views is one of the best ways of migrating an ugly design to a clean design - you need the views to support legacy clients until they too, can be fixed.
Steve Broberg
+2  A: 

The whole point of relational systems is that you keep data in normalized relations for efficiency and / or manageability, and then use the relational operators to convert them into the relations you need.

A non-materialized view is not stored, it's a query.

That's why you should create it in the form that best fits your applications needs.

See this answer for more detail.

Quassnoi
A: 

I don't think this is a rule, but if it was - No rule should always be followed.

Paul Rowland
I think the accepted approach is that you follow the 'rules' of normalization then you follow the 'rules' of denormalization if there are good reason for doing so. Unless you are an anarchist in which case the rules are there are no rules, fight the power, bondage trousers, kudos to you.
onedaywhen
A: 

a view (unless it is materialized/indexed view) is nothing but a stored query Views can contain more than one table, can have self joins to the same table etc etc

SQLMenace
Indeed and I can join a viewed table (a.k.a. VIEW) to other tables... so it would really help matters if all columns are scalar types.
onedaywhen
...I've added a description of this usage and the implications for 1NF to my question.
onedaywhen
+1  A: 

No - normalization rules apply to the persistence of data, not the presentation of it. E.g., any duplicate rows in a view would break 1NF, which is obviously overly restrictive.

For more info, see First normal form.

RedFilter
How is a VIEW with duplicate rows useful? Do you have a real life example in mind? Thanks.
onedaywhen