CHAPTER 9 Views, Synonyms, and Sequences. Views are used extensively in reporting applications and also to present subsets of data to applications. Synonyms.

Slides:



Advertisements
Similar presentations
Database Security and Auditing: Protecting Data Integrity and Accessibility Chapter 8 Application Data Auditing.
Advertisements

Database Security and Auditing: Protecting Data Integrity and Accessibility Chapter 8 Application Data Auditing.
Copyright  Oracle Corporation, All rights reserved. 10 Creating and Managing Tables.
10 Copyright © 2004, Oracle. All rights reserved. Creating Other Schema Objects.
Chapter 3: Using SQL Queries to Insert, Update, Delete, and View Data
A Guide to Oracle9i1 Advanced SQL And PL/SQL Topics Chapter 9.
Database objects User schema DCL Oracle dictionary.
Copyright س Oracle Corporation, All rights reserved. 13 Other Database Objects.
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.
INTEGRITY Enforcing integrity in Oracle. Oracle Tables mrobbert owner granted access.
SQL's Data Definition Language (DDL) – View, Sequence, Index.
Advance Computer Programming Java Database Connectivity (JDBC) – In order to connect a Java application to a database, you need to use a JDBC driver. –
Chapter 6 Additional Database Objects
I Copyright © 2004, Oracle. All rights reserved. Introduction.
RDB/1 An introduction to RDBMS Objectives –To learn about the history and future direction of the SQL standard –To get an overall appreciation of a modern.
Database Security and Auditing: Protecting Data Integrity and Accessibility Chapter 6 Virtual Private Databases.
CSIS 4310 – Advanced Databases Virtual Private Databases.
13 Other Database Objects Important Legal Notice:  Materials on this lecture are from a book titled “Oracle Education” by Kochhar, Gravina, and Nathan.
Lecture 7 Integrity & Veracity UFCE8K-15-M: Data Management.
Introduction to SEQUEL. What is SEQUEL? Acronym for Structural English Query Language Acronym for Structural English Query Language Standard language.
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 7 PL/SQL Packages.
Dr. Chen, Oracle Database System (Oracle) 1 Chapter 6 Additional Database Objects (up to p.195 and all in the pptx file) Jason C. H. Chen, Ph.D. Professor.
Copyright © 2004, Oracle. All rights reserved. Lecture 3: Creating Other Schema Objects Lecture 3: Creating Other Schema Objects ORACLE.
12 Copyright © Oracle Corporation, All rights reserved. Other Database Objects.
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.
Chapter 5 Sequences.
10 Creating and Managing Tables Objectives At the end of this lesson, you will be able to: Describe the main database objects Create tables Describe.
Lecture2: Database Environment Prepared by L. Nouf Almujally 1 Ref. Chapter2 Lecture2.
Database Programming Sections 11 & 12 – Creating, and Managing Views, Sequences, Indexes, and Synonymns.
Copyright  Oracle Corporation, All rights reserved. 10 Creating and Managing Tables.
ITBIS373 Database Development Lecture 3a - Chapter 3: Using SQL Queries to Insert, Update, Delete, and View Data.
What is a Package? A package is an Oracle object, which holds other objects within it. Objects commonly held within a package are procedures, functions,
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 9 Database Triggers.
Database Programming Sections 11 & 12 –Sequences, Indexes, and Synonymns.
George Mpopo | Rosebank College ADVANCED DATABASES WITH ORACLE 11g FOR ADDB7311 LEARNING UNIT 2 of 7.
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 1 (SQL) Other Database Objects Asif Sohail University of the.
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.
PL/SQLPL/SQL Oracle11g: PL/SQL Programming Chapter 9 Database Triggers.
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 9 Database Triggers.
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 8 Program Unit Dependencies.
Chapter 5 : Integrity And Security  Domain Constraints  Referential Integrity  Security  Triggers  Authorization  Authorization in SQL  Views 
Chapter 4 Indexes. Indexes Logically represents subsets of data from one or more tables View Generates numeric valuesSequence Basic unit of storage; composed.
Transactions, Roles & Privileges Oracle and ANSI Standard SQL Lecture 11.
Chapter 12 Additional Database Objects. Chapter Objectives  Define the purpose of a sequence and state how it can be used by an organization  Explain.
Altering Tables and Constraints Database Systems Objectives Add and modify columns. Add, enable, disable, or remove constraints. Drop a table. Remove.
Chapter 13Introduction to Oracle9i: SQL1 Chapter 13 User Creation and Management.
A Guide to SQL, Eighth Edition Chapter Six Updating Data.
Oracle 11g: SQL Chapter 7 User Creation and Management.
Chapter 6 Virtual Private Databases
IS6146 Databases for Management Information Systems Lecture 3: SQL III – The DDL Rob Gleasure robgleasure.com.
Database Programming Sections 12 – Sequences, Indexes, and Synonymns.
Chapter 12Introduction to Oracle9i: SQL1 Chapter 12 Additional Database Objects.
Dr. Chen, Oracle Database System (Oracle) 1 Chapter 7 User Creation and Management Jason C. H. Chen, Ph.D. Professor of MIS School of Business Gonzaga.
Chapter 15 Materialized Views.
Constraints and Views Chap. 3-5 continued (7 th ed. 5-7)
Oracle9i Developer: PL/SQL Programming Chapter 6 PL/SQL Packages.
Tables and Triggers.
Database Fundamentals
Chapter 5 Sequences.
Managing Objects with Data Dictionary Views
Chapter 4 Indexes.
CH 4 Indexes.
Managing Objects with Data Dictionary Views
A Guide to SQL, Eighth Edition
CH 4 Indexes.
Oracle9i Developer: PL/SQL Programming Chapter 8 Database Triggers.
Prof. Arfaoui. COM390 Chapter 9
Presentation transcript:

CHAPTER 9 Views, Synonyms, and Sequences

Views are used extensively in reporting applications and also to present subsets of data to applications. Synonyms provide a means to transparently allow users to display and use other users’ objects. Sequences are often used to generate unique numbers that are used to populate primary-key and foreign-key values.

Uses for Views Create an efficient method of storing a SQL query for reuse. Provide an interface layer between an application and physical tables. Hide the complexity of a SQL query from an application. Report to a user about only a subset of columns and/or rows.

Creating a View Create views on tables, views, or materialized views. Use the CREATE VIEW statement. create table sales( sales_id number primary key,amnt number,state varchar2(2) ); If the view already exists, consider using CREATE OR REPLACE VIEW statement.

Views With Check Option You can issue UPDATE statements against views which result in updating the underlying tables referenced in the view SQL query. WITH CHECK OPTION specifies that the UPDATE statement can only affect rows that are selected by the SQL the view is based upon. create or replace view sales_rockies as select sales_id,amnt,state from sales where state in ('CO','UT','WY','ID','AZ') with check option;

Creating Read-Only Views If you don’t want a user to be able to perform INSERT, UPDATE, or DELETE operations on a view, then don’t grant those object privileges on the underlying table(s) to users with access to the view. Also, specify READ ONLY when creating the view. create or replace view sales_rockies as select sales_id,amnt,state from sales where state in ('CO','UT','WY','ID','AZ') with read only;

Updatable Join Views If you have multiple tables defined in the FROM clause of the SQL query on which the view is based, it’s still possible to update the underlying tables. The DML statement must modify only one underlying table. The view must be created without the READ ONLY clause. The column being updated belongs to the key-preserved table in the join view (there is only one key-preserved table in a join view).

Creating an INSTEAD OF Trigger An INSTEAD OF trigger on a view instructs Oracle to execute PL/SQL code instead of using the DML statement. create or replace trigger emp_dept_v_updt instead of update on emp_dept_v for each row begin update emp set emp_name=UPPER(:new.emp_name) where emp_id=:old.emp_id; end; /

Implementing an Invisible Column in a View New starting with Oracle Database 12c create or replace view sales_co (sales_id, amnt, state, sales_person_id invisible) as select sales_id, amnt, state, sales_person_id from sales where state = 'CO';

Modifying a View Definition CREATE OR REPLACE allows you to modify the view definition without dropping the object. Advantage is that you don’t have to reissue any grants on the view. Most DBAs/Developers almost always use the CREATE OR REPLACE, and not dropping and recreating the view.

Displaying the SQL Used to Create a View Troubleshooting issues with views often begins with viewing the SQL that the view is based upon. select view_name,text from dba_views where owner = upper('&owner') and view_name like upper('&view_name');

Oracle Internal View Definitions You may occasionally need the definition of an Oracle internal view. Sometimes you may wonder how Oracle is retrieving information; you might be troubleshooting a complex database issue or bug. SQL> select view_definition from v$fixed_view_definition where view_name='V$BH'; Gives you insight into Oracle’s internal workings.

Renaming a View You may want to change the name so that it better conforms to standard. You may want to rename a view before dropping it so that you can better determine whether it’s in use. SQL> rename inv to inv_old;

Dropping a View In general, for objects not being used, it’s better to drop them so as to not clutter up your environment and make it more difficult to maintain and manage. Drop views that aren’t being used. SQL> drop view inv_v;

Uses for Synonyms Provide transparent access to other users’ database objects Eliminates the need to hard code schema name into application code

Synonyms are Pointers to Database Objects Tables Views or object views Other synonyms Remote objects via a database link PL/SQL packages, procedures, and functions Materialized views Sequences Java class schema object User-defined object types

Creating a Synonym Use the CREATE SYNONYM statement to create a synonym. SQL> select * from inv; Query from DBA/USER/ALL_SYNONYMS to view details.

Public Synonyms Public synonyms allow you to create access to the synonym on a database wide basis. This simplifies some aspects of the DBA’s job. However, it has a few drawbacks: Troubleshooting can be problematic if you’re not aware of globally defined (public) synonyms. Applications that share one database can have collisions on object names if multiple applications use public synonyms that aren’t unique within the database. Security should be administered as needed, not on a wholesale basis. You’ll run into DBAs who often use public synonyms, and other DBAs who never use them.

Dynamically Generating Synonyms Use SQL to generate SQL. CONNECT -- SET LINESIZE 132 PAGESIZE 0 ECHO OFF FEEDBACK OFF SET VERIFY OFF HEAD OFF TERM OFF TRIMSPOOL ON -- SPO gen_syns_dyn.sql -- select 'create or replace synonym ' || table_name || ' for ' || '&&master_user..' || table_name || ';' from user_tables; -- SPO OFF; -- SET ECHO ON FEEDBACK ON VERIFY ON HEAD ON TERM ON;

Displaying Synonym Metadata select synonym_name, table_owner, table_name, db_link from user_synonyms order by 1; select owner,object_name,object_type,status from dba_objects where object_name like upper('&object_name%');

Renaming a Synonym Conform to naming standards. Determine if the synonym is being used. SQL> rename inv_s to inv_st;

Dropping a Synonym In general, you should drop objects that are not being used. SQL> drop synonym inv;

Sequences Object that guarantees SELECT access to a unique integer for the given sequence. Used often for PK and FK values.

Creating a Sequence SQL> create sequence inv_seq; SQL> create sequence inv2 start with maxvalue ;

Sequence Pseudo-columns Two pseudo column values used to access a sequence: NEXTVAL CURRVAL SQL> select inv_seq.nextval from dual; SQL> select inv_seq.currval from dual;

Autoincrementing Columns Starting with 12c, Oracle does have autoincrementing columns If using 11g or older, manually simulate this with a trigger: create or replace trigger inv_bu_tr before insert on inv for each row begin select inv_seq.nextval into :new.inv_id from dual; end; /

Interesting Uses of Sequences SQL> create sequence inv_seq_odd start with 1 increment by 2; SQL> create sequence inv_seq_even start with 2 increment by 2; SQL> create sequence inv_seq_odd_dwn start with -1 increment by -2; SQL> create sequence inv_seq_even_dwn start with -2 increment by -2;

Using One Sequence or Many Usually there are not performance issues with accessing one sequence. Usually there are not issues with using large integer values for populating NUMBER columns.

Viewing Sequence Metadata select sequence_name,min_value,max_value,increment_by from user_sequences; SQL> select dbms_metadata.get_ddl('SEQUENCE','INV_SEQ') from dual;

Renaming a Sequence Conform to naming standards. Rename the object before you drop it. SQL> rename inv_seq to inv_seq_old;

Dropping a Sequence Drop objects that aren’t being used. SQL> drop sequence inv;

Resetting a Sequence UNDEFINE seq_name UNDEFINE reset_to PROMPT "sequence name" ACCEPT '&&seq_name' PROMPT "reset to value" ACCEPT &&reset_to COL seq_id NEW_VALUE hold_seq_id COL min_id NEW_VALUE hold_min_id -- SELECT &&reset_to - &&seq_name..nextval - 1 seq_id FROM dual; -- SELECT &&hold_seq_id - 1 min_id FROM dual; -- ALTER SEQUENCE &&seq_name INCREMENT BY &hold_seq_id MINVALUE &hold_min_id; -- SELECT &&seq_name..nextval FROM dual; -- ALTER SEQUENCE &&seq_name INCREMENT BY 1;

Summary Views, Synonyms, and Sequences are used extensively within database applications. Views are stored SQL queries that allow you to encapsulate SQL logic and present it as an object to the user. Synonyms provide a transparent mechanism to enable access to another user’s objects. Sequences are counters that are often used to guarantee unique values are generated for use when populating primary and foreign key columns.