Introduction to the Oracle DBMS

Similar documents
Introduction to Triggers using SQL

Oracle Database: SQL and PL/SQL Fundamentals

Oracle Database: SQL and PL/SQL Fundamentals

Oracle Database 10g: Introduction to SQL

Oracle Database: SQL and PL/SQL Fundamentals NEW

Database Programming with PL/SQL: Learning Objectives

Managing Objects with Data Dictionary Views. Copyright 2006, Oracle. All rights reserved.

Oracle Database: SQL and PL/SQL Fundamentals NEW

Oracle 10g PL/SQL Training

Oracle SQL. Course Summary. Duration. Objectives

StreamServe Persuasion SP5 Oracle Database

Oracle Database 10g: Program with PL/SQL

SQL Server Database Coding Standards and Guidelines

IT2304: Database Systems 1 (DBS 1)

Oracle Data Dictionary

MyOra 3.0. User Guide. SQL Tool for Oracle. Jayam Systems, LLC

Oracle Database 10g Express

IT2305 Database Systems I (Compulsory)

3.GETTING STARTED WITH ORACLE8i

Optimizing with Open Source Technology Postgres

Duration Vendor Audience 5 Days Oracle End Users, Developers, Technical Consultants and Support Staff

SQL - QUICK GUIDE. Allows users to access data in relational database management systems.

Oracle Database 11g: SQL Tuning Workshop

Instant SQL Programming

Information Systems SQL. Nikolaj Popov

Normal Form vs. Non-First Normal Form

Migrating Non-Oracle Databases and their Applications to Oracle Database 12c O R A C L E W H I T E P A P E R D E C E M B E R

Oracle Database: Program with PL/SQL

More SQL: Assertions, Views, and Programming Techniques

Oracle Database: Program with PL/SQL

Oracle Database 12c: Introduction to SQL Ed 1.1

DBMoto 6.5 Setup Guide for SQL Server Transactional Replications

Top 10 Oracle SQL Developer Tips and Tricks

In this session, we use the table ZZTELE with approx. 115,000 records for the examples. The primary key is defined on the columns NAME,VORNAME,STR

2. Oracle SQL*PLUS Winter Some SQL Commands. To connect to a CS server, do:

Oracle. Brief Course Content This course can be done in modular form as per the detail below. ORA-1 Oracle Database 10g: SQL 4 Weeks 4000/-

news from Tom Bacon about Monday's lecture

D61830GC30. MySQL for Developers. Summary. Introduction. Prerequisites. At Course completion After completing this course, students will be able to:

Oracle Database: Develop PL/SQL Program Units

Topics Advanced PL/SQL, Integration with PROIV SuperLayer and use within Glovia

Oracle Database: Program with PL/SQL

Introduction to SQL and database objects

CHAPTER 2 DATABASE MANAGEMENT SYSTEM AND SECURITY

Migrate Topaz databases from One Server to Another

Database 10g Edition: All possible 10g features, either bundled or available at additional cost.

Oracle Database: Program with PL/SQL

Oracle Database 11g SQL

Oracle Architecture, Concepts & Facilities

1 Changes in this release

Oracle Database: Program with PL/SQL

A basic create statement for a simple student table would look like the following.

MyOra 3.5. User Guide. SQL Tool for Oracle. Kris Murthy

Oracle Database: Introduction to SQL

SQL Basics. Introduction to Standard Query Language

TrendWorX32 SQL Query Engine V9.2 Beta III

Porting from Oracle to PostgreSQL

An Oracle White Paper June Migrating Applications and Databases with Oracle Database 12c

Relational Database: Additional Operations on Relations; SQL

Oracle Database: Introduction to SQL

Configuring an Oracle Business Intelligence Enterprise Edition Resource in Metadata Manager

Oracle Database: Introduction to SQL

Embedded SQL. Unit 5.1. Dr Gordon Russell, Napier University

Oracle Database 11g Express Edition PL/SQL and Database Administration Concepts -II

Facebook Twitter YouTube Google Plus Website

Using SQL Server Management Studio

CSC 443 Data Base Management Systems. Basic SQL

SQL Developer. User Manual. Version Copyright Jan Borchers All rights reserved.

Oracle SQL*PLUS (For , Fall 2006) Prepared by: Mr. Nkulu Nkasa

ODBC Client Driver Help Kepware, Inc.

4 Simple Database Features

SQL*Plus s Forgotten History

Improving SQL Server Performance

Optimizing the Performance of the Oracle BI Applications using Oracle Datawarehousing Features and Oracle DAC

Retrieving Data Using the SQL SELECT Statement. Copyright 2006, Oracle. All rights reserved.

MyOra 4.5. User Guide. SQL Tool for Oracle. Kris Murthy

Informatica Corporation Proactive Monitoring for PowerCenter Operations Version 3.0 Release Notes May 2014

EMC Documentum Content Services for SAP iviews for Related Content

MOC 20461C: Querying Microsoft SQL Server. Course Overview

Review your answers, feedback, and question scores below. An asterisk (*) indicates a correct answer.

Introduction This document s purpose is to define Microsoft SQL server database design standards.

Business Portal for Microsoft Dynamics GP User s Guide Release 5.1

OLH: Oracle Loader for Hadoop OSCH: Oracle SQL Connector for Hadoop Distributed File System (HDFS)

Teradata Utilities Class Outline

Configuring an Alternative Database for SAS Web Infrastructure Platform Services

2874CD1EssentialSQL.qxd 6/25/01 3:06 PM Page 1 Essential SQL Copyright 2001 SYBEX, Inc., Alameda, CA

Cross Platform Transportable Tablespaces Migration in Oracle 11g

Module 2: Database Architecture

DBMS Questions. 3.) For which two constraints are indexes created when the constraint is added?

How To Create A Table In Sql (Ahem)

Oracle Insurance Policy Administration

Oracle Data Miner (Extension of SQL Developer 4.0)

Creating PL/SQL Blocks. Copyright 2007, Oracle. All rights reserved.

Documentum Business Process Analyzer and Business Activity Monitor Installation Guide for JBoss

Creating a Java application using Perfect Developer and the Java Develo...

Report and Dashboard Template User Guide

Netezza SQL Class Outline

PL/SQL MOCK TEST PL/SQL MOCK TEST I

Transcription:

Introduction to the Oracle DBMS Kristian Torp Department of Computer Science Aalborg University www.cs.aau.dk/ torp torp@cs.aau.dk December 2, 2011 daisy.aau.dk Kristian Torp (Aalborg University) Introduction to the Oracle DBMS December 2, 2011 1 / 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

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

Outline 1 Users 2 The Data Dictionary 3 General Stuff Kristian Torp (Aalborg University) Introduction to the Oracle DBMS December 2, 2011 4 / 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

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

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 ( predefined super user ) s q l p l u s system@<system i d e n t i f i e r > Create a new super user, make n i c e r password create user dbadm i d e n t i f i e d by dbadm ; grant r i g h t s grant dba to dbadm ; l ogout e 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

New User User Requirements Create 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 > Create a new user create user dbuser i d e n t i f i e d by dbuser ; grant r i g h t s to new user grant connect, resource to dbuser ; connect as new user connect dbuser / dbuser Now change your password Kristian Torp (Aalborg University) Introduction to the Oracle DBMS December 2, 2011 8 / 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 > drop the users created above drop user 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

Outline 1 Users 2 The Data Dictionary 3 General Stuff Kristian Torp (Aalborg University) Introduction to the Oracle DBMS December 2, 2011 10 / 72

Overview of Data Dictionary The idea in the naming Name all [rest of name] dba [rest of name] user [rest of name] Description All the stuff that the current user can see All the stuff in the database 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 user views Kristian Torp (Aalborg University) Introduction to the Oracle DBMS December 2, 2011 11 / 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 cnt from a l l o b j e c t s ao group by ao. owner, ao. o b j e c t t y p e order by cnt desc Note The all objects lists all objects stored in the database The 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 TYPE TABLE INDEX SYNONYM.. Kristian Torp (Aalborg University) Introduction to the Oracle DBMS December 2, 2011 12 / 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

The Unix ls command Example (Show me the stuff) s e l e c t from cat ; 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

Limiting the Number of Rows Returned Example (Give me the first ten rows) s e l e c t from cat 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

Outline 4 Tables 5 Columns 6 Constraints 7 Views Kristian Torp (Aalborg University) Introduction to the Oracle DBMS December 2, 2011 16 / 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

The dual Table Example (Find the current time) s e l e c t from Note current timestamp dual 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

Space Consumption I Example (How much space are my tables taking up?) s e l e c t us. segment name as t a b l e name, bytes /1024 as KB from user segments us where us. segment type = TABLE order 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 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( bytes ) / ( 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 segment type = TABLE group by segment name ; Kristian Torp (Aalborg University) Introduction to the Oracle DBMS December 2, 2011 20 / 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 table name, ( avg row len 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, avg row len, num rows, ( avg row len num rows ) / ( 1 0 2 4 ) size 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 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 drop t a b l e ua. table name ; from u s e r t a b l 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 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 drop t a b l e lower ( ua. table name ) ; from u s e r t a b l e s ua ; Note The function lower 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 Example (Store the drop script in a file) set heading o f f do not show the column names set feedback o f f do not w r i t e n rows returned at the end set pagesize 50000 make the page s i z e very l a r g e send output to a f i l e spool c : \ d r o p t a b l e s. s q l The a c t u a l query to execute s e l e c t drop t a b l e lower ( ua. table name ) ; from u s e r t a b l e s ua ; spool o f f stop w r i t i n g to 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 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 Example (The columns on the student table) s e l e c t from u s e r t a b c o l s utc where utc. table name = 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 Example (Columns names that include the string ID) s e l e c t utc. from u s e r t a b c o l s utc where utc. 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 Example (Find the number of bytes needed to store a date) s e l e c t vsize ( date ) from dual ; Example (Find the number of bytes needed to store an interval) s e l e c t vsize ( i n t e r v a l 37 12 day to hour ) 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 Example (Find the most used data type for columns) s e l e c t utc. data type, count ( ) as no cols from u s e r t a b c o l s utc group by utc. data type order by no cols desc Kristian Torp (Aalborg University) Introduction to the Oracle DBMS December 2, 2011 29 / 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 Example (Name of tables that have a primary key) s e l e c t uc. table name from 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 Constraint Types Constraint Type P R C Description Primary key Referential Integrity (foreign key) Check constraint Kristian Torp (Aalborg University) Introduction to the Oracle DBMS December 2, 2011 31 / 72

Missing Primary Keys Example (Tables with no primary key) s e l e c t table name from u s e r t a b l e s where table name not i n ( s e l e c t table name from u s e r c o n s t r a i n t s where 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 Example (Columns associated with a constraint) s e l e c t from user cons columns ucc where ucc. table name = 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 Example (Find tables with a composite primary key) s e l e c t uc. table name, count ( ucc. column name ) from user cons columns 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 primary key and uc. owner = ucc. owner and uc. constraint name = ucc. constraint name group by uc. table name having count ( ucc. column name ) > 1 Kristian Torp (Aalborg University) Introduction to the Oracle DBMS December 2, 2011 34 / 72

Outline 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 Example (Show all views) s e l e c t from user views Kristian Torp (Aalborg University) Introduction to the Oracle DBMS December 2, 2011 36 / 72

View Implementation Example (The select statements used for creating the view) s e l e c t uv. t e x t from user views 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 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 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. last number from user sequences us Kristian Torp (Aalborg University) Introduction to the Oracle DBMS December 2, 2011 39 / 72

Outline 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 Example (Create a Directory Alias) create 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) create 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 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 Example s 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 Example (Grant Read/Write Access) grant read, w r i t e on d i r e c t o r y f i l e s t u f f to <user name>; Note Provides both read and write rights the directory alias Example (Grant Read-Only Access) grant read on d i r e c t o r y f i l e s t u f f to <user name>; Note Provides 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 create or replace procedure s t r 2 f i l e ( dir name varchar2, f i l e n a m e varchar2, s t r varchar2 ) 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. fopen ( dir name, file name, 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 ) ; exception when o ther s then i f u t l f i l e. is open ( 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, Hello 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 I create or replace f u n c t i o n f i l e 2 c l o b ( dir name varchar2, f i l e n a m e varchar2 ) r e t u r n clob 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 clob ; v l i n e varchar2 ( 2 0 0 0 ) ; v l e n g t h i n t ; begin dbms lob. createtemporary ( v clob, f a l s e ) ; v f i l e := u t l f i l e. fopen ( dir name, file name, r ) ; begin loop u 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 lob. g e t l e n g t h ( v c l o b ) + 1; dbms lob. w r i t e ( v clob, l e n g t h ( v l i n e ), v length, v l i n e ) ; end loop ; exception when o the rs then i f u t l f i l e. is open ( 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 ; 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 Example (Use the Clob Conversion) Create a t a b l e with a clob column create t a b l e withaclob ( i i n t, l clob ) ; Use the new f u n c t i o n to load a f i l e i n t o a clob columkn i n s e r t i n t o withaclob values ( 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 uploaded as a clob 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 Example (Relocate Directory Alias) create or replace 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) create or replace d i r e c t o r y f i l e s t u f f as / unix / path / dos / machine ; Note No warning/error when creating non-existing directory Kristian Torp (Aalborg University) Introduction to the Oracle DBMS December 2, 2011 48 / 72

Dropping 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 Sequences 9 Directories and Files 10 Synonyms 11 Comments Kristian Torp (Aalborg University) Introduction to the Oracle DBMS December 2, 2011 50 / 72

Introduction Concept A synonym is an alias, such that it is simpler to refer to database objects own by another user. Example (List all non-public synonyms) s e l e c t from all synonyms a l s where a l s. owner <> PUBLIC ; Note A public synonym is visible to all database users Public 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 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 a l s. synonym name, a l s. table owner, a l s. table name from all synonyms 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. table owner and a t t. table name = a l s. table name ) ; 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 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 Example (Find all tables with comments) s e l e c t utc. from user tab comments utc where utc. comments i s not n u l l Kristian Torp (Aalborg University) Introduction to the Oracle DBMS December 2, 2011 54 / 72

Column Comments Example (Find column comments on the Student table) s e l e c t ucc. column name, ucc. comments from user col comments ucc where ucc. table name = STUDENT Kristian Torp (Aalborg University) Introduction to the Oracle DBMS December 2, 2011 55 / 72

Outline 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) cr eate or replace procedure l s i s cursor c t a b l e s i s s e l e c t from cat ; v table name cat. table name%type ; v type cat. t a b l e t y p e%type ; begin open c t a b l e s ; loop f e t c h c t a b l e s i n t o v table name, v type ; e x i t when c t a b l e s%notfound ; p ( v table name ) ; end loop ; close 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 Example (Executed the stored procedure) t o enable ou tput from the ser ve r set s e r v e r o u t p u t on exec i s s h o r t f o r execute exec l s ; Kristian Torp (Aalborg University) Introduction to the Oracle DBMS December 2, 2011 58 / 72

Space Usage Function I 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 Example create or replace f u n c t i o n space usage ( p table name i n u s e r t a b l e s. table name%type ) r e t u r n number i s v s i z e k b number ; begin s e l e c t ( ut. avg row len ut. num rows ) / ( 1 0 2 4 ) i n t o v s i z e k b from u s e r t a b l e s ut where u t. table name = upper ( p table name ) ; 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 Note The into keyword The upper keyword The return keyword It is very important that parameter names, e.g., p table name are different 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) t o enable ou tput from the ser ve r set s e r v e r o u t p u t on declare v r v number ; begin v r v := space usage ( student ) ; dbms output. p u t l i n e ( Size student t a b l e i s v r v KB ) ; 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 Example (Show source code for the ls stored procedure ) s e l e c t us. t e x t from user source us where us. name = LS order 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 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$reserved words 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 12 Stored Procedures 13 Triggers 14 Timing Kristian Torp (Aalborg University) Introduction to the Oracle DBMS December 2, 2011 65 / 72

Introduction 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 ut where ut. 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 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) set heading o f f set pagesize 50000 set termout o f f spool 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 to time s e l e c t c. c a r i d, c. road id, c. speed from c a r g p s a r r a y c order by c a r i d desc ; Stop query t o time t i m i n g stop spool o f f set termout 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 15 Tablespace Usage 16 Additional Information Kristian Torp (Aalborg University) Introduction to the Oracle DBMS December 2, 2011 69 / 72

Space Usage Example (Space consumption for tablespaces) s e l e c t segment name, tablespace name, sum( bytes /(1024 1024)) MB from dba segments where owner= TORP and segment name not l i k e BIN group by segment name, tablespace name order 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 15 Tablespace Usage 16 Additional Information Kristian Torp (Aalborg University) Introduction to the Oracle DBMS December 2, 2011 71 / 72

Web Sites Sites Oracle 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 explanation dbazine.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