0% found this document useful (0 votes)
31 views21 pages

Unit-3 Final

RDBMS-Oracle notes2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views21 pages

Unit-3 Final

RDBMS-Oracle notes2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

For more references Please Visit :

https://easy2learncode.in

RDBMS Using Oracle


unit-3
(Unit-3: Other ORACLE Database Objects,
Concurrency control using lock)
- Sojitra Dhruvi H.

22
RDBMS Using Oracle unit-3

View
 A view is a logical representation of a division(subset) of
data from one or more table

 A view is a virtual table.

 So ,creating views does not take any storage space.


other than the space in the data dictionary.

 In SQL, a view is a virtual table based on the result-set of


an SQL statement.

 A view contains rows and columns, just like a real table.


The fields in a view are fields from one or more real
tables in the database.

 You can add SQL functions, WHERE, and JOIN


statements to a view and present the data as if the data
were coming from one single table.
Two types of view
1)simple view 2)Complex view

https://easy2learncode.in Page 2
RDBMS Using Oracle unit-3

A simple view is one that :


 Derives data from only one table.
 Contains no functions or groups of data.
 Can perform DML operations through the view.
Syntax :
Create [or replace] VIEW view_nmAS Query[DML
COMMAND].
Example:
Create table dept (dept_nonumber(3),dept_name
varchar2(10));

insert into dept (dept_no,dept_name) values (1,'BCA');

Select * from dept

create view emp14 as select * from dept

select * from emp14

 Drop view
Drop view <view_name>
https://easy2learncode.in Page 3
RDBMS Using Oracle unit-3

Drop view emp14

A complex view is one that :


 Derives data from many tables.
 Contains functions or groups of data.
 Does not always allow DML operations through the
view.
Syntax :
Create [or replace] VIEW view_nmAS Query
Example:
Create table emp (no number(3),name varchar2(10),job
varchar2(10),salary number(5));

insert into emp(no,name,job,salary) values


(1,'aaa','Accountant',15000);

Select * from emp;

create view vw_emp_dept AS select E.NAME ,


D.DEPT_NAME fromEMP E,DEPT D WHERE
E.NO=D.DEPT_NO;

https://easy2learncode.in Page 4
RDBMS Using Oracle unit-3

select * from vw_emp_dept;

DROP VIEW view_nm;


drop view vw_emp_dept;

Features Simple Complex


views view

Number of tables One One or more

Contain function No Yes

Contain group of data No Yes

DML operation(select, insert, Yes Not always


delete, update)

SEQUENCE
 An oracle sequence is aoracle object that is used to
generate an Auto number field.

https://easy2learncode.in Page 5
RDBMS Using Oracle unit-3

 That can be used to generate a sequential list of unique


numbers for a table’s columns.

 Usually, the sequence is used to enter data in “PRIMARY


KEY” column to maintain unique values.

 Also used any numeric column.


Syntax:

 Cretae SEQUENCE <name>


[
[Increment by<no.>]  increase/decrease.

[Start with <no.>]  first number of seq.

[Maxvalue<no>]  largest value of seq.

[Minvalue<no>]  lowest value of seq.

[cache / nocache]  size of the block of seq.]

Example.
STEP(1)

https://easy2learncode.in Page 6
RDBMS Using Oracle unit-3

create table student(id number(5),name


varchar2(10),city varchar2(10));
STEP(2)
create sequence seq01
increment by 20
start with 10
maxvalue 100;

STEP(3)
insert into student
values(seq01.nextval,'shiv','rajkot');
insert into student
values(seq01.nextval,'shivansh','rajkot');
STEP(4)
select *from student;
STEP(5)
Drop sequence
Drop sequence < sequence _name>
Drop sequence seq01

https://easy2learncode.in Page 7
RDBMS Using Oracle unit-3

SYNONYMS
 A synonym is an alternative name for object.

 such as table, view, sequences ,stored procedures and


other database objects.

 A synonym can point to a table, view, sequence, in the


local database. To an object in another database.

 It’s main advantages is to hide the identity of the


object.

 Ex.

 Step-1

 Syntax : Create synonym syn_name for table_name


create synonym syn_123 for student;

 Step-2
select *from syn_123

 Step-3

 Drop synonym

 Drop synonym <synonym _name>


drop synonym syn_123

https://easy2learncode.in Page 8
RDBMS Using Oracle unit-3

DATABASE LINKS
 Database link allows you to connect from one database
to another.

 You can establish the direct connection from local


database to remote database.

 A database link can be private –owned by a single user,


or public in which all the user in the local database can
use the link.

 Ex.
create database link myremotedb using 'system'
selectsysdate from dual myremotedb

INDEX

 The CREATE INDEX statement is used to create indexes


in tables.
 We can create indexes explicitly to a speed up SQL
statement execution on a table.
 The index points directly to the rows containing the
values. (ex.row_id)

Types of index:

https://easy2learncode.in Page 9
RDBMS Using Oracle unit-3

INDEX

Function Application
B*Tree Bitmap
based Domain

B-Tree/Normal/Simple Indexes
 CreateINDEX <index_name>ON
table_nm<colm1,colm2>

Ex. Create index inx1 ON student(id)

selectname,city from student where id=50;

Drop index
Drop index < index _name>
Drop index inx1

Bitmap Indexes

It is only apply on grouping distinct value like a


department number, gender, course.

https://easy2learncode.in Page 10
RDBMS Using Oracle unit-3

Syntax:
Create bitmap index Index_name on
Table_name(Columns which have distinct values);

Example:

CREATE BITMAP index BM_DEPT_NAME on


DEPT(Dept_name);
select * from DEPT where DEPT_NAME= 'BCA';

Function-Based Indexes

Syntax:
Create index indexname on
tablename(Function_name(column_name));

Example:
Create index func_name_idx ON
student(UPPER(NAME))

Select * from student where lower(NAME)='shiv'

Create index FI_student on


student(TO_CHAR(Hire_date,'YYYY'));

Select * from student where


TO_CHAR(Hire_date,'YYYY')=2022;

Application Domain Indexes


Oracle provides extensible indexing to accommodate indexes on
complex data types such as documents, spatial data, images, and video
clips and to make use of specialized indexing techniques.

https://easy2learncode.in Page 11
RDBMS Using Oracle unit-3

With extensible indexing, you can encapsulate application-specific index


management routines as an index type schema object and define
a domain index (an application-specific index) on table columns or
attributes of an object type.

Extensible indexing also provides efficient processing of application-


specific operators.

The application software, controls the structure and content of a domain


index.

The Oracle server interacts with the application to build, maintain, and
search the domain index.

The index structure itself can be stored in the Oracle database as an


index-organized table or externally as a file.

CLUSTER

 A cluster is a schema object that contains data from one


or more tables. All of which have one or more column is
common.

 Oracle DB store together all the rows from all the tables
that share the same cluster key. use the CREATE
CLUSTER statement to create a cluster.

 Table clustering is very seldom used by oracle DBA’s and


developers.

 Two or more computer that share resources and work


together to form a larger logical computing unit.

Syntax :
https://easy2learncode.in Page 12
RDBMS Using Oracle unit-3

 create cluster <cluster_name>(column dayatype(size),


columndatatypes(size),…)

CREATE CLUSTER personnel(department NUMBER(4)) SIZE


512 STORAGE (initial 100K next 50K);

SNAPSHOT
 It is a read only copy of a table or a subset of a table. A
snapshot is more useful in distributed computing
environment .that time using command create
snapshot.

 a snapshot is a table that contains the results of a query


of more tables of view, often on a remote database.

Syntax :
create snapshot [schema.]snapshot as query.

Ex. create table cust (roll number(3) primary key ,name


varchar2(10),city varchar2(10));

insert into cust values(1,'aaa','bombay');

Ex.
create snapshot snap_emp1 as select *from cust
select * from snap_emp1

https://easy2learncode.in Page 13
RDBMS Using Oracle unit-3

What Are Locks?

 In multi-user system , many users may update the


same information at the same time.
 Locking allows only one user to update a particular
data block while another person cannot modify
the same data.

Locking Issues

1. Lost Updates

A Lost Update Is A Classic Database Problem.


Actually, It Is A problem in all multiuser –
environments. Simply put, a lost update occurs
when the following events occur, such like:
1. A transaction in session1 retrieves(queries) a row of
data into local memory and display into an end
user, user1.
2. Another transaction in session2 retrieves that same
row, but displays the data to a different end user,
user2.
3. User1, using the application, modifies that row and
has the application update the database and
commit. Session1’s transaction is now complete.

https://easy2learncode.in Page 14
RDBMS Using Oracle unit-3

4. User modifies that row also, and has the application


update the database and commit. Session2’s
transaction is now complete.
This process is referred to as a lost update because
all of the changes made in step 3 will be lost.

2. Pessimistic Locking
 pessimistic locking prevents any other
application or user from fetching or updating
the same record at the same time.
 Pessimistic locking can be a very powerful
mechanism, but often databases can impose
limitations. Some databases are not support
this locking.
 Raise an exception when an attempt is made to
update a record that has already been locked
and updated.

3. Optimistic Locking

 It is a technique for SQL database applications that


does not lock rows for viewing, updating or deleting.

 The row with DML query changes are locked until


changes are committed.

 The oracle database uses optimistic locking by


default.

https://easy2learncode.in Page 15
RDBMS Using Oracle unit-3

 Any command that begins with update…set that is


not preceded by a select…for update is an example
of optimistic locking.

 You re-read data and only update it, if it did not


change since the initial fetch.

4. Blocking
 Blocking occurs when one session holds a lock
on a resource that another session is
requesting.
 As a result, the requesting session will be
blocked – it will hang until the holding session
gives up the locked resource.

 The five common DML statements that will


block in the database are insert, update, delete,
merge, and select for update.

5. Deadlocks

 A deadlock occurs when you have two sessions,


each of which is holding a resource that the other
wants.

 when two or more threads of control are blocked,


each waiting on a resource held by the other thread.

https://easy2learncode.in Page 16
RDBMS Using Oracle unit-3

 When this happens, there is no possibility of the


threads ever making forward progress unless some
outside agent takes action to break the deadlock.

6. Lock Escalation
 The system is decreasing the granularity of your
locks.

 An example would be the database system turning


your 100 row-level locks against a table into a
single table-level lock. Lock escalation is used
frequently in databases that consider a lock to be a
scare resource and overhead to be avoided.

 The fact that a database supports lock escalation, is


performed to manage hundreds of locks.

Lock Types

1. DML Locks

 DML(data manipulation language), In general


SELECT,INSERT,UPDATE and DELETE.

 DML locks will be locks on a specific row of data, or


a lock at the table level, which locks every row in
the table.
https://easy2learncode.in Page 17
RDBMS Using Oracle unit-3

 This lock prevents other processes from updating(or


locking) the row.

 This lock is released only when the locking process


successfully commits the transaction to the
database(i.e., updates data) or when the process is
rolled back.

2. DDL Locks

 Data definition language (DDL), in general


CREATE, ALTER, AND so on.

 DDL locks protect the definition of the structure of


objects.

 This lock is placed over the table to prevent


structural alterations to the table.

 For ex. This type of lock keeps the DBA (database


administrator) from being able to remove a table by
issuing a DROP statement against the table.

 This lock is released only when the locking process


successfully commits the transaction to the
database or when the process is rolled back.

https://easy2learncode.in Page 18
RDBMS Using Oracle unit-3

 DDL locks are automatically placed against objects


during a DDL operation to protect them from
changes by other sessions.

3. Latches
 Latches are locks that are held for short period of
time,

 For ex., the time it takes to modify an in-memory


data structure. They are used to protect certain
memory structures such as the database block
buffer cache or the library cache in the shared pool.

 They do not support queuing and do not protect


database objects such as tables or data files.

 It is possible to use manual locking using the FOR


UPDATE statement or LOCK TABLE statement, or
you can create your own locks by using the
DBMS_LOCK package.

4. Manual Locking and User-Defined Locks

Manual Locking:
 The SELECT…FOR UPDATE statement is the
predominant method of manually locking data.

https://easy2learncode.in Page 19
RDBMS Using Oracle unit-3

 We can also manually lock data using the LOCK


TABLE statement.

 This statement is used rarely, because of the


coarseness of the lock.

 It simply locks the table, not the rows in the table.

 If you start modifying the rows, they will be locked


as normal. So, this is not a method to save on
resource.

CREATING YOUR OWNLOCKS:


 Oracle actually exposes to developers the enqueue
lock mechanism that it uses internally, via the
DBMS_LOCK package.

 You might use this package to serialize access to


some resource external to oracle.

 The DBMS_LOCK package allows you to manually


release a lock when you are done with it, or to give
it up automatically when you commit, or even to
keep it as long as you are logged in.

https://easy2learncode.in Page 20
RDBMS Using Oracle unit-3

Follow Us :

https://easy2learncode.in Page 21

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy