CHAPTER 9 Views, Synonyms, and Sequences. Views are used extensively in reporting applications and...
-
Upload
junior-peters -
Category
Documents
-
view
215 -
download
3
Transcript of CHAPTER 9 Views, Synonyms, and Sequences. Views are used extensively in reporting applications and...
![Page 1: CHAPTER 9 Views, Synonyms, and Sequences. Views are used extensively in reporting applications and also to present subsets of data to applications. Synonyms.](https://reader036.fdocuments.in/reader036/viewer/2022070403/56649f315503460f94c4d3b2/html5/thumbnails/1.jpg)
CHAPTER 9Views, Synonyms, and Sequences
![Page 2: CHAPTER 9 Views, Synonyms, and Sequences. Views are used extensively in reporting applications and also to present subsets of data to applications. Synonyms.](https://reader036.fdocuments.in/reader036/viewer/2022070403/56649f315503460f94c4d3b2/html5/thumbnails/2.jpg)
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.
![Page 3: CHAPTER 9 Views, Synonyms, and Sequences. Views are used extensively in reporting applications and also to present subsets of data to applications. Synonyms.](https://reader036.fdocuments.in/reader036/viewer/2022070403/56649f315503460f94c4d3b2/html5/thumbnails/3.jpg)
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.
![Page 4: CHAPTER 9 Views, Synonyms, and Sequences. Views are used extensively in reporting applications and also to present subsets of data to applications. Synonyms.](https://reader036.fdocuments.in/reader036/viewer/2022070403/56649f315503460f94c4d3b2/html5/thumbnails/4.jpg)
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.
![Page 5: CHAPTER 9 Views, Synonyms, and Sequences. Views are used extensively in reporting applications and also to present subsets of data to applications. Synonyms.](https://reader036.fdocuments.in/reader036/viewer/2022070403/56649f315503460f94c4d3b2/html5/thumbnails/5.jpg)
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;
![Page 6: CHAPTER 9 Views, Synonyms, and Sequences. Views are used extensively in reporting applications and also to present subsets of data to applications. Synonyms.](https://reader036.fdocuments.in/reader036/viewer/2022070403/56649f315503460f94c4d3b2/html5/thumbnails/6.jpg)
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;
![Page 7: CHAPTER 9 Views, Synonyms, and Sequences. Views are used extensively in reporting applications and also to present subsets of data to applications. Synonyms.](https://reader036.fdocuments.in/reader036/viewer/2022070403/56649f315503460f94c4d3b2/html5/thumbnails/7.jpg)
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).
![Page 8: CHAPTER 9 Views, Synonyms, and Sequences. Views are used extensively in reporting applications and also to present subsets of data to applications. Synonyms.](https://reader036.fdocuments.in/reader036/viewer/2022070403/56649f315503460f94c4d3b2/html5/thumbnails/8.jpg)
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;
/
![Page 9: CHAPTER 9 Views, Synonyms, and Sequences. Views are used extensively in reporting applications and also to present subsets of data to applications. Synonyms.](https://reader036.fdocuments.in/reader036/viewer/2022070403/56649f315503460f94c4d3b2/html5/thumbnails/9.jpg)
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';
![Page 10: CHAPTER 9 Views, Synonyms, and Sequences. Views are used extensively in reporting applications and also to present subsets of data to applications. Synonyms.](https://reader036.fdocuments.in/reader036/viewer/2022070403/56649f315503460f94c4d3b2/html5/thumbnails/10.jpg)
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.
![Page 11: CHAPTER 9 Views, Synonyms, and Sequences. Views are used extensively in reporting applications and also to present subsets of data to applications. Synonyms.](https://reader036.fdocuments.in/reader036/viewer/2022070403/56649f315503460f94c4d3b2/html5/thumbnails/11.jpg)
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');
![Page 12: CHAPTER 9 Views, Synonyms, and Sequences. Views are used extensively in reporting applications and also to present subsets of data to applications. Synonyms.](https://reader036.fdocuments.in/reader036/viewer/2022070403/56649f315503460f94c4d3b2/html5/thumbnails/12.jpg)
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.
![Page 13: CHAPTER 9 Views, Synonyms, and Sequences. Views are used extensively in reporting applications and also to present subsets of data to applications. Synonyms.](https://reader036.fdocuments.in/reader036/viewer/2022070403/56649f315503460f94c4d3b2/html5/thumbnails/13.jpg)
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;
![Page 14: CHAPTER 9 Views, Synonyms, and Sequences. Views are used extensively in reporting applications and also to present subsets of data to applications. Synonyms.](https://reader036.fdocuments.in/reader036/viewer/2022070403/56649f315503460f94c4d3b2/html5/thumbnails/14.jpg)
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;
![Page 15: CHAPTER 9 Views, Synonyms, and Sequences. Views are used extensively in reporting applications and also to present subsets of data to applications. Synonyms.](https://reader036.fdocuments.in/reader036/viewer/2022070403/56649f315503460f94c4d3b2/html5/thumbnails/15.jpg)
Uses for Synonyms• Provide transparent access to other users’ database
objects• Eliminates the need to hard code schema name into
application code
![Page 16: CHAPTER 9 Views, Synonyms, and Sequences. Views are used extensively in reporting applications and also to present subsets of data to applications. Synonyms.](https://reader036.fdocuments.in/reader036/viewer/2022070403/56649f315503460f94c4d3b2/html5/thumbnails/16.jpg)
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
![Page 17: CHAPTER 9 Views, Synonyms, and Sequences. Views are used extensively in reporting applications and also to present subsets of data to applications. Synonyms.](https://reader036.fdocuments.in/reader036/viewer/2022070403/56649f315503460f94c4d3b2/html5/thumbnails/17.jpg)
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.
![Page 18: CHAPTER 9 Views, Synonyms, and Sequences. Views are used extensively in reporting applications and also to present subsets of data to applications. Synonyms.](https://reader036.fdocuments.in/reader036/viewer/2022070403/56649f315503460f94c4d3b2/html5/thumbnails/18.jpg)
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.
![Page 19: CHAPTER 9 Views, Synonyms, and Sequences. Views are used extensively in reporting applications and also to present subsets of data to applications. Synonyms.](https://reader036.fdocuments.in/reader036/viewer/2022070403/56649f315503460f94c4d3b2/html5/thumbnails/19.jpg)
Dynamically Generating Synonyms• Use SQL to generate SQL.
CONNECT &&master_user/&&master_pwd.@&&tns_alias
--
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;
![Page 20: CHAPTER 9 Views, Synonyms, and Sequences. Views are used extensively in reporting applications and also to present subsets of data to applications. Synonyms.](https://reader036.fdocuments.in/reader036/viewer/2022070403/56649f315503460f94c4d3b2/html5/thumbnails/20.jpg)
Displaying Synonym Metadata
select synonym_name, table_owner, table_name, db_linkfrom user_synonymsorder by 1;
select owner,object_name,object_type,statusfrom dba_objectswhere object_name like upper('&object_name%');
![Page 21: CHAPTER 9 Views, Synonyms, and Sequences. Views are used extensively in reporting applications and also to present subsets of data to applications. Synonyms.](https://reader036.fdocuments.in/reader036/viewer/2022070403/56649f315503460f94c4d3b2/html5/thumbnails/21.jpg)
Renaming a Synonym• Conform to naming standards.• Determine if the synonym is being used.
SQL> rename inv_s to inv_st;
![Page 22: CHAPTER 9 Views, Synonyms, and Sequences. Views are used extensively in reporting applications and also to present subsets of data to applications. Synonyms.](https://reader036.fdocuments.in/reader036/viewer/2022070403/56649f315503460f94c4d3b2/html5/thumbnails/22.jpg)
Dropping a Synonym• In general, you should drop objects that are not being
used.
SQL> drop synonym inv;
![Page 23: CHAPTER 9 Views, Synonyms, and Sequences. Views are used extensively in reporting applications and also to present subsets of data to applications. Synonyms.](https://reader036.fdocuments.in/reader036/viewer/2022070403/56649f315503460f94c4d3b2/html5/thumbnails/23.jpg)
Sequences• Object that guarantees SELECT access to a unique
integer for the given sequence.• Used often for PK and FK values.
![Page 24: CHAPTER 9 Views, Synonyms, and Sequences. Views are used extensively in reporting applications and also to present subsets of data to applications. Synonyms.](https://reader036.fdocuments.in/reader036/viewer/2022070403/56649f315503460f94c4d3b2/html5/thumbnails/24.jpg)
Creating a Sequence
SQL> create sequence inv_seq;
SQL> create sequence inv2 start with 10000 maxvalue 1000000;
![Page 25: CHAPTER 9 Views, Synonyms, and Sequences. Views are used extensively in reporting applications and also to present subsets of data to applications. Synonyms.](https://reader036.fdocuments.in/reader036/viewer/2022070403/56649f315503460f94c4d3b2/html5/thumbnails/25.jpg)
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;
![Page 26: CHAPTER 9 Views, Synonyms, and Sequences. Views are used extensively in reporting applications and also to present subsets of data to applications. Synonyms.](https://reader036.fdocuments.in/reader036/viewer/2022070403/56649f315503460f94c4d3b2/html5/thumbnails/26.jpg)
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;
/
![Page 27: CHAPTER 9 Views, Synonyms, and Sequences. Views are used extensively in reporting applications and also to present subsets of data to applications. Synonyms.](https://reader036.fdocuments.in/reader036/viewer/2022070403/56649f315503460f94c4d3b2/html5/thumbnails/27.jpg)
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;
![Page 28: CHAPTER 9 Views, Synonyms, and Sequences. Views are used extensively in reporting applications and also to present subsets of data to applications. Synonyms.](https://reader036.fdocuments.in/reader036/viewer/2022070403/56649f315503460f94c4d3b2/html5/thumbnails/28.jpg)
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.
![Page 29: CHAPTER 9 Views, Synonyms, and Sequences. Views are used extensively in reporting applications and also to present subsets of data to applications. Synonyms.](https://reader036.fdocuments.in/reader036/viewer/2022070403/56649f315503460f94c4d3b2/html5/thumbnails/29.jpg)
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;
![Page 30: CHAPTER 9 Views, Synonyms, and Sequences. Views are used extensively in reporting applications and also to present subsets of data to applications. Synonyms.](https://reader036.fdocuments.in/reader036/viewer/2022070403/56649f315503460f94c4d3b2/html5/thumbnails/30.jpg)
Renaming a Sequence• Conform to naming standards.• Rename the object before you drop it.
SQL> rename inv_seq to inv_seq_old;
![Page 31: CHAPTER 9 Views, Synonyms, and Sequences. Views are used extensively in reporting applications and also to present subsets of data to applications. Synonyms.](https://reader036.fdocuments.in/reader036/viewer/2022070403/56649f315503460f94c4d3b2/html5/thumbnails/31.jpg)
Dropping a Sequence• Drop objects that aren’t being used.
SQL> drop sequence inv;
![Page 32: CHAPTER 9 Views, Synonyms, and Sequences. Views are used extensively in reporting applications and also to present subsets of data to applications. Synonyms.](https://reader036.fdocuments.in/reader036/viewer/2022070403/56649f315503460f94c4d3b2/html5/thumbnails/32.jpg)
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;
![Page 33: CHAPTER 9 Views, Synonyms, and Sequences. Views are used extensively in reporting applications and also to present subsets of data to applications. Synonyms.](https://reader036.fdocuments.in/reader036/viewer/2022070403/56649f315503460f94c4d3b2/html5/thumbnails/33.jpg)
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.