问题
Where are view constraints useful? By that I mean, Oracle allows a constraint to be created on a view. They are not enforced by the database. It seems to be just more metadata that can be used by the database, but I'm trying to understand under what circumstances they are useful.
Tom Kyte answered in a question:
They are used for complex query rewrites with materialized views and such. It is more "meta data" -- it gives the optimizer more information, allows for a broader range of query rewriting to take place.
... but that's a bit brief.
回答1:
From Oracle Documentation :
View Constraints
You can create constraints on views. The only type of constraint supported on a view is a RELY constraint.
This type of constraint is useful when queries typically access views instead of base tables, and the database administrator thus needs to define the data relationships between views rather than tables. View constraints are particularly useful in OLAP environments, where they may enable more sophisticated rewrites for materialized views.
Quoted another Oracle Documentation page, but never used constraints on views anyway.
回答2:
Minor use in an Oracle product. In the Designer-generated PL/SQL web applications, components based on views needed a primary key defined on the view. That allowed the application to hyperlink from a list of records to a single-record display.
I remember seeing a few cases with Hibernate where it generated better code when there were PK and FK constraints defined on views. (Can anybody else confirm that?)
And Tom points to query rewrite.
So I think the answer is "if your tools can use the information, then it's better to supply it." Of course, it's going to be hard to figure out which tools will use it.
I try to include them because
- It's not much work, though the scripts to recreate views are somewhat more complicated.
- It helps in making the physical implementation of the logical model complete
- It reminds me of real data constraints that I need to implement somehow, via triggers or background packages or in a "constraint violation" report.
来源:https://stackoverflow.com/questions/4435034/whats-the-point-of-a-view-constraint