SlideShare a Scribd company logo
1 of 44
Copyright © 2007, Oracle. All rights reserved.
Creating Other Schema Objects
Copyright © 2007, Oracle. All rights reserved.
11 - 2
Objectives
After completing this lesson, you should be able to do the
following:
• Create simple and complex views
• Retrieve data from views
• Create, maintain, and use sequences
• Create and maintain indexes
• Create private and public synonyms
Copyright © 2007, Oracle. All rights reserved.
11 - 3
Lesson Agenda
• Overview of views:
– Creating, modifying, and retrieving data from a view
– Data manipulation language (DML) operations on a view
– Dropping a view
• Overview of sequences:
– Creating, using, and modifying a sequence
– Cache sequence values
– NEXTVAL and CURRVAL pseudocolumns
• Overview of indexes
– Creating, dropping indexes
• Overview of synonyms
– Creating, dropping synonyms
Copyright © 2007, Oracle. All rights reserved.
11 - 4
Database Objects
Logically represents subsets of data from
one or more tables
View
Generates numeric values
Sequence
Basic unit of storage; composed of rows
Table
Gives alternative names to objects
Synonym
Improves the performance of data retrieval
queries
Index
Description
Object
Copyright © 2007, Oracle. All rights reserved.
11 - 5
What Is a View?
EMPLOYEES table
Copyright © 2007, Oracle. All rights reserved.
11 - 6
Advantages of Views
To restrict
data access
To make complex
queries easy
To provide
data
independence
To present
different views of
the same data
Copyright © 2007, Oracle. All rights reserved.
11 - 7
Simple Views and Complex Views
Yes
No
No
One
Simple Views
Yes
Contain functions
Yes
Contain groups of data
One or more
Number of tables
Not always
DML operations through
a view
Complex Views
Feature
Copyright © 2007, Oracle. All rights reserved.
11 - 8
Creating a View
• You embed a subquery in the CREATE VIEW statement:
• The subquery can contain complex SELECT syntax.
CREATE [OR REPLACE] [FORCE|NOFORCE] VIEW view
[(alias[, alias]...)]
AS subquery
[WITH CHECK OPTION [CONSTRAINT constraint]]
[WITH READ ONLY [CONSTRAINT constraint]];
Copyright © 2007, Oracle. All rights reserved.
11 - 9
Creating a View
• Create the EMPVU80 view, which contains details of the
employees in department 80:
• Describe the structure of the view by using the iSQL*Plus
DESCRIBE command:
DESCRIBE empvu80
CREATE VIEW empvu80
AS SELECT employee_id, last_name, salary
FROM employees
WHERE department_id = 80;
Copyright © 2007, Oracle. All rights reserved.
11 - 10
Creating a View
• Create a view by using column aliases in the subquery:
• Select the columns from this view by the given alias names.
CREATE VIEW salvu50
AS SELECT employee_id ID_NUMBER, last_name NAME,
salary*12 ANN_SALARY
FROM employees
WHERE department_id = 50;
Copyright © 2007, Oracle. All rights reserved.
11 - 11
SELECT *
FROM salvu50;
Retrieving Data from a View
Copyright © 2007, Oracle. All rights reserved.
11 - 12
Modifying a View
• Modify the EMPVU80 view by using a CREATE OR REPLACE
VIEW clause. Add an alias for each column name:
• Column aliases in the CREATE OR REPLACE VIEW clause are
listed in the same order as the columns in the subquery.
CREATE OR REPLACE VIEW empvu80
(id_number, name, sal, department_id)
AS SELECT employee_id, first_name || ' '
|| last_name, salary, department_id
FROM employees
WHERE department_id = 80;
Copyright © 2007, Oracle. All rights reserved.
11 - 13
Creating a Complex View
Create a complex view that contains group functions to display
values from two tables:
CREATE OR REPLACE VIEW dept_sum_vu
(name, minsal, maxsal, avgsal)
AS SELECT d.department_name, MIN(e.salary),
MAX(e.salary),AVG(e.salary)
FROM employees e JOIN departments d
ON (e.department_id = d.department_id)
GROUP BY d.department_name;
Copyright © 2007, Oracle. All rights reserved.
11 - 14
Rules for Performing
DML Operations on a View
• You can usually perform DML operations on
simple views.
• You cannot remove a row if the view contains the following:
– Group functions
– A GROUP BY clause
– The DISTINCT keyword
– The pseudocolumn ROWNUM keyword
Copyright © 2007, Oracle. All rights reserved.
11 - 15
Rules for Performing
DML Operations on a View
You cannot modify data in a view if it contains:
• Group functions
• A GROUP BY clause
• The DISTINCT keyword
• The pseudocolumn ROWNUM keyword
• Columns defined by expressions
Copyright © 2007, Oracle. All rights reserved.
11 - 16
Rules for Performing
DML Operations on a View
You cannot add data through a view if the view includes:
• Group functions
• A GROUP BY clause
• The DISTINCT keyword
• The pseudocolumn ROWNUM keyword
• Columns defined by expressions
• NOT NULL columns in the base tables that are not selected
by the view
Copyright © 2007, Oracle. All rights reserved.
11 - 17
Using the WITH CHECK OPTION Clause
• You can ensure that DML operations performed on the view
stay in the domain of the view by using the WITH CHECK
OPTION clause:
• Any attempt to INSERT a row with a department_id other
than 20, or to UPDATE the department number for any row in
the view fails because it violates the WITH CHECK OPTION
constraint.
CREATE OR REPLACE VIEW empvu20
AS SELECT *
FROM employees
WHERE department_id = 20
WITH CHECK OPTION CONSTRAINT empvu20_ck ;
Copyright © 2007, Oracle. All rights reserved.
11 - 18
Denying DML Operations
• You can ensure that no DML operations occur by adding the
WITH READ ONLY option to your view definition.
• Any attempt to perform a DML operation on any row in the
view results in an Oracle server error.
Copyright © 2007, Oracle. All rights reserved.
11 - 19
CREATE OR REPLACE VIEW empvu10
(employee_number, employee_name, job_title)
AS SELECT employee_id, last_name, job_id
FROM employees
WHERE department_id = 10
WITH READ ONLY ;
Denying DML Operations
Copyright © 2007, Oracle. All rights reserved.
11 - 20
Removing a View
You can remove a view without losing data because a view is
based on underlying tables in the database.
DROP VIEW view;
DROP VIEW empvu80;
Copyright © 2007, Oracle. All rights reserved.
11 - 21
Practice 11: Overview of Part 1
This practice covers the following topics:
• Creating a simple view
• Creating a complex view
• Creating a view with a check constraint
• Attempting to modify data in the view
• Removing views
Copyright © 2007, Oracle. All rights reserved.
11 - 22
Lesson Agenda
• Overview of views:
– Creating, modifying, and retrieving data from a view
– DML operations on a view
– Dropping a view
• Overview of sequences:
– Creating, using, and modifying a sequence
– Cache sequence values
– NEXTVAL and CURRVAL pseudocolumns
• Overview of indexes
– Creating, dropping indexes
• Overview of synonyms
– Creating, dropping synonyms
Copyright © 2007, Oracle. All rights reserved.
11 - 23
Sequences
Logically represents subsets of data from
one or more tables
View
Generates numeric values
Sequence
Basic unit of storage; composed of rows
Table
Gives alternative names to objects
Synonym
Improves the performance of some queries
Index
Description
Object
Copyright © 2007, Oracle. All rights reserved.
11 - 24
Sequences
A sequence:
• Can automatically generate unique numbers
• Is a shareable object
• Can be used to create a primary key value
• Replaces application code
• Speeds up the efficiency of accessing sequence values
when cached in memory
1
2 4
3 5
6 8
7
10
9
Copyright © 2007, Oracle. All rights reserved.
11 - 25
CREATE SEQUENCE Statement:
Syntax
Define a sequence to generate sequential numbers
automatically:
CREATE SEQUENCE sequence
[INCREMENT BY n]
[START WITH n]
[{MAXVALUE n | NOMAXVALUE}]
[{MINVALUE n | NOMINVALUE}]
[{CYCLE | NOCYCLE}]
[{CACHE n | NOCACHE}];
Copyright © 2007, Oracle. All rights reserved.
11 - 26
Creating a Sequence
• Create a sequence named DEPT_DEPTID_SEQ to be used
for the primary key of the DEPARTMENTS table.
• Do not use the CYCLE option.
CREATE SEQUENCE dept_deptid_seq
INCREMENT BY 10
START WITH 120
MAXVALUE 9999
NOCACHE
NOCYCLE;
Copyright © 2007, Oracle. All rights reserved.
11 - 27
NEXTVAL and CURRVAL Pseudocolumns
• NEXTVAL returns the next available sequence value. It
returns a unique value every time it is referenced, even for
different users.
• CURRVAL obtains the current sequence value.
• NEXTVAL must be issued for that sequence before CURRVAL
contains a value.
Copyright © 2007, Oracle. All rights reserved.
11 - 29
Using a Sequence
• Insert a new department named “Support” in location ID
2500:
• View the current value for the DEPT_DEPTID_SEQ
sequence:
INSERT INTO departments(department_id,
department_name, location_id)
VALUES (dept_deptid_seq.NEXTVAL,
'Support', 2500);
SELECT dept_deptid_seq.CURRVAL
FROM dual;
Copyright © 2007, Oracle. All rights reserved.
11 - 30
Caching Sequence Values
• Caching sequence values in memory gives faster access to
those values.
• Gaps in sequence values can occur when:
– A rollback occurs
– The system crashes
– A sequence is used in another table
Copyright © 2007, Oracle. All rights reserved.
11 - 31
Modifying a Sequence
Change the increment value, maximum value, minimum value,
cycle option, or cache option:
ALTER SEQUENCE dept_deptid_seq
INCREMENT BY 20
MAXVALUE 999999
NOCACHE
NOCYCLE;
Copyright © 2007, Oracle. All rights reserved.
11 - 32
Guidelines for Modifying
a Sequence
• You must be the owner or have the ALTER privilege for the
sequence.
• Only future sequence numbers are affected.
• The sequence must be dropped and re-created to restart the
sequence at a different number.
• Some validation is performed.
• To remove a sequence, use the DROP statement:
DROP SEQUENCE dept_deptid_seq;
Copyright © 2007, Oracle. All rights reserved.
11 - 33
Lesson Agenda
• Overview of views:
– Creating, modifying, and retrieving data from a view
– DML operations on a view
– Dropping a view
• Overview of sequences:
– Creating, using, and modifying a sequence
– Cache sequence values
– NEXTVAL and CURRVAL pseudocolumns
• Overview of indexes
– Creating, dropping indexes
• Overview of synonyms
– Creating, dropping synonyms
Copyright © 2007, Oracle. All rights reserved.
11 - 34
Indexes
Logically represents subsets of data from
one or more tables
View
Generates numeric values
Sequence
Basic unit of storage; composed of rows
Table
Gives alternative names to objects
Synonym
Improves the performance of some queries
Index
Description
Object
Copyright © 2007, Oracle. All rights reserved.
11 - 35
Indexes
An index:
• Is a schema object
• Can be used by the Oracle server to speed up the retrieval
of rows by using a pointer
• Can reduce disk input/output (I/O) by using a rapid path
access method to locate data quickly
• Is independent of the table that it indexes
• Is used and maintained automatically by the Oracle server
Copyright © 2007, Oracle. All rights reserved.
11 - 36
How Are Indexes Created?
• Automatically: A unique index is created automatically when
you define a PRIMARY KEY or UNIQUE constraint in a table
definition.
• Manually: Users can create nonunique indexes on columns
to speed up access to the rows.
Copyright © 2007, Oracle. All rights reserved.
11 - 37
Creating an Index
• Create an index on one or more columns:
• Improve the speed of query access to the LAST_NAME
column in the EMPLOYEES table:
CREATE INDEX emp_last_name_idx
ON employees(last_name);
CREATE [UNIQUE][BITMAP]INDEX index
ON table (column[, column]...);
Copyright © 2007, Oracle. All rights reserved.
11 - 38
Index Creation Guidelines
Do not create an index when:
The columns are not often used as a condition in the query
The table is small or most queries are expected to retrieve more than
2% to 4% of the rows in the table
The table is updated frequently
A column contains a large number of null values
One or more columns are frequently used together in a WHERE
clause or a join condition
A column contains a wide range of values
The indexed columns are referenced as part of an expression
The table is large and most queries are expected to retrieve less
than 2% to 4% of the rows in the table
Create an index when:
Copyright © 2007, Oracle. All rights reserved.
11 - 39
Removing an Index
• Remove an index from the data dictionary by using the DROP
INDEX command:
• Remove the emp_last_name_idx index from the data
dictionary:
• To drop an index, you must be the owner of the index or
have the DROP ANY INDEX privilege.
DROP INDEX emp_last_name_idx;
DROP INDEX index;
Copyright © 2007, Oracle. All rights reserved.
11 - 40
Lesson Agenda
• Overview of views:
– Creating, modifying, and retrieving data from a view
– DML operations on a view
– Dropping a view
• Overview of sequences:
– Creating, using, and modifying a sequence
– Cache sequence values
– NEXTVAL and CURRVAL pseudocolumns
• Overview of indexes
– Creating, dropping indexes
• Overview of synonyms
– Creating, dropping synonyms
Copyright © 2007, Oracle. All rights reserved.
11 - 41
Synonyms
Logically represents subsets of data from
one or more tables
View
Generates numeric values
Sequence
Basic unit of storage; composed of rows
Table
Gives alternative names to objects
Synonym
Improves the performance of some queries
Index
Description
Object
Copyright © 2007, Oracle. All rights reserved.
11 - 42
Creating a Synonym for an Object
Simplify access to objects by creating a synonym (another
name for an object). With synonyms, you can:
• Create an easier reference to a table that is owned by
another user
• Shorten lengthy object names
CREATE [PUBLIC] SYNONYM synonym
FOR object;
Copyright © 2007, Oracle. All rights reserved.
11 - 43
Creating and Removing Synonyms
• Create a shortened name for the DEPT_SUM_VU view:
• Drop a synonym:
CREATE SYNONYM d_sum
FOR dept_sum_vu;
DROP SYNONYM d_sum;
Copyright © 2007, Oracle. All rights reserved.
11 - 44
Summary
In this lesson, you should have learned how to:
• Create, use, and remove views
• Automatically generate sequence numbers by using a
sequence generator
• Create indexes to improve speed of query retrieval
• Use synonyms to provide alternative names for objects
Copyright © 2007, Oracle. All rights reserved.
11 - 45
Practice 11: Overview of Part 2
This practice covers the following topics:
• Creating sequences
• Using sequences
• Creating nonunique indexes
• Creating synonyms

More Related Content

Similar to Les11.ppt

Les13[1]Other Database Objects
Les13[1]Other Database ObjectsLes13[1]Other Database Objects
Les13[1]Other Database Objectssiavosh kaviani
 
08 Dynamic SQL and Metadata
08 Dynamic SQL and Metadata08 Dynamic SQL and Metadata
08 Dynamic SQL and Metadatarehaniltifat
 
SQL WORKSHOP::Lecture 13
SQL WORKSHOP::Lecture 13SQL WORKSHOP::Lecture 13
SQL WORKSHOP::Lecture 13Umair Amjad
 
Less08_Schema Advanced Databases and Management.pptx
Less08_Schema Advanced Databases and Management.pptxLess08_Schema Advanced Databases and Management.pptx
Less08_Schema Advanced Databases and Management.pptxMurtazaMughal13
 
Data Redaction - OTN TOUR LA 2015
Data Redaction - OTN TOUR LA 2015 Data Redaction - OTN TOUR LA 2015
Data Redaction - OTN TOUR LA 2015 Alex Zaballa
 
Oracle Data Redaction
Oracle Data RedactionOracle Data Redaction
Oracle Data RedactionAlex Zaballa
 
2° Ciclo Microsoft CRUI 3° Sessione: l'evoluzione delle piattaforme tecnologi...
2° Ciclo Microsoft CRUI 3° Sessione: l'evoluzione delle piattaforme tecnologi...2° Ciclo Microsoft CRUI 3° Sessione: l'evoluzione delle piattaforme tecnologi...
2° Ciclo Microsoft CRUI 3° Sessione: l'evoluzione delle piattaforme tecnologi...Jürgen Ambrosi
 
Less07 schema
Less07 schemaLess07 schema
Less07 schemaImran Ali
 
TSQL in SQL Server 2012
TSQL in SQL Server 2012TSQL in SQL Server 2012
TSQL in SQL Server 2012Eduardo Castro
 
05 Creating Stored Procedures
05 Creating Stored Procedures05 Creating Stored Procedures
05 Creating Stored Proceduresrehaniltifat
 
database management system lessonchapter
database management system lessonchapterdatabase management system lessonchapter
database management system lessonchapterMohammedNouh7
 
common_schema, DBA's framework for MySQL
common_schema, DBA's framework for MySQLcommon_schema, DBA's framework for MySQL
common_schema, DBA's framework for MySQLShlomi Noach
 

Similar to Les11.ppt (20)

Les11
Les11Les11
Les11
 
Les09.ppt
Les09.pptLes09.ppt
Les09.ppt
 
Les13[1]Other Database Objects
Les13[1]Other Database ObjectsLes13[1]Other Database Objects
Les13[1]Other Database Objects
 
08 Dynamic SQL and Metadata
08 Dynamic SQL and Metadata08 Dynamic SQL and Metadata
08 Dynamic SQL and Metadata
 
SQL WORKSHOP::Lecture 13
SQL WORKSHOP::Lecture 13SQL WORKSHOP::Lecture 13
SQL WORKSHOP::Lecture 13
 
chap13.ppt
chap13.pptchap13.ppt
chap13.ppt
 
Sequences and indexes
Sequences and indexesSequences and indexes
Sequences and indexes
 
Less08_Schema Advanced Databases and Management.pptx
Less08_Schema Advanced Databases and Management.pptxLess08_Schema Advanced Databases and Management.pptx
Less08_Schema Advanced Databases and Management.pptx
 
plsql les02
 plsql les02 plsql les02
plsql les02
 
Les12[1]Creating Views
Les12[1]Creating ViewsLes12[1]Creating Views
Les12[1]Creating Views
 
Data Redaction - OTN TOUR LA 2015
Data Redaction - OTN TOUR LA 2015 Data Redaction - OTN TOUR LA 2015
Data Redaction - OTN TOUR LA 2015
 
Oracle Data Redaction
Oracle Data RedactionOracle Data Redaction
Oracle Data Redaction
 
Les09
Les09Les09
Les09
 
2° Ciclo Microsoft CRUI 3° Sessione: l'evoluzione delle piattaforme tecnologi...
2° Ciclo Microsoft CRUI 3° Sessione: l'evoluzione delle piattaforme tecnologi...2° Ciclo Microsoft CRUI 3° Sessione: l'evoluzione delle piattaforme tecnologi...
2° Ciclo Microsoft CRUI 3° Sessione: l'evoluzione delle piattaforme tecnologi...
 
Less07 schema
Less07 schemaLess07 schema
Less07 schema
 
TSQL in SQL Server 2012
TSQL in SQL Server 2012TSQL in SQL Server 2012
TSQL in SQL Server 2012
 
05 Creating Stored Procedures
05 Creating Stored Procedures05 Creating Stored Procedures
05 Creating Stored Procedures
 
Mysql
MysqlMysql
Mysql
 
database management system lessonchapter
database management system lessonchapterdatabase management system lessonchapter
database management system lessonchapter
 
common_schema, DBA's framework for MySQL
common_schema, DBA's framework for MySQLcommon_schema, DBA's framework for MySQL
common_schema, DBA's framework for MySQL
 

Recently uploaded

AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?XfilesPro
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraDeakin University
 
Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2Hyundai Motor Group
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 

Recently uploaded (20)

AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptxVulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
The transition to renewables in India.pdf
The transition to renewables in India.pdfThe transition to renewables in India.pdf
The transition to renewables in India.pdf
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping Elbows
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning era
 
Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 

Les11.ppt

  • 1. Copyright © 2007, Oracle. All rights reserved. Creating Other Schema Objects
  • 2. Copyright © 2007, Oracle. All rights reserved. 11 - 2 Objectives After completing this lesson, you should be able to do the following: • Create simple and complex views • Retrieve data from views • Create, maintain, and use sequences • Create and maintain indexes • Create private and public synonyms
  • 3. Copyright © 2007, Oracle. All rights reserved. 11 - 3 Lesson Agenda • Overview of views: – Creating, modifying, and retrieving data from a view – Data manipulation language (DML) operations on a view – Dropping a view • Overview of sequences: – Creating, using, and modifying a sequence – Cache sequence values – NEXTVAL and CURRVAL pseudocolumns • Overview of indexes – Creating, dropping indexes • Overview of synonyms – Creating, dropping synonyms
  • 4. Copyright © 2007, Oracle. All rights reserved. 11 - 4 Database Objects Logically represents subsets of data from one or more tables View Generates numeric values Sequence Basic unit of storage; composed of rows Table Gives alternative names to objects Synonym Improves the performance of data retrieval queries Index Description Object
  • 5. Copyright © 2007, Oracle. All rights reserved. 11 - 5 What Is a View? EMPLOYEES table
  • 6. Copyright © 2007, Oracle. All rights reserved. 11 - 6 Advantages of Views To restrict data access To make complex queries easy To provide data independence To present different views of the same data
  • 7. Copyright © 2007, Oracle. All rights reserved. 11 - 7 Simple Views and Complex Views Yes No No One Simple Views Yes Contain functions Yes Contain groups of data One or more Number of tables Not always DML operations through a view Complex Views Feature
  • 8. Copyright © 2007, Oracle. All rights reserved. 11 - 8 Creating a View • You embed a subquery in the CREATE VIEW statement: • The subquery can contain complex SELECT syntax. CREATE [OR REPLACE] [FORCE|NOFORCE] VIEW view [(alias[, alias]...)] AS subquery [WITH CHECK OPTION [CONSTRAINT constraint]] [WITH READ ONLY [CONSTRAINT constraint]];
  • 9. Copyright © 2007, Oracle. All rights reserved. 11 - 9 Creating a View • Create the EMPVU80 view, which contains details of the employees in department 80: • Describe the structure of the view by using the iSQL*Plus DESCRIBE command: DESCRIBE empvu80 CREATE VIEW empvu80 AS SELECT employee_id, last_name, salary FROM employees WHERE department_id = 80;
  • 10. Copyright © 2007, Oracle. All rights reserved. 11 - 10 Creating a View • Create a view by using column aliases in the subquery: • Select the columns from this view by the given alias names. CREATE VIEW salvu50 AS SELECT employee_id ID_NUMBER, last_name NAME, salary*12 ANN_SALARY FROM employees WHERE department_id = 50;
  • 11. Copyright © 2007, Oracle. All rights reserved. 11 - 11 SELECT * FROM salvu50; Retrieving Data from a View
  • 12. Copyright © 2007, Oracle. All rights reserved. 11 - 12 Modifying a View • Modify the EMPVU80 view by using a CREATE OR REPLACE VIEW clause. Add an alias for each column name: • Column aliases in the CREATE OR REPLACE VIEW clause are listed in the same order as the columns in the subquery. CREATE OR REPLACE VIEW empvu80 (id_number, name, sal, department_id) AS SELECT employee_id, first_name || ' ' || last_name, salary, department_id FROM employees WHERE department_id = 80;
  • 13. Copyright © 2007, Oracle. All rights reserved. 11 - 13 Creating a Complex View Create a complex view that contains group functions to display values from two tables: CREATE OR REPLACE VIEW dept_sum_vu (name, minsal, maxsal, avgsal) AS SELECT d.department_name, MIN(e.salary), MAX(e.salary),AVG(e.salary) FROM employees e JOIN departments d ON (e.department_id = d.department_id) GROUP BY d.department_name;
  • 14. Copyright © 2007, Oracle. All rights reserved. 11 - 14 Rules for Performing DML Operations on a View • You can usually perform DML operations on simple views. • You cannot remove a row if the view contains the following: – Group functions – A GROUP BY clause – The DISTINCT keyword – The pseudocolumn ROWNUM keyword
  • 15. Copyright © 2007, Oracle. All rights reserved. 11 - 15 Rules for Performing DML Operations on a View You cannot modify data in a view if it contains: • Group functions • A GROUP BY clause • The DISTINCT keyword • The pseudocolumn ROWNUM keyword • Columns defined by expressions
  • 16. Copyright © 2007, Oracle. All rights reserved. 11 - 16 Rules for Performing DML Operations on a View You cannot add data through a view if the view includes: • Group functions • A GROUP BY clause • The DISTINCT keyword • The pseudocolumn ROWNUM keyword • Columns defined by expressions • NOT NULL columns in the base tables that are not selected by the view
  • 17. Copyright © 2007, Oracle. All rights reserved. 11 - 17 Using the WITH CHECK OPTION Clause • You can ensure that DML operations performed on the view stay in the domain of the view by using the WITH CHECK OPTION clause: • Any attempt to INSERT a row with a department_id other than 20, or to UPDATE the department number for any row in the view fails because it violates the WITH CHECK OPTION constraint. CREATE OR REPLACE VIEW empvu20 AS SELECT * FROM employees WHERE department_id = 20 WITH CHECK OPTION CONSTRAINT empvu20_ck ;
  • 18. Copyright © 2007, Oracle. All rights reserved. 11 - 18 Denying DML Operations • You can ensure that no DML operations occur by adding the WITH READ ONLY option to your view definition. • Any attempt to perform a DML operation on any row in the view results in an Oracle server error.
  • 19. Copyright © 2007, Oracle. All rights reserved. 11 - 19 CREATE OR REPLACE VIEW empvu10 (employee_number, employee_name, job_title) AS SELECT employee_id, last_name, job_id FROM employees WHERE department_id = 10 WITH READ ONLY ; Denying DML Operations
  • 20. Copyright © 2007, Oracle. All rights reserved. 11 - 20 Removing a View You can remove a view without losing data because a view is based on underlying tables in the database. DROP VIEW view; DROP VIEW empvu80;
  • 21. Copyright © 2007, Oracle. All rights reserved. 11 - 21 Practice 11: Overview of Part 1 This practice covers the following topics: • Creating a simple view • Creating a complex view • Creating a view with a check constraint • Attempting to modify data in the view • Removing views
  • 22. Copyright © 2007, Oracle. All rights reserved. 11 - 22 Lesson Agenda • Overview of views: – Creating, modifying, and retrieving data from a view – DML operations on a view – Dropping a view • Overview of sequences: – Creating, using, and modifying a sequence – Cache sequence values – NEXTVAL and CURRVAL pseudocolumns • Overview of indexes – Creating, dropping indexes • Overview of synonyms – Creating, dropping synonyms
  • 23. Copyright © 2007, Oracle. All rights reserved. 11 - 23 Sequences Logically represents subsets of data from one or more tables View Generates numeric values Sequence Basic unit of storage; composed of rows Table Gives alternative names to objects Synonym Improves the performance of some queries Index Description Object
  • 24. Copyright © 2007, Oracle. All rights reserved. 11 - 24 Sequences A sequence: • Can automatically generate unique numbers • Is a shareable object • Can be used to create a primary key value • Replaces application code • Speeds up the efficiency of accessing sequence values when cached in memory 1 2 4 3 5 6 8 7 10 9
  • 25. Copyright © 2007, Oracle. All rights reserved. 11 - 25 CREATE SEQUENCE Statement: Syntax Define a sequence to generate sequential numbers automatically: CREATE SEQUENCE sequence [INCREMENT BY n] [START WITH n] [{MAXVALUE n | NOMAXVALUE}] [{MINVALUE n | NOMINVALUE}] [{CYCLE | NOCYCLE}] [{CACHE n | NOCACHE}];
  • 26. Copyright © 2007, Oracle. All rights reserved. 11 - 26 Creating a Sequence • Create a sequence named DEPT_DEPTID_SEQ to be used for the primary key of the DEPARTMENTS table. • Do not use the CYCLE option. CREATE SEQUENCE dept_deptid_seq INCREMENT BY 10 START WITH 120 MAXVALUE 9999 NOCACHE NOCYCLE;
  • 27. Copyright © 2007, Oracle. All rights reserved. 11 - 27 NEXTVAL and CURRVAL Pseudocolumns • NEXTVAL returns the next available sequence value. It returns a unique value every time it is referenced, even for different users. • CURRVAL obtains the current sequence value. • NEXTVAL must be issued for that sequence before CURRVAL contains a value.
  • 28. Copyright © 2007, Oracle. All rights reserved. 11 - 29 Using a Sequence • Insert a new department named “Support” in location ID 2500: • View the current value for the DEPT_DEPTID_SEQ sequence: INSERT INTO departments(department_id, department_name, location_id) VALUES (dept_deptid_seq.NEXTVAL, 'Support', 2500); SELECT dept_deptid_seq.CURRVAL FROM dual;
  • 29. Copyright © 2007, Oracle. All rights reserved. 11 - 30 Caching Sequence Values • Caching sequence values in memory gives faster access to those values. • Gaps in sequence values can occur when: – A rollback occurs – The system crashes – A sequence is used in another table
  • 30. Copyright © 2007, Oracle. All rights reserved. 11 - 31 Modifying a Sequence Change the increment value, maximum value, minimum value, cycle option, or cache option: ALTER SEQUENCE dept_deptid_seq INCREMENT BY 20 MAXVALUE 999999 NOCACHE NOCYCLE;
  • 31. Copyright © 2007, Oracle. All rights reserved. 11 - 32 Guidelines for Modifying a Sequence • You must be the owner or have the ALTER privilege for the sequence. • Only future sequence numbers are affected. • The sequence must be dropped and re-created to restart the sequence at a different number. • Some validation is performed. • To remove a sequence, use the DROP statement: DROP SEQUENCE dept_deptid_seq;
  • 32. Copyright © 2007, Oracle. All rights reserved. 11 - 33 Lesson Agenda • Overview of views: – Creating, modifying, and retrieving data from a view – DML operations on a view – Dropping a view • Overview of sequences: – Creating, using, and modifying a sequence – Cache sequence values – NEXTVAL and CURRVAL pseudocolumns • Overview of indexes – Creating, dropping indexes • Overview of synonyms – Creating, dropping synonyms
  • 33. Copyright © 2007, Oracle. All rights reserved. 11 - 34 Indexes Logically represents subsets of data from one or more tables View Generates numeric values Sequence Basic unit of storage; composed of rows Table Gives alternative names to objects Synonym Improves the performance of some queries Index Description Object
  • 34. Copyright © 2007, Oracle. All rights reserved. 11 - 35 Indexes An index: • Is a schema object • Can be used by the Oracle server to speed up the retrieval of rows by using a pointer • Can reduce disk input/output (I/O) by using a rapid path access method to locate data quickly • Is independent of the table that it indexes • Is used and maintained automatically by the Oracle server
  • 35. Copyright © 2007, Oracle. All rights reserved. 11 - 36 How Are Indexes Created? • Automatically: A unique index is created automatically when you define a PRIMARY KEY or UNIQUE constraint in a table definition. • Manually: Users can create nonunique indexes on columns to speed up access to the rows.
  • 36. Copyright © 2007, Oracle. All rights reserved. 11 - 37 Creating an Index • Create an index on one or more columns: • Improve the speed of query access to the LAST_NAME column in the EMPLOYEES table: CREATE INDEX emp_last_name_idx ON employees(last_name); CREATE [UNIQUE][BITMAP]INDEX index ON table (column[, column]...);
  • 37. Copyright © 2007, Oracle. All rights reserved. 11 - 38 Index Creation Guidelines Do not create an index when: The columns are not often used as a condition in the query The table is small or most queries are expected to retrieve more than 2% to 4% of the rows in the table The table is updated frequently A column contains a large number of null values One or more columns are frequently used together in a WHERE clause or a join condition A column contains a wide range of values The indexed columns are referenced as part of an expression The table is large and most queries are expected to retrieve less than 2% to 4% of the rows in the table Create an index when:
  • 38. Copyright © 2007, Oracle. All rights reserved. 11 - 39 Removing an Index • Remove an index from the data dictionary by using the DROP INDEX command: • Remove the emp_last_name_idx index from the data dictionary: • To drop an index, you must be the owner of the index or have the DROP ANY INDEX privilege. DROP INDEX emp_last_name_idx; DROP INDEX index;
  • 39. Copyright © 2007, Oracle. All rights reserved. 11 - 40 Lesson Agenda • Overview of views: – Creating, modifying, and retrieving data from a view – DML operations on a view – Dropping a view • Overview of sequences: – Creating, using, and modifying a sequence – Cache sequence values – NEXTVAL and CURRVAL pseudocolumns • Overview of indexes – Creating, dropping indexes • Overview of synonyms – Creating, dropping synonyms
  • 40. Copyright © 2007, Oracle. All rights reserved. 11 - 41 Synonyms Logically represents subsets of data from one or more tables View Generates numeric values Sequence Basic unit of storage; composed of rows Table Gives alternative names to objects Synonym Improves the performance of some queries Index Description Object
  • 41. Copyright © 2007, Oracle. All rights reserved. 11 - 42 Creating a Synonym for an Object Simplify access to objects by creating a synonym (another name for an object). With synonyms, you can: • Create an easier reference to a table that is owned by another user • Shorten lengthy object names CREATE [PUBLIC] SYNONYM synonym FOR object;
  • 42. Copyright © 2007, Oracle. All rights reserved. 11 - 43 Creating and Removing Synonyms • Create a shortened name for the DEPT_SUM_VU view: • Drop a synonym: CREATE SYNONYM d_sum FOR dept_sum_vu; DROP SYNONYM d_sum;
  • 43. Copyright © 2007, Oracle. All rights reserved. 11 - 44 Summary In this lesson, you should have learned how to: • Create, use, and remove views • Automatically generate sequence numbers by using a sequence generator • Create indexes to improve speed of query retrieval • Use synonyms to provide alternative names for objects
  • 44. Copyright © 2007, Oracle. All rights reserved. 11 - 45 Practice 11: Overview of Part 2 This practice covers the following topics: • Creating sequences • Using sequences • Creating nonunique indexes • Creating synonyms