3 Views.

Slides:



Advertisements
Similar presentations
Organisation Of Data (1) Database Theory
Advertisements

Business Planning using Spreasheets-2 1 BP-2: Good Spreadsheet Practice  There is always the temptation to rush in and start entering data.  However.
A Guide to SQL, Seventh Edition. Objectives Embed SQL commands in PL/SQL programs Retrieve single rows using embedded SQL Update a table using embedded.
A Guide to MySQL 7. 2 Objectives Understand, define, and drop views Recognize the benefits of using views Use a view to update data Grant and revoke users’
A Guide to SQL, Seventh Edition. Objectives Understand, create, and drop views Recognize the benefits of using views Grant and revoke user’s database.
11 Chapter 2: Basic Queries 2.1: Overview of the SQL Procedure 2.2: Specifying Columns 2.3: Specifying Rows.
SAS Efficiency Techniques and Methods By Kelley Weston Sr. Statistical Programmer Quintiles.
Copyright © 2004, Oracle. All rights reserved. Lecture 3: Creating Other Schema Objects Lecture 3: Creating Other Schema Objects ORACLE.
11 Chapter 7: Creating Tables and Views 7.1 Creating Views with the SQL Procedure 7.2 Creating Tables with the SQL Procedure (Self-Study) 7.3 Integrity.
1 Agenda – 10/24/2013 Answer questions from lab on 10/22. Present SQL View database object. Present SQL UNION statement.
Database structure and space Management. Database Structure An ORACLE database has both a physical and logical structure. By separating physical and logical.
Chapter 2 Views. Objectives ◦ Create simple and complex views ◦ Creating a view with a check constraint ◦ Retrieve data from views ◦ Data manipulation.
Database structure and space Management. Segments The level of logical database storage above an extent is called a segment. A segment is a set of extents.
Chapter 13 Views Oracle 10g: SQL. Oracle 10g: SQL2 Objectives Create a view, using CREATE VIEW command or the CREATE OR REPLACE VIEW command Employ the.
Physical Database Design Purpose- translate the logical description of data into the technical specifications for storing and retrieving data Goal - create.
11 Chapter 4: Subqueries 4.1: Noncorrelated Subqueries 4.2: Correlated Subqueries (Self-Study)
Unit-8 Introduction Of MySql. Types of table in PHP MySQL supports various of table types or storage engines to allow you to optimize your database. The.
1 Database Fundamentals Introduction to SQL. 2 SQL Overview Structured Query Language The standard for relational database management systems (RDBMS)
Working Efficiently with Large SAS® Datasets Vishal Jain Senior Programmer.
Data Integrity & Indexes / Session 1/ 1 of 37 Session 1 Module 1: Introduction to Data Integrity Module 2: Introduction to Indexes.
SQL IMPLEMENTATION & ADMINISTRATION Indexing & Views.
Chapter 10: Accessing Relational Databases (Self-Study)
Table General Guidelines for Better System Performance
A Guide to SQL, Seventh Edition
Chapter 6: Set Operators
Displaying Query Results
Oracle Join Syntax.
Creating Views Schedule: Timing Topic 20 minutes Lecture
Displaying Data from Multiple Tables
C-language Lecture By B.S.S.Tejesh, S.Neeraja Asst.Prof.
Displaying Data from Multiple Tables
Database Performance Tuning and Query Optimization
Basic Queries Specifying Columns
PROC SQL, Overview.
3 Macro Storage.
Using the Set Operators
Instructor: Raul Cruz-Cano
Noncorrelated subquery
Correlated Subqueries
What Is a View? EMPNO ENAME JOB EMP Table EMPVU10 View
CSCI 2141 – Intro to Database Systems
Integrity Constraints
Displaying Queries 2 Display a query’s results in a specified order.
Subsetting Rows with the WHERE clause
Chapter 2 Views.
Oracle Join Syntax.
MANAGING DATA RESOURCES
Table General Guidelines for Better System Performance
Grouping Summary Results
A Guide to SQL, Eighth Edition
Inner Joins.
Program Testing and Performance
Chapter 2 Views.
Restricting and Sorting Data
5 The EXCEPT Operator Unique rows from the first result set that are not found in the second result set are selected.
Structured Types (9-12-2) Structured types allow composite attributes of E-R diagrams to be represented directly. Unnamed row types can also be used in.
The INTERSECT Operator
3 Specifying Rows.
Using SQL*Plus.
Chapter 11 Database Performance Tuning and Query Optimization
Setting SQL Procedure Options
Oracle Join Syntax.
Using Subqueries to Solve Queries
IST 318 Database Administration
Displaying Data from Multiple Tables
Using Subqueries to Solve Queries
UNION Operator keywords Displays all rows from both the tables
Displaying Data from Multiple Tables
Displaying Data from Multiple Tables
Subqueries Schedule: Timing Topic 25 minutes Lecture
Presentation transcript:

3 Views

What Is a PROC SQL View? A stored query Contains no actual data Extracts underlying data each time that it is used -- accesses the most current data Can be referenced in SAS programs in the same way as a data table Cannot have the same name as a data table stored in the same SAS library.

Creating a View General form of the CREATE VIEW statement: The CREATE VIEW statement differs from the CREATE TABLE statement in that a view is always created from the results of a query. Methods that create empty tables without extracting data are not appropriate for creating views. CREATE VIEW view-name AS query-expression;

Example Tom Zhou is a sales manager who needs access to current personnel information for his reports, in particular name, job title, salary, and years of service. He asked for access to personnel data so that he can generate reports. Tom should not be allowed access to personnel data for any employee that he does not directly supervise.

The necessary data can be obtained from these tables: Employee_organization (n=424) Employee_payroll (n=424) Employee_addresses (n=424)

Creating a View proc sql; create view orion.Tom_Zhou as select Employee_Name as Name format=$25.0, Job_Title as Title format=$15.0, Salary 'Annual Salary' format=comma10.2, int((today()-Employee_Hire_Date)/365.25) as YOS 'Years of Service' from orion.Employee_Addresses as a, orion.Employee_Payroll as p, orion.Employee_Organization as o where a.Employee_ID=p.Employee_ID and o.Employee_ID=p.Employee_ID and Manager_ID=120102;/*Tom Zhou’s id*/ quit; Instructor note: The formatting and order of this code is important to make later comparisons to the output of DESCRIBE VIEW easier to perform.

proc contents data=orion.tom_zhou; run;

Using a View proc sql; title "Tom Zhou's Direct Reports"; title2 "By Title and Years of Service"; select * from orion.Tom_Zhou order by Title desc, YOS desc; quit; title;

Describe view -- outputs stored query to log proc sql; describe view orion.Tom_Zhou; quit;

Views: Advantages Avoid storing copies of large data tables Avoid a frequent refresh of data table copies. When the underlying data changes, a view uses the most current data Pull together data from multiple database tables and multiple libraries or databases Simplify complex queries Prevent other users from inadvertently altering the query code

Views: Disadvantages Because views access the most current data in changing tables, the results might be different each time that you access the view. Views can require significant resources each time that they execute. With a view, you save disk storage space at the cost of extra CPU and memory usage. When accessing the same data several times in a program, use a table instead of a view. This ensures consistent results from one step to the next and can significantly reduce the resources that are required.

A problem with views (stored queries) Embedded libnames.

libname test 'c:\tmp'; proc sql; create table test.staff as select * from orion.staff ; quit;

Two-Level Table Names in Views Create a permanent view, Level_II, in c:\tmp, using the libref test : libname test 'c:\tmp'; proc sql; create view test.Level_II as select Employee_ID, Gender,Job_Title as Title from test.Staff where scan(Job_Title,-1) ='II' and Emp_Term_Date is missing ; quit;

Two-Level Table Names in Permanent Views Several weeks later, you remember creating the Level_II view and decide that it is the perfect source to use for the current reporting project.

A program libname test clear; libname orion "c:\tmp"; proc sql; select * from orion.Level_II; quit;

Two-Level Table Names in Permanent Views proc sql; create view test.Level_II as select Employee_ID, Gender,Job_Title as Title from test.Staff where scan(Job_Title,-1) ='II' and Emp_Term_Date is missing ; quit; libname test clear; libname orion "c:\tmp"; proc sql; select * from orion.Level_II; quit; There is no test libref assigned.

Two-Level Table Names in Permanent Views You can use two techniques to address this issue when you reference permanent tables in views: ANSI method: Omit the libref -- use a single-level table name. SAS enhancement: Embed the LIBNAME statement with a USING clause.

Two-Level Table Names in Permanent Views ANSI Method: Omit the libref. libname test 'c:\tmp'; proc sql; create view test.Level_II as select Employee_ID, Gender,Job_Title as Title from Staff where scan(Job_Title,-1) ='II' and Emp_Term_Date is missing; quit; This method works as long as the view and table are stored in the same location. When a view is not stored in the same location as its source tables (co-located), it doesn’t work ...

Two-Level Table Names in Permanent Views ANSI Example: Omit the libref. libname test 'c:\tmp'; proc sql; create view test.Level_II as select Employee_ID, Gender,Job_Title as Title from Staff where scan(Job_Title,-1) ='II' and Emp_Term_Date is missing; quit; At view execution, PROC SQL interprets this as the following: "Look in the location where the Level_II view is stored for a table named Staff." A SAS programmer might interpret this as a reference to the table work.Staff. ...

Two-Level Table Names in Permanent Views SAS Enhancement: Embed the LIBNAME statement with a USING clause. The scope of the embedded libref is local to the view, and it will not conflict with an identically named libref in the SAS session. CREATE VIEW proc-sql-view AS query-expression <USING LIBNAME-clause<, …LIBNAME-clause>>;

Two-Level Table Names in Permanent Views Example: Embed the LIBNAME statement with a USING clause. The path defined in an embedded LIBNAME statement might not be valid if the view is executed on a different operating system. libname test 'c:\test'; proc sql; create view test.Level_II as select Employee_ID, Gender,Job_Title as Title from orion.Staff where scan(Job_Title,-1) ='II' and Emp_Term_Date is missing using libname orion 'c:\tmp'; quit; When the view test.Level_II executes, the libref orion always refers to the location c:\test'.

Re-create the view -- use an embedded LIBNAME statement to make it portable. Tom can then copy the view to any location that he chooses and use it to create his reports.

Making a View Portable proc sql; create view orion.Tom_Zhou as select Employee_Name as Name format=$25.0, Job_Title as Title format=$15.0, Salary "Annual Salary" format=comma10.2, int((today()-Employee_Hire_Date)/365.25) as YOS 'Years of Service' from orion.Employee_Addresses as a, orion.Employee_Payroll as p, orion.Employee_Organization as o where a.Employee_ID=p.Employee_ID and o.Employee_ID=p.Employee_ID and Manager_ID=120102 using libname orion “c:\users\dlm1\dropbox\SAS\sasdata\sql"; ; quit;

General Guidelines for Using Views Avoid ORDER BY clauses in view definitions, which force data sorting each time that the view is referenced. When you create a permanent view with permanent tables in the FROM clause, use a USING clause to specify the location of the libraries to make your view portable.