introduction to the oracle dbms (29)

72
Introduction to the Oracle DBMS Kristian Torp Department of Computer Science Aalborg University www.cs.aau.dk/˜torp [email protected] December 2, 2011 daisy.aau.dk Kristian T orp (Aalborg Universit y) Introduction to the Oracle DBMS De ce mber 2, 2011 1 / 72

Upload: hassan-shahzad-aheer

Post on 06-Apr-2018

233 views

Category:

Documents


0 download

TRANSCRIPT

8/3/2019 Introduction to the Oracle Dbms (29)

http://slidepdf.com/reader/full/introduction-to-the-oracle-dbms-29 1/72

Introduction to the Oracle DBMS

Kristian Torp

Department of Computer ScienceAalborg University

www.cs.aau.dk/˜torp

[email protected]

December 2, 2011

daisy.aau.dk

Kristian Torp (Aalborg University) Introduction to the Oracle DBMS December 2, 2011 1 / 72

8/3/2019 Introduction to the Oracle Dbms (29)

http://slidepdf.com/reader/full/introduction-to-the-oracle-dbms-29 2/72

Outline

1 Users

2 The Data Dictionary

3 General Stuff

Kristian Torp (Aalborg University) Introduction to the Oracle DBMS December 2, 2011 2 / 72

8/3/2019 Introduction to the Oracle Dbms (29)

http://slidepdf.com/reader/full/introduction-to-the-oracle-dbms-29 3/72

Learning Goals

Prerequisites

Knowledge about basic database concepts, e.g., table and index

Basic knowledge of SQL

Learning Goals

Use the Oracle DBMS

Know where Oracle different from standard or other DBMSs

Know how to find database metadata

Kristian Torp (Aalborg University) Introduction to the Oracle DBMS December 2, 2011 3 / 72

8/3/2019 Introduction to the Oracle Dbms (29)

http://slidepdf.com/reader/full/introduction-to-the-oracle-dbms-29 4/72

Outline

1 Users

2 The Data Dictionary

3 General Stuff

Kristian Torp (Aalborg University) Introduction to the Oracle DBMS December 2, 2011 4 / 72

8/3/2019 Introduction to the Oracle Dbms (29)

http://slidepdf.com/reader/full/introduction-to-the-oracle-dbms-29 5/72

Disclaimer

For Testing Only!

Examples in the following are only for test machines!

Should not be used in a production environment!

For Production

Contact your local DBA

Read the manual on how to make a secure environment

Kristian Torp (Aalborg University) Introduction to the Oracle DBMS December 2, 2011 5 / 72

8/3/2019 Introduction to the Oracle Dbms (29)

http://slidepdf.com/reader/full/introduction-to-the-oracle-dbms-29 6/72

Overview

Supplied Users

SYS, the “root” user of the Oracle DBMS

SYSTEM, has database administration rights

Note

It is a good idea to create a separate user for your database

How to create users is different on different DBMSs

There are nice GUI tools for creating users

Kristian Torp (Aalborg University) Introduction to the Oracle DBMS December 2, 2011 6 / 72

8/3/2019 Introduction to the Oracle Dbms (29)

http://slidepdf.com/reader/full/introduction-to-the-oracle-dbms-29 7/72

New DBA User

User Requirements

Create a new super user on the Oracle DBMS

The new user must not be one of the supplied users.

Example (Create a Super User)

−− l o g i n as system ( p r e d e f in e d s up er u se r )s q l p l u s system@<system i d e n t i f i e r >

−− C r ea t e a new s u pe r u se r , make n i c e r p as sw or d

c r e at e u se r dbadm i d e n t i f i e d by dbadm ;

−− g ra nt r i g h t s

g r a n t dba t o dbadm ;

−− l o g o u te x i t ;

−− l o g i n as new dba

s q l p l u s dbadm / dbadm@<system i d e n t i f i e r >

−− Now change your password

Kristian Torp (Aalborg University) Introduction to the Oracle DBMS December 2, 2011 7 / 72

8/3/2019 Introduction to the Oracle Dbms (29)

http://slidepdf.com/reader/full/introduction-to-the-oracle-dbms-29 8/72

New User

User RequirementsCreate a new user on the Oracle DBMS no administration rights

Should be able to create tables and query them

Example (Create a Super User)−− l o g i n as system

s q l p l u s dbadm@<system i d e n t i f i e r >

−− C r ea t e a new u s e r

c r e at e u se r dbuser i d e n t i f i e d by dbuser ;

−− g ra nt r i g h t s t o new u ser

g r a n t c on ne ct , r e so u rc e t o dbuser ;−− c o n ne c t as new u s e r

connect dbuse r / dbuse r

−− Now change your password

Kristian Torp (Aalborg University) Introduction to the Oracle DBMS December 2, 2011 8 / 72

8/3/2019 Introduction to the Oracle Dbms (29)

http://slidepdf.com/reader/full/introduction-to-the-oracle-dbms-29 9/72

Drop Users

Example (Drop New Super User and Plain Users)−− l o g i n as system

s q l p l u s system@<system i d e n t i f i e r >

−− d ro p t h e u se rs c r e at e d above

d ro p u s er dbuser ;

Note

All schema object owned by the dropped users are also dropped!

You cannot drop the user currently used

Kristian Torp (Aalborg University) Introduction to the Oracle DBMS December 2, 2011 9 / 72

8/3/2019 Introduction to the Oracle Dbms (29)

http://slidepdf.com/reader/full/introduction-to-the-oracle-dbms-29 10/72

Outline

1 Users

2 The Data Dictionary

3 General Stuff

Kristian Torp (Aalborg University) Introduction to the Oracle DBMS December 2, 2011 10 / 72

8/3/2019 Introduction to the Oracle Dbms (29)

http://slidepdf.com/reader/full/introduction-to-the-oracle-dbms-29 11/72

Overview of Data Dictionary

The idea in the naming

Name Description

all [rest of name] All the stuff that the current user can see

dba [rest of name] All the stuff in the database

user [rest of name] All the stuff that the current user owns

user ⊆ all ⊆ dba

Access Rights

It is possible that you do not have access to the ’dba’ views.

Example (Info. on Indexes)s e l e c t ∗

from a l l i n d e x e s

Example (Info. on Views)s e l e c t ∗

from u s e r v i e ws

Kristian Torp (Aalborg University) Introduction to the Oracle DBMS December 2, 2011 11 / 72

8/3/2019 Introduction to the Oracle Dbms (29)

http://slidepdf.com/reader/full/introduction-to-the-oracle-dbms-29 12/72

What Stuff is in the DBMS

Example (Objects users have in the database)s e l e c t ao . owner , ao . o b j e c t t y p e , count ( ∗ ) as c n t

from a l l o b j e c t s aogroup by ao . owner , ao . o b j e c t t y p e

o r d e r by c n t desc

Note

The all objects lists all objects stored in the databaseThe object type is, e.g., tables, indexes, java classes, and synonyms

Example (Object Types)s e l e c t d i s t i n c t o b j e c t t y p e

from a l l o b j e c t s

Result

OBJECT TYPETABLE

INDEX

SYNONYM

..

Kristian Torp (Aalborg University) Introduction to the Oracle DBMS December 2, 2011 12 / 72

8/3/2019 Introduction to the Oracle Dbms (29)

http://slidepdf.com/reader/full/introduction-to-the-oracle-dbms-29 13/72

Outline

1 Users

2 The Data Dictionary

3 General Stuff

Kristian Torp (Aalborg University) Introduction to the Oracle DBMS December 2, 2011 13 / 72

8/3/2019 Introduction to the Oracle Dbms (29)

http://slidepdf.com/reader/full/introduction-to-the-oracle-dbms-29 14/72

The Unix ls command

Example (Show me the stuff)s e l e c t ∗

from ca t ;

Note

cat is short for catalog

A view with two columns table name and table type

Contains tables, views, sequences (but not itself!)

Kristian Torp (Aalborg University) Introduction to the Oracle DBMS December 2, 2011 14 / 72

8/3/2019 Introduction to the Oracle Dbms (29)

http://slidepdf.com/reader/full/introduction-to-the-oracle-dbms-29 15/72

Limiting the Number of Rows Returned

Example (Give me the first ten rows)s e l e c t ∗

from c a t

where rownum<

= 10

Note

The rownum pseudo column is Oracle specific on other DBMSs it is

called limit

Kristian Torp (Aalborg University) Introduction to the Oracle DBMS December 2, 2011 15 / 72

O li

8/3/2019 Introduction to the Oracle Dbms (29)

http://slidepdf.com/reader/full/introduction-to-the-oracle-dbms-29 16/72

Outline

4 Tables

5 Columns

6 Constraints

7 Views

Kristian Torp (Aalborg University) Introduction to the Oracle DBMS December 2, 2011 16 / 72

A il bl T bl

8/3/2019 Introduction to the Oracle Dbms (29)

http://slidepdf.com/reader/full/introduction-to-the-oracle-dbms-29 17/72

Available Tables

Example (Look at all my tables)s e l e c t ∗

from u s e r t a b l e s ;

Example (Look at all tables available to me)s e l e c t ∗

from a l l t a b l e s ;

Example (Find tables that are non-local)s e l e c t ∗

from a l l t a b l e s

except

s e l e c t ∗from u s e r t a b l e s ;

Note

Not union compatible!

Kristian Torp (Aalborg University) Introduction to the Oracle DBMS December 2, 2011 17 / 72

Th d l T bl

8/3/2019 Introduction to the Oracle Dbms (29)

http://slidepdf.com/reader/full/introduction-to-the-oracle-dbms-29 18/72

The dual Table

Example (Find the current time)s e l e c t c u r re n t t i m es t a mp

from d u a l

Note

A utility table in Oracle because the from clause is required

Has one column dummy

Has one row (with the value X)

Uses the built-in function current timestamp

Cultural format, e.g., 2012-02-28 12:34:56 vs. 02-28-2012 12:34:56

Uses the dual table

Kristian Torp (Aalborg University) Introduction to the Oracle DBMS December 2, 2011 18 / 72

S C ti I

8/3/2019 Introduction to the Oracle Dbms (29)

http://slidepdf.com/reader/full/introduction-to-the-oracle-dbms-29 19/72

Space Consumption I

Example (How much space are my tables taking up?)s e l e c t us . segment name as ” t a bl e name” ,

b y t e s / 1 0 2 4 as ”KB”

from u s er s e g m en t s us

where us . s e gm e n t t y p e = ’TABLE ’o r d e r by KB desc ;

Note

Note the word ’TABLE’ has to be in upper case

Kristian Torp (Aalborg University) Introduction to the Oracle DBMS December 2, 2011 19 / 72

Space Consumption II

8/3/2019 Introduction to the Oracle Dbms (29)

http://slidepdf.com/reader/full/introduction-to-the-oracle-dbms-29 20/72

Space Consumption II

Example (How much space are my tables taking up (take two)?)

Another way to calculate size http://askanantha.blogspot.com/

2007/09/get-oracle-table-size.html

s e l e c t segment name as table name ,sum( b y t es ) / ( 1 0 2 4 ) as t a b l e s i z e k b

from u s e r e x t e n t s

where s e gm e nt t y pe = ’TABLE ’

group by segment name ;

Kristian Torp (Aalborg University) Introduction to the Oracle DBMS December 2, 2011 20 / 72

Space Consumption III

8/3/2019 Introduction to the Oracle Dbms (29)

http://slidepdf.com/reader/full/introduction-to-the-oracle-dbms-29 21/72

Space Consumption III

Example (How much space are my tables taking up (take three)?)Size of table based on rows http://askanantha.blogspot.com/

2007/09/get-oracle-table-size.html

s e l e c t t ab le n a me , ( a v g r o w l e n ∗num rows ) / ( 1 0 2 4 )

from u s e r t a b l e s ;

Example (Show more information on table size calculation)s e l e c t table name ,

a v g r o w l e n ,

num rows ,

( a v g r o w l e n ∗num rows ) / ( 1 0 2 4 ) ” s i z e kb ”from u s e r t a b l e s ;

Kristian Torp (Aalborg University) Introduction to the Oracle DBMS December 2, 2011 21 / 72

Create a SQL Script to drop all Tables I

8/3/2019 Introduction to the Oracle Dbms (29)

http://slidepdf.com/reader/full/introduction-to-the-oracle-dbms-29 22/72

Create a SQL Script to drop all Tables I

Example (Drop Single Table)

drop t a b l e tab ;

Note

Time consuming when many tables

Example (A script that drops all table)s e l e c t ’ d rop t ab l e ’ | | ua . t able name | | ’ ; ’

from u s e r t a bl e s ua ;

Note|| is the string concatenation operator in Oracle

’ hello , ’ || ’ world! ’ = ’ hello , world! ’

Recall keywords case insensitive in SQL

Kristian Torp (Aalborg University) Introduction to the Oracle DBMS December 2, 2011 22 / 72

Create a SQL Script to drop all Tables II

8/3/2019 Introduction to the Oracle Dbms (29)

http://slidepdf.com/reader/full/introduction-to-the-oracle-dbms-29 23/72

Create a SQL Script to drop all Tables II

User Requirements

All names in lower case in scripts

Example (A script that drops all tables (all lower case))s e l e c t ’ d rop t ab l e ’ | | l o w e r ( ua . t able name ) | | ’ ; ’

from u s e r t a bl e s ua ;

Note

The functionlower

converts a character string to lower case

Kristian Torp (Aalborg University) Introduction to the Oracle DBMS December 2, 2011 23 / 72

Create a SQL Script to drop all Tables III

8/3/2019 Introduction to the Oracle Dbms (29)

http://slidepdf.com/reader/full/introduction-to-the-oracle-dbms-29 24/72

Create a SQL Script to drop all Tables III

Example (Store the drop script in a file)s e t h ea di ng o f f −− do not show t he column names

s e t f ee db ac k o f f −− do n ot w r i t e n r ows r et ur ne d a t t h e end

s e t pagesize 50000 −− make t h e page s i z e v e ry l a r g e

−− send o u tp u t t o a f i l e

s p o o l c : \ d r o p t a b l e s . s q l

−− The a c t u a l q uer y t o e xe cu te

s e l e c t ’ d rop t ab l e ’| |

l o w e r ( ua . t able name )| |

’ ; ’from u s e r t a bl e s ua ;

s po ol o f f −− s t o p w r i t i n g t o a f i l e

Example (Execute the commands in the file from within SQL∗Plus)SQL>@c: \ d r o p t a b l e s . s q l

Note

Assume file is stored in c: drop files.sql

Kristian Torp (Aalborg University) Introduction to the Oracle DBMS December 2, 2011 24 / 72

Outline

8/3/2019 Introduction to the Oracle Dbms (29)

http://slidepdf.com/reader/full/introduction-to-the-oracle-dbms-29 25/72

Outline

4 Tables

5 Columns

6 Constraints

7 Views

Kristian Torp (Aalborg University) Introduction to the Oracle DBMS December 2, 2011 25 / 72

Columns for a Table

8/3/2019 Introduction to the Oracle Dbms (29)

http://slidepdf.com/reader/full/introduction-to-the-oracle-dbms-29 26/72

Columns for a Table

Example (The columns on the student table)s e l e c t ∗

from u s er t a b c o l s u tc

where u t c . t a bl e n a m e = ’STUDENT ’

Note

Internally all identifiers are in upper case

Kristian Torp (Aalborg University) Introduction to the Oracle DBMS December 2, 2011 26 / 72

Finding Specific Columns Based on Name

8/3/2019 Introduction to the Oracle Dbms (29)

http://slidepdf.com/reader/full/introduction-to-the-oracle-dbms-29 27/72

Finding Specific Columns Based on Name

Example (Columns names that include the string ID)s e l e c t ut c . ∗

from u s er t a b c o l s u tc

where ut c . column name l i k e ’%ID%’

Note

like is simple string matching and % is multi-character wildcard

Recall that internally in Oracle all identifiers are in upper case

Kristian Torp (Aalborg University) Introduction to the Oracle DBMS December 2, 2011 27 / 72

Space Consumption Column Values

8/3/2019 Introduction to the Oracle Dbms (29)

http://slidepdf.com/reader/full/introduction-to-the-oracle-dbms-29 28/72

Space Consumption Column Values

Example (Find the number of bytes needed to store a date)s e l e c t v s i z e ( date )

from dual ;

Example (Find the number of bytes needed to store an interval)s e l e c t v s i z e ( i n t e r v a l ’ 37 12 ’ day t o ho ur )

from dual ;

Example (Find the number of bytes needed to store a float)

s e l e c t vsize(123456789012345.34567890)from dual ;

Kristian Torp (Aalborg University) Introduction to the Oracle DBMS December 2, 2011 28 / 72

Data Types for Columns

8/3/2019 Introduction to the Oracle Dbms (29)

http://slidepdf.com/reader/full/introduction-to-the-oracle-dbms-29 29/72

Data Types for Columns

Example (Find the most used data type for columns)s e l e c t u t c . d a t a t y p e , count ( ∗ ) as n o c o l s

from u s er t a b c o l s u tcgroup by u t c . d a t a t y p e

o r d e r by n o c o l s desc

Kristian Torp (Aalborg University) Introduction to the Oracle DBMS December 2, 2011 29 / 72

Outline

8/3/2019 Introduction to the Oracle Dbms (29)

http://slidepdf.com/reader/full/introduction-to-the-oracle-dbms-29 30/72

Outline

4 Tables

5 Columns

6 Constraints

7 Views

Kristian Torp (Aalborg University) Introduction to the Oracle DBMS December 2, 2011 30 / 72

Finding Primary Keys

8/3/2019 Introduction to the Oracle Dbms (29)

http://slidepdf.com/reader/full/introduction-to-the-oracle-dbms-29 31/72

g y y

Example (Name of tables that have a primary key)s e l e c t uc . t able name

from u s e r c o n s t r a i nt s uc

where uc . c o n s t r a i n t t y p e = ’P ’

Constraint Types

Constraint Type Description

P Primary key

R Referential Integrity (foreign key)

C Check constraint

Kristian Torp (Aalborg University) Introduction to the Oracle DBMS December 2, 2011 31 / 72

Missing Primary Keys

8/3/2019 Introduction to the Oracle Dbms (29)

http://slidepdf.com/reader/full/introduction-to-the-oracle-dbms-29 32/72

g y y

Example (Tables with no primary key)s e l e c t t able name

from u s e r t a b l e s

where t able name n o t i n ( s e l e c t t able name

from u s e r c o n s t r a i n t swhere c o n s t r a i n t t y p e = ’P ’ ) ;

Action to Take

Now you should add the missing primary keys or face the consequences!

Kristian Torp (Aalborg University) Introduction to the Oracle DBMS December 2, 2011 32 / 72

Constraints on Which Columns

8/3/2019 Introduction to the Oracle Dbms (29)

http://slidepdf.com/reader/full/introduction-to-the-oracle-dbms-29 33/72

Example (Columns associated with a constraint)s e l e c t ∗

from u s er c o ns c o lu m ns ucc

where u cc . t a bl e n a m e = ’STUDENT ’

Note

Only for the Student table

All types of constraints are used

Kristian Torp (Aalborg University) Introduction to the Oracle DBMS December 2, 2011 33 / 72

Composite Primary Keys

8/3/2019 Introduction to the Oracle Dbms (29)

http://slidepdf.com/reader/full/introduction-to-the-oracle-dbms-29 34/72

p y y

Example (Find tables with a composite primary key)s e l e c t uc . table name , count ( ucc . column name )

from u se r co ns c ol um ns ucc , u s e r c o n s t r a i n t s uc

where uc . c o n s t r a i n t t y p e = ’P ’ −− p r i m ar y keyand uc . owner = u cc . owner

and uc . c o n s tr a i n t n a m e = ucc . c o n s tr a i n t n a m e

group by uc . t able name

h a vi n g c o un t ( ucc . column name ) > 1

Kristian Torp (Aalborg University) Introduction to the Oracle DBMS December 2, 2011 34 / 72

Outline

8/3/2019 Introduction to the Oracle Dbms (29)

http://slidepdf.com/reader/full/introduction-to-the-oracle-dbms-29 35/72

4 Tables

5 Columns

6 Constraints

7 Views

Kristian Torp (Aalborg University) Introduction to the Oracle DBMS December 2, 2011 35 / 72

Which Views are Defined

8/3/2019 Introduction to the Oracle Dbms (29)

http://slidepdf.com/reader/full/introduction-to-the-oracle-dbms-29 36/72

Example (Show all views)

s e l e c t ∗from u s e r v i e w s

Kristian Torp (Aalborg University) Introduction to the Oracle DBMS December 2, 2011 36 / 72

View Implementation

8/3/2019 Introduction to the Oracle Dbms (29)

http://slidepdf.com/reader/full/introduction-to-the-oracle-dbms-29 37/72

Example (The select statements used for creating the view)s e l e c t uv . t e x t

from u s er v i e ws uv

Note

The text is a long column (an old type of clob) therefore the text may not

be directly visible. Click on the field to see the content in most IDEs.

Kristian Torp (Aalborg University) Introduction to the Oracle DBMS December 2, 2011 37 / 72

Outline

8/3/2019 Introduction to the Oracle Dbms (29)

http://slidepdf.com/reader/full/introduction-to-the-oracle-dbms-29 38/72

8 Sequences

9 Directories and Files

10 Synonyms

11 Comments

Kristian Torp (Aalborg University) Introduction to the Oracle DBMS December 2, 2011 38 / 72

Which Sequences are Available

8/3/2019 Introduction to the Oracle Dbms (29)

http://slidepdf.com/reader/full/introduction-to-the-oracle-dbms-29 39/72

Example (Find all sequences)s e l e c t ∗

from user sequences

Example (Find the last number used from the sequence)s e l e c t us . l a s t n u m b e r

from u s er s e q u en c es us

Kristian Torp (Aalborg University) Introduction to the Oracle DBMS December 2, 2011 39 / 72

Outline

8/3/2019 Introduction to the Oracle Dbms (29)

http://slidepdf.com/reader/full/introduction-to-the-oracle-dbms-29 40/72

8 Sequences

9 Directories and Files

10 Synonyms

11 Comments

Kristian Torp (Aalborg University) Introduction to the Oracle DBMS December 2, 2011 40 / 72

Examples

8/3/2019 Introduction to the Oracle Dbms (29)

http://slidepdf.com/reader/full/introduction-to-the-oracle-dbms-29 41/72

Example (Create a Directory Alias)

c re at e d i r e c t o r y f i l e s t u f f as ’ c : \ tmp ’ ;

Note

Directory alias file stuff is stored and used in upper case FILE STUFF

Directory name is case sensitive

No warning if directory does not exists on server side

Example (Create a Stupid Directory Alias)c re at e d i r e c t o r y dummy as ’ c : \ DoesNotExist ’ ;

Note

Create a directory alias to a non-existing directory

Kristian Torp (Aalborg University) Introduction to the Oracle DBMS December 2, 2011 41 / 72

Overview: Directories

8/3/2019 Introduction to the Oracle Dbms (29)

http://slidepdf.com/reader/full/introduction-to-the-oracle-dbms-29 42/72

Idea

An alias for a directory on the server  file system

Can be used to read from and write to files

Must have the create any directory to create directories

Directory aliases can be use for external tables

If the directory is drop in the operating system, a run-time error is

raised if alias is used.

Kristian Torp (Aalborg University) Introduction to the Oracle DBMS December 2, 2011 42 / 72

List Directory Aliases

8/3/2019 Introduction to the Oracle Dbms (29)

http://slidepdf.com/reader/full/introduction-to-the-oracle-dbms-29 43/72

Examples e l e c t ∗

from a l l d i r e c t o r i e s ;

Note

Describes all directories aliases accessible to the current user

View is in the sys schema

Kristian Torp (Aalborg University) Introduction to the Oracle DBMS December 2, 2011 43 / 72

Grant

8/3/2019 Introduction to the Oracle Dbms (29)

http://slidepdf.com/reader/full/introduction-to-the-oracle-dbms-29 44/72

Example (Grant Read/Write Access)g r a n t r ead , w r i t e on d i r e c t o r y f i l e s t u f f t o <user name> ;

Note

Provides both read and write rights the directory alias

Example (Grant Read-Only Access)g ra nt read on d i r e c t o r y f i l e s t u f f t o <user name> ;

NoteProvides only read rights the directory alias

Kristian Torp (Aalborg University) Introduction to the Oracle DBMS December 2, 2011 44 / 72

Write A String to A File

8/3/2019 Introduction to the Oracle Dbms (29)

http://slidepdf.com/reader/full/introduction-to-the-oracle-dbms-29 45/72

c r e at e o r r e pl a c e p ro ce du re s t r 2 f i l e ( d ir n am e varchar2 ,

f i l e n a m e varchar2 ,

s t r v a r c h a r 2 )as

v f i l e u t l f i l e . f i l e t y p e ;

begin

v f i l e : = u t l f i l e . fo pen ( d ir n am e , f i l e n a me , ’w ’ ) ;

u t l f i l e . p u t l i n e ( v f i l e , s t r ) ;

u t l f i l e . f c l o s e ( v f i l e ) ;e x c e p t i o n

when o t h e r s t h en

i f u t l f i l e . i s o p e n ( v f i l e ) then

u t l f i l e . f c l o s e ( v f i l e ) ;

end i f ;

end ;

Example (Called the Stored Procedure)execute s t r 2 f i l e ( ’FILE STUFF ’ , ’ n e w f i l e . t x t ’ , ’ H e l l o F i l e ! ’ ) ;

Kristian Torp (Aalborg University) Introduction to the Oracle DBMS December 2, 2011 45 / 72

Convert a Text file to a clob Il f i f i l 2 l b ( d i h 2

8/3/2019 Introduction to the Oracle Dbms (29)

http://slidepdf.com/reader/full/introduction-to-the-oracle-dbms-29 46/72

c re at e o r r ep la ce f u n c t io n f i l e 2 c l o b ( d ir n am e varchar2 ,

f i l e n a m e v a r c h a r 2 )

r e t u r n c l o b i s

v f i l e u t l f i l e . f i l e t y p e ; v c l o b c l o b ;

v l i n e v a r c h a r 2 ( 2 0 0 0 ) ; v l e n g t h i n t ;

begin

dbms lob . crea t et emp orar y ( v c lo b , f a l s e ) ;

v f i l e : = u t l f i l e . fo pen ( d ir n am e , f i l e n a me , ’ r ’ ) ;

begin

l o o pu t l f i l e . g e t l i n e ( v f i l e , v l i n e ) ;

v l e n g t h : = dbms l ob . g e t l e n g t h ( v c l o b ) + 1 ;

dbms lob . w r i t e ( v c lo b , l e ng t h ( v l i n e ) , v l en gt h , v l i n e ) ;

end l o o p ;

e x c e p t i o n when o t h e r s t h en

i f u t l f i l e . i s o p e n ( v f i l e ) thenu t l f i l e . f c l o s e ( v f i l e ) ;

end i f ;

end ;

r e t u r n v c l o b ;

Kristian Torp (Aalborg University) Introduction to the Oracle DBMS December 2, 2011 46 / 72

Convert a Text file to a clob II

8/3/2019 Introduction to the Oracle Dbms (29)

http://slidepdf.com/reader/full/introduction-to-the-oracle-dbms-29 47/72

Example (Use the Clob Conversion)−− Cr eate a t a b l e w i th a c l ob column

c re at e t a b l e withA Clob ( i i n t , l c l o b ) ;

−− Use t he new f u n c t i o n t o l oa d a f i l e i n t o a c l ob columkn

i n s e r t i n t o withAClob

v a l u e s ( 1 , f i l e 2 c l o b ( ’FILE STUFF ’ , ’ t e s t r e a d f i l e . t x t ’ ) ) ;

commit ;

−− Check f i l e i s ‘ ‘ u plo aded ’ ’ as a c l o b

s e l e c t ∗

from withAClob ;

Kristian Torp (Aalborg University) Introduction to the Oracle DBMS December 2, 2011 47 / 72

Updating/Moving a Directory

8/3/2019 Introduction to the Oracle Dbms (29)

http://slidepdf.com/reader/full/introduction-to-the-oracle-dbms-29 48/72

Example (Relocate Directory Alias)c re at e o r r ep la ce d i r e c t o r y f i l e s t u f f as ’ c : \ tmp \ d b f i l e s ’ ;

Note

The directory alias has now been relocated

Example (Stupid Relocation)c re at e o r r ep la ce d i r e c t o r y f i l e s t u f f as ’ / un ix / path / dos / machine ’ ;

NoteNo warning/error when creating non-existing directory

Kristian Torp (Aalborg University) Introduction to the Oracle DBMS December 2, 2011 48 / 72

Dropping

8/3/2019 Introduction to the Oracle Dbms (29)

http://slidepdf.com/reader/full/introduction-to-the-oracle-dbms-29 49/72

Example (Drop a Directory)drop d i r e c t o r y f i l e s t u f f ;

Note

The directory alias is now removed

Kristian Torp (Aalborg University) Introduction to the Oracle DBMS December 2, 2011 49 / 72

Outline

8/3/2019 Introduction to the Oracle Dbms (29)

http://slidepdf.com/reader/full/introduction-to-the-oracle-dbms-29 50/72

8 Sequences

9 Directories and Files

10 Synonyms

11

Comments

Kristian Torp (Aalborg University) Introduction to the Oracle DBMS December 2, 2011 50 / 72

Introduction

8/3/2019 Introduction to the Oracle Dbms (29)

http://slidepdf.com/reader/full/introduction-to-the-oracle-dbms-29 51/72

Concept

A synonym is an alias, such that it is simpler to refer to databaseobjects own by another user.

Example (List all non-public synonyms)

s e l e c t ∗from a ll s yn on ym s a l s

where al s . owner <> ’PUBLIC ’ ;

Note

A public synonym is visible to all database usersPublic synonyms are used heavily by the Oracle DBMS to provide

access to meta data.

Kristian Torp (Aalborg University) Introduction to the Oracle DBMS December 2, 2011 51 / 72

Synonyms for Tables

8/3/2019 Introduction to the Oracle Dbms (29)

http://slidepdf.com/reader/full/introduction-to-the-oracle-dbms-29 52/72

Example

Find the synonym name plus the base table for a public synonym that

is a table and starts with a ’D’

s e l e c t al s . synonym name , al s . t able owner , a l s . t able name

from a ll s yn on ym s a l s

where a l s . owner = ’ PUBLIC ’

and a l s . synonym name l i k e ’D%’and e x i s t s ( s e l e c t ’ x ’

from a l l t a b l e s a t t

where a t t . owner = a l s . t a b l e o w n e r

and a t t . t ab l e n am e = a l s . t ab l e n am e ) ;

Note

The dual is return. This means that dual is real table and not a view.

Kristian Torp (Aalborg University) Introduction to the Oracle DBMS December 2, 2011 52 / 72

Outline

8/3/2019 Introduction to the Oracle Dbms (29)

http://slidepdf.com/reader/full/introduction-to-the-oracle-dbms-29 53/72

8 Sequences

9 Directories and Files

10 Synonyms

11

Comments

Kristian Torp (Aalborg University) Introduction to the Oracle DBMS December 2, 2011 53 / 72

Comments on Tables

8/3/2019 Introduction to the Oracle Dbms (29)

http://slidepdf.com/reader/full/introduction-to-the-oracle-dbms-29 54/72

Example (Find all tables with comments)s e l e c t ut c . ∗

from u se r t ab c o mm en ts u t cwhere u t c . comments i s n o t n u l l

Kristian Torp (Aalborg University) Introduction to the Oracle DBMS December 2, 2011 54 / 72

Column Comments

8/3/2019 Introduction to the Oracle Dbms (29)

http://slidepdf.com/reader/full/introduction-to-the-oracle-dbms-29 55/72

Example (Find column comments on the Student table)s e l e c t ucc . column name , ucc . comments

from u s e r c o l c om m e n ts u ccwhere u cc . t a bl e n a m e = ’STUDENT ’

Kristian Torp (Aalborg University) Introduction to the Oracle DBMS December 2, 2011 55 / 72

Outline

8/3/2019 Introduction to the Oracle Dbms (29)

http://slidepdf.com/reader/full/introduction-to-the-oracle-dbms-29 56/72

12 Stored Procedures

13 Triggers

14 Timing

Kristian Torp (Aalborg University) Introduction to the Oracle DBMS December 2, 2011 56 / 72

The Unix ls command I

Example (Create the ls command as a stored procedure)

8/3/2019 Introduction to the Oracle Dbms (29)

http://slidepdf.com/reader/full/introduction-to-the-oracle-dbms-29 57/72

Example (Create the ls command as a stored procedure)c r e at e o r r e pl a c e p ro ce du re l s i s

c u r s o r c t a b l e s i s

s e l e c t ∗ from ca t ;v t a b l e n a m e c a t . t a bl e n a m e%t ype ;

v t y p e c a t . t a b l e t y p e%t ype ;

begin

open c t a b l e s ;

l o o p

f e t c h c t a b l e s i n t o v t ab le n am e , v t y p e ;

e x i t when c t a b l e s %not f ound ;

p ( v t a b l e n a m e ) ;

end l o o p ;

c l o s e c t a b l e s ;

end ;

 / 

Note

The slash ’/’ at the end

Kristian Torp (Aalborg University) Introduction to the Oracle DBMS December 2, 2011 57 / 72

The Unix ls command II

8/3/2019 Introduction to the Oracle Dbms (29)

http://slidepdf.com/reader/full/introduction-to-the-oracle-dbms-29 58/72

Example (Executed the stored procedure)−− t o enabl e o u tp ut from t he s er v er

s e t s e r v e r o u t p u t on

−− exec i s s h or t f o r executeexec l s ;

Kristian Torp (Aalborg University) Introduction to the Oracle DBMS December 2, 2011 58 / 72

Space Usage Function I

8/3/2019 Introduction to the Oracle Dbms (29)

http://slidepdf.com/reader/full/introduction-to-the-oracle-dbms-29 59/72

User Requirements

Create a stored procedure that returns the space usage for a table

Must take the table name as input and return the size in KB

Kristian Torp (Aalborg University) Introduction to the Oracle DBMS December 2, 2011 59 / 72

Space Usage Function II

8/3/2019 Introduction to the Oracle Dbms (29)

http://slidepdf.com/reader/full/introduction-to-the-oracle-dbms-29 60/72

Examplec re at e o r r ep la ce f u n c t io n space usage

( p t a b l e n a m e i n u s e r t a b l e s . t a bl e n a m e%t y p e )

r e t u r n number i s

v s i z e k b number ;

begin

s e l e c t ( u t . a v g r o w l e n∗

u t . num rows ) / ( 1 0 2 4 )i n t o v s i z e k b

from u s er t a bl e s u t

where u t . t a bl e n a m e = upper ( p t a b l e n a m e ) ;

r e t u r n v s i z e k b ;

end ; / 

Kristian Torp (Aalborg University) Introduction to the Oracle DBMS December 2, 2011 60 / 72

Space Usage Function III

8/3/2019 Introduction to the Oracle Dbms (29)

http://slidepdf.com/reader/full/introduction-to-the-oracle-dbms-29 61/72

NoteThe into keyword

The upper keyword

The return keyword

It is very important that parameter names, e.g., p table name aredifferent from any colum name

The code convention is to prefix all parameter names with ’p ’

There is no error handling here at all Should be added in a production version

Kristian Torp (Aalborg University) Introduction to the Oracle DBMS December 2, 2011 61 / 72

Space Usage Function IV

Example (Executed the function)

8/3/2019 Introduction to the Oracle Dbms (29)

http://slidepdf.com/reader/full/introduction-to-the-oracle-dbms-29 62/72

Example (Executed the function)−− t o enabl e o u tp ut from t he s er v er

s e t s e r v e r o u t p u t on

d e c l a r e

v r v number ;

begin

v r v : = space usage ( ’ s t u d e n t ’ ) ;

d bm s o ut pu t . p u t l i n e ( ’ S ize s t u d e n t t a b l e i s ’ | | v r v | | ’ K B ’ ) ;

end ; / 

Note

Note allowed to use lower case table names, e.g., student

The return value has to be defined

The return is printed to the screen

The trailing slash

Kristian Torp (Aalborg University) Introduction to the Oracle DBMS December 2, 2011 62 / 72

See the Source Code

8/3/2019 Introduction to the Oracle Dbms (29)

http://slidepdf.com/reader/full/introduction-to-the-oracle-dbms-29 63/72

Example (Show source code for the ls stored procedure )s e l e c t us . t e x t

from u s e r s ou r c e us

where us . name = ’ LS ’

o r d e r by us . l i n e

Note

The text comes out in the corrected order due to the order by clause

Note the bad name convention there is a column name name. This

names overlaps with a reserved word

Kristian Torp (Aalborg University) Introduction to the Oracle DBMS December 2, 2011 63 / 72

Reserved Words in PL/SQL

8/3/2019 Introduction to the Oracle Dbms (29)

http://slidepdf.com/reader/full/introduction-to-the-oracle-dbms-29 64/72

Reserved Words

There is a huge number of reserved words in PL/SQL.They can (naturally) be queried

Example (List the Reserved Words)s e l e c t ∗

from v $ r e s er v e d w o r d s

Note

All v$ views are also called dynamic views

Access Rights

It is possible that you do not have access to the view v$reserved words

Kristian Torp (Aalborg University) Introduction to the Oracle DBMS December 2, 2011 64 / 72

Outline

8/3/2019 Introduction to the Oracle Dbms (29)

http://slidepdf.com/reader/full/introduction-to-the-oracle-dbms-29 65/72

12 Stored Procedures

13 Triggers

14 Timing

Kristian Torp (Aalborg University) Introduction to the Oracle DBMS December 2, 2011 65 / 72

Introduction

8/3/2019 Introduction to the Oracle Dbms (29)

http://slidepdf.com/reader/full/introduction-to-the-oracle-dbms-29 66/72

Example (List all tables with triggers)s e l e c t d i s t i n c t ut . table name

from u s e r t r i g g e r s u twhere u t . b a s e o b j e c t t y p e = ’TABLE ’

Kristian Torp (Aalborg University) Introduction to the Oracle DBMS December 2, 2011 66 / 72

Outline

8/3/2019 Introduction to the Oracle Dbms (29)

http://slidepdf.com/reader/full/introduction-to-the-oracle-dbms-29 67/72

12 Stored Procedures

13 Triggers

14 Timing

Kristian Torp (Aalborg University) Introduction to the Oracle DBMS December 2, 2011 67 / 72

Timing a Query

Example (From SQL∗Plus)

8/3/2019 Introduction to the Oracle Dbms (29)

http://slidepdf.com/reader/full/introduction-to-the-oracle-dbms-29 68/72

Example (From SQL∗Plus)s e t h ea di ng o f f

s e t pagesize 50000

s e t t er mo ut o f f

s p o o l c : \ t e s t . l s t

t i m i n g s t a r t

−− S t a r t query t o t im e

s e l e c t c . c a r i d , c . r o ad i d , c . speed

from c ar g p s a r r ay co r d e r by c a r i d desc ;

−− Stop q ue ry t o t im e

t i m i n g st op

s po ol o f f

s e t t e r m o u t on

Note

Output is spooled to a file

The result of the query is not displayed on screen

Kristian Torp (Aalborg University) Introduction to the Oracle DBMS December 2, 2011 68 / 72

Outline

8/3/2019 Introduction to the Oracle Dbms (29)

http://slidepdf.com/reader/full/introduction-to-the-oracle-dbms-29 69/72

15 Tablespace Usage

16 Additional Information

Kristian Torp (Aalborg University) Introduction to the Oracle DBMS December 2, 2011 69 / 72

Space Usage

8/3/2019 Introduction to the Oracle Dbms (29)

http://slidepdf.com/reader/full/introduction-to-the-oracle-dbms-29 70/72

Example (Space consumption for tablespaces)s e l e c t segment name , tablespace name , sum( b y t e s / ( 1 0 2 4 ∗ 1 0 2 4 ) ) ”MB”

from dba segments

where owner= ’TORP ’ and segment name no t l i k e ’ BIN ’

group by segment name , tablespace name

o r d e r by segment name , tablespace name ;

Note

Dropped tablespaces are not included

Dropped database objects are in the ’BIN’ segment

The size is in MB

Kristian Torp (Aalborg University) Introduction to the Oracle DBMS December 2, 2011 70 / 72

Outline

8/3/2019 Introduction to the Oracle Dbms (29)

http://slidepdf.com/reader/full/introduction-to-the-oracle-dbms-29 71/72

15 Tablespace Usage

16 Additional Information

Kristian Torp (Aalborg University) Introduction to the Oracle DBMS December 2, 2011 71 / 72

Web Sites

8/3/2019 Introduction to the Oracle Dbms (29)

http://slidepdf.com/reader/full/introduction-to-the-oracle-dbms-29 72/72

SitesOracle technical network otn.oracle.com

The official homepage good information but no critique

Morgan’s Library www.psoug.org/library.html

Many concrete examples, little or no explanationdbazine.com’s Oracle home page www.dbazine.com/oracle

Good examples

dbasupport.com’s www.dbasupport.com/oracle/

Overall good site with informaton on several DBMSs

Kristian Torp (Aalborg University) Introduction to the Oracle DBMS December 2, 2011 72 / 72