Monday, April 24, 2017

Views - SQL

Benifits  of   Views   

A view provides several benefits.

1. Views can hide complexity

If you have a query that requires joining several tables, or has complex logic or calculations,
you can code all that logic into a view, then select from the view just like you would a table.

2. Views can be used as a security mechanism

A view can select certain columns and/or rows from a table, and permissions set on the view
instead of the underlying tables. This allows surfacing only the data that a user needs to see.

3. Views can simplify supporting legacy code

If you need to refactor a table that would break a lot of code, you can replace the table
with a view of the same name. The view provides the exact same schema as the original table,
while the actual schema has changed. This keeps the legacy code that references the table
from breaking, allowing you to change the legacy code at your leisure.


4.Among other things, it can be used for security. If you have a "customer" table, you might
want to give all of your sales people access to the name, address, zipcode, etc. fields, but
not credit_card_number. You can create a view that only includes the columns they need access
 to and then grant them access on the view.

5.A view is an encapsulation of a query. Queries that are turned into views tend to be complicated
 and as such saving them as a view for reuse can be advantageous.  Though a view doesn't store data, some refer to a views as “virtual tables,” you can query a view like you can a table .

6.Once a view is created you can used then as you would any table in a SELECT statement.

7.Views take up very little space, as the data is stored once in the source table.

Sample  view  is  shown  below .

 

CREATE OR REPLACE VIEW    SAMPLE_VIEW     AS
SELECT  cast('A' as varchar2(2)) col1,'Overdue period > 0  month and < 3 months' col2
FROM dual
UNION
SELECT cast('B' as varchar2(2)) col1,'Overdue period > 3  month and < 6 months' col2
FROM dual
UNION
SELECT cast('C' as varchar2(2))  col1,'Overdue period >= 24 months' col2
FROM dual
UNION
SELECT cast('D' as varchar2(2))  col1, 'Overdue RV > 0'  col2
FROM dual

No comments:

Post a Comment

The AI Driven Software Developer, Optimize Innovate Transform

  The AI-Driven Software Developer: Optimize, Innovate, Transform": AI Transformation in Software Development : Understand how AI is re...