Creating Views. 2 home back first prev next last What Will I Learn? List three uses for views from the standpoint of a database administrator Explain,

Slides:



Advertisements
Similar presentations
Creating Tables. 2 home back first prev next last What Will I Learn? List and provide an example of each of the number, character, and date data types.
Advertisements

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
Basic SQL Introduction Presented by: Madhuri Bhogadi.
Manipulating Data Schedule: Timing Topic 60 minutes Lecture
Database Programming Sections 13. Marge Hohly  1. Which statements are True about the following sequence? The sequence was used to generate numbers.
Introduction to Structured Query Language (SQL)
10 Copyright © 2004, Oracle. All rights reserved. Creating Other Schema Objects.
Introduction to Structured Query Language (SQL)
8 Copyright © Oracle Corporation, All rights reserved. Manipulating Data.
Chapter 3: Using SQL Queries to Insert, Update, Delete, and View Data
View Sen Zhang. Views are very common in business systems users view of data is simplified a form of security - user sees only the data he/she needs to.
Database Systems More SQL Database Design -- More SQL1.
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’
View and Materialized view. What is a view? Logically represents subset of data from one or more table. In sql, a view is a virtual relation based on.
Introduction to Structured Query Language (SQL)
A Guide to SQL, Seventh Edition. Objectives Understand, create, and drop views Recognize the benefits of using views Grant and revoke user’s database.
Introduction to Oracle9i: SQL1 Views. Introduction to Oracle9i: SQL2 Chapter Objectives Create a view, using CREATE VIEW command or the CREATE OR REPLACE.
DAY 21: MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Akhila Kondai October 30, 2013.
Chapter 6 Additional Database Objects
Database Programming Sections 13–Creating, revoking objects privileges.
Views: Limiting Access to Data A view is a named select statement that is stored in a database as an object. It allows you to view a subset of rows or.
Copyright © 2004, Oracle. All rights reserved. Lecture 3: Creating Other Schema Objects Lecture 3: Creating Other Schema Objects ORACLE.
Other database objects (Sequence). What Is a Sequence? A sequence: Automatically generates sequential numbers Is a sharable object Is typically used to.
11 Copyright © 2007, Oracle. All rights reserved. Creating Other Schema Objects.
Chapter 6 Additional Database Objects Oracle 10g: SQL.
10 Copyright © 2009, Oracle. All rights reserved. Using DDL Statements to Create and Manage Tables.
1 Database Administration. 2 Objectives  Understand, create, and drop views  Grant and revoke users’ privileges  Understand and obtain information.
Chapter 6 Database Administration
DEFAULT Values and the MERGE Statement. 2 home back first prev next last What Will I Learn? Understand when to specify a DEFAULT value. Construct and.
11 Copyright © Oracle Corporation, All rights reserved. Creating Views.
Database Programming Sections 11 & 12 – Creating, and Managing Views, Sequences, Indexes, and Synonymns.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
Chapter 2 Views. Objectives ◦ Create simple and complex views ◦ Creating a view with a check constraint ◦ Retrieve data from views ◦ Data manipulation.
Manipulating Data in PL/SQL. 2 home back first prev next last What Will I Learn? Construct and execute PL/SQL statements that manipulate data with DML.
IS 230Lecture 6Slide 1 Lecture 7 Advanced SQL Introduction to Database Systems IS 230 This is the instructor’s notes and student has to read the textbook.
Managing Constraints. 2 home back first prev next last What Will I Learn? Four different functions that the ALTER statement can perform on constraints.
Objectives Database triggers and syntax
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 9 Database Triggers.
Controlling User Access. 2 home back first prev next last What Will I Learn? Compare the difference between object privileges and system privileges Construct.
Database Programming Sections 11 & 12 –Sequences, Indexes, and Synonymns.
Database Security. Multi-user database systems like Oracle include security to control how the database is accessed and used for example security Mechanisms:
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.
Copyright  Oracle Corporation, All rights reserved. 12 Creating Views.
PL/SQLPL/SQL Oracle11g: PL/SQL Programming Chapter 9 Database Triggers.
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 9 Database Triggers.
Retrieving Data in PL/SQL. 2 home back first prev next last What Will I Learn? In this lesson, you will learn to: –Recognize the SQL statements that can.
Chapter 5 : Integrity And Security  Domain Constraints  Referential Integrity  Security  Triggers  Authorization  Authorization in SQL  Views 
DML Part 1 Yogiek Indra Kurniawan. All Files Can Be Downloaded at : Menu : “Perkuliahan”
Chapter 12 Additional Database Objects. Chapter Objectives  Define the purpose of a sequence and state how it can be used by an organization  Explain.
A Guide to SQL, Eighth Edition Chapter Six Updating Data.
Creating Views Database Systems Objectives Explain the concept of a view. Create simple and complex views. Retrieve data through a view. Alter the.
Chapter 12Introduction to Oracle9i: SQL1 Chapter 12 Additional Database Objects.
Copyright س Oracle Corporation, All rights reserved. 12 Creating Views.
Chapter 13 Triggers. Trigger Overview A trigger is a program unit that is executed (fired) due to an event Event such as updating tables, deleting data.
Creating and Revoking Object Privileges. 2 home back first prev next last What Will I Learn? Explain what a ROLE is and what its advantages are. Construct.
 CONACT UC:  Magnific training   
Dr. Chen, Oracle Database System (Oracle) 1 Basic Nested Queries and Views Jason C. H. Chen, Ph.D. Professor of MIS School of Business Gonzaga University.
Other database objects (Sequence and view). What Is a Sequence? A sequence: Automatically generates sequential numbers Is a sharable object Is typically.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
Prepared by : Moshira M. Ali CS490 Coordinator Arab Open University
ITEC 313 Database Programming
Creating Views Schedule: Timing Topic 20 minutes Lecture
Creating Other Schema Objects
Creating Other Schema Objects
What Is a View? EMPNO ENAME JOB EMP Table EMPVU10 View
Chapter 2 Views.
Database Programming Sections 11 – Creating, and Managing Views, Sequences, Indexes, and Synonymns.
Chapter 2 Views.
Contents Preface I Introduction Lesson Objectives I-2
IST 318 Database Administration
Presentation transcript:

Creating Views

2 home back first prev next last What Will I Learn? List three uses for views from the standpoint of a database administrator Explain, from a business perspective, why it is important to be able to create and use logical subsets of data derived from one or more tables Create a view with and without column aliases in the subquery using a single base table Create a complex view that contains group functions to display values from two tables Retrieve data from a view

3 home back first prev next last Why Learn It? Take a minute to look back at what you've learned so far as an Oracle Academy student. –How easy would it be to explain what you know to someone who hadn't taken this class? –You should pat yourself on the back! –The level of knowledge you have acquired is understood by only a select few. Now, imagine yourself as the Database Administrator of a business. –What do you do when a manager asks you to make it possible for him/her to be able to retrieve and input data using the company's database? –"Don't make it too complicated; I just want to be able to prepare reports about all our operations."

4 home back first prev next last Why Learn It?

5 home back first prev next last Why Learn It? Should these employees have access to all of the company's data? How will they execute commands that require join conditions? Is it wise to allow data input from anyone? These are questions that you, as DBA, need to know how to answer. In this section, you will learn how to create "views" – virtual representations of tables customized to meet specific user requirements.

6 home back first prev next last VIEW A view, like a table, is a database object. However, views are not “real" tables. –They are logical representations of existing tables or of another view. –Views contain no data of their own. –They function as a window through which data from tables can be viewed or changed. –The view is a query stored as a SELECT statement in the data dictionary. –The tables on which a view is based are called "base tables ". CREATE VIEW view_employees AS SELECT first_name, last_name, FROM employees WHERE employee_id BETWEEN 100 and 124;

7 home back first prev next last WHY USE VIEWS? Views restrict access to the data because the view can display selective columns from the table. Views can be used to reduce the complexity of executing queries based on more complicated SELECT statements. –For example, the creator of the view can construct join statements that retrieve data from multiple tables. The user of the view neither sees the underlying code nor how to create it. The user, through the view, interacts with the database using simple queries. Views can be used to retrieve data from several tables, providing data independence for users. Users can view the same data in different ways. Views provide groups of users with access to data according to their particular permissions or criteria.

8 home back first prev next last CREATING A VIEW To create a view, embed a subquery within the CREATE VIEW statement. The syntax of a view statement is as follows: CREATE [OR REPLACE] [FORCE| NOFORCE] VIEW view [(alias [, alias]...)] AS subquery [WITH CHECK OPTION [CONSTRAINT constraint]] [WITH READ ONLY [CONSTRAINT constraint]]; – Example CREATE OR REPLACE VIEW view_of_animals AS SELECT animal_name…;

9 home back first prev next last CREATING A VIEW CREATE [OR REPLACE] [FORCE| NOFORCE] VIEW view [(alias [, alias]...)] AS subquery [WITH CHECK OPTION [CONSTRAINT constraint]] [WITH READ ONLY [CONSTRAINT constraint]];

10 home back first prev next last GUIDELINES FOR CREATING A VIEW The subquery that defines the view can contain complex SELECT syntax. The subquery that defines the view cannot contain an ORDER BY clause. The ORDER BY clause is specified when you retrieve data from the view. You can use the OR REPLACE option to change the definition of the view without having to drop it or regrant object privileges previously granted on it. Aliases can be used for the column names in the subquery. –Aliases must be used for the expression columns

11 home back first prev next last CREATE VIEW There are two classifications for views: –simple and complex. The table summarizes the features of each view.

12 home back first prev next last SIMPLE VIEW The view shown below is an example of a simple view. The subquery derives data from only one table and it does not contain a join function or any group functions. Because it is a simple view, INSERT, UPDATE, DELETE, and MERGE operations affecting the base tables could be performed through the view. CREATE VIEW view_copy_d_cds AS SELECT cd_number, title, producer, year FROM d_cds;

13 home back first prev next last SIMPLE VIEW Column names in the SELECT statement can have aliases as shown below. Note that aliases can also be listed after the CREATE VIEW statement and before the SELECT subquery. CREATE VIEW view_copy_d_cds AS SELECT cd_number AS "Number", title AS "Title", year AS “Year_Recorded" FROM d_cds; CREATE VIEW view_copy_d_cds(Number, Title, Year_Recorded) AS SELECT cd_number,title, year FROM d_cds;

14 home back first prev next last SIMPLE VIEW -- 视图列名同基表列名 为 first_name create view a as select first_name from employees; -- 视图列名为 ename create or replace view a(ename) as select first_name from employees; create or replace view a as select first_name ename from employees; -- 视图列名为 emp_name create or replace view a(emp_name) as select first_name ename from employees;

15 home back first prev next last SIMPLE VIEW It is possible to create a view regardless of whether or not the base tables exist. –Adding the word FORCE to the CREATE VIEW statement creates the view. –As a DBA, this option could be useful during the development of a database to verify with the potential users of the view that the view, as designed, meets their requirements. –The NOFORCE option is the default.

16 home back first prev next last SIMPLE VIEW DROP TABLE a; -- ORA-00942: 表或视图不存在 create view a_view as select * from a; -- 未找到数据 select * from user_views where view_name='A_VIEW'; -- 行 0 上出现错误 : ORA-00942: 表或视图不存在 create force view a_view as select * from a; 秒后返回了 1 行 select * from user_views where view_name='A_VIEW'; -- ORA-04063: view "S01.A_VIEW" 有错误 select * from a_view; -- 视图已删除。 DROP VIEW a_view;

17 home back first prev next last COMPLEX VIEW Complex views are views that can contain group functions and joins. The following example creates a view that derives data from two tables. CREATE VIEW view_dj_on_demand (LAST_NAME, TELEPHONE, EVENT, DATE_HELD) AS SELECT c.last_name, c.phone, e.name, TO_CHAR(e.event_date, 'Month dd, YYYY') FROM d_clients c, d_events e WHERE c.client_number = e.client_number;

18 home back first prev next last COMPLEX VIEW Group functions can also be added to complex- view statements. CREATE VIEW view_dj_cds (TITLE, SONG, MIN_YEAR, MAX_YEAR) AS SELECT c.title, t.song_id, MIN(c.year), MAX(c.year) FROM d_cds c, d_track_listings t WHERE c.cd_number = t.cd_number GROUP BY c.cd_number, c.title, t.song_id;

19 home back first prev next last MODIFYING A VIEW To modify an existing view without having to recreate it, use the OR REPLACE option in the CREATE VIEW statement. The old view is replaced by the new version. –For example: CREATE OR REPLACE VIEW view_copy_d_cds AS SELECT cd_number, title, producer, year FROM d_cds;

20 home back first prev next last Terminology Key terms used in this lesson include: –View –Simple view –Complex review –CREATE VIEW –VIEW_NAME –Alias –Subquery –NO FORCE –FORCE –REPLACE

21 home back first prev next last Summary In this lesson you have learned to: –List three uses for views from the standpoint of a database administrator –Explain, from a business perspective, why it is important to be able to create and use logical subsets of data derived from one or more tables –Create a view with and without column aliases in the subquery using a single base table –Create a complex view that contains group functions to display values from two tables –Retrieve data from a view