uc berkeley extension · uc berkeley extension ... 2.5 built-in functions, system functions ......

32
UC BERKELEY EXTENSION Database/Application/Programming Courses Instructor: Michael Kremer, Ph.D. Course Title: SQL Database Programming Course Subtitle: Oracle PL/SQL and SQL Server T-SQL

Upload: nguyenduong

Post on 29-Aug-2018

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: UC BERKELEY EXTENSION · UC BERKELEY EXTENSION ... 2.5 Built-In Functions, System Functions ... control statements. The non-procedural SQL language is set based

UC BERKELEY EXTENSION

Database/Application/Programming Courses

Instructor: Michael Kremer, Ph.D.

Course Title: SQL Database Programming

Course Subtitle: Oracle PL/SQL and SQL Server T-SQL

Page 2: UC BERKELEY EXTENSION · UC BERKELEY EXTENSION ... 2.5 Built-In Functions, System Functions ... control statements. The non-procedural SQL language is set based
Page 3: UC BERKELEY EXTENSION · UC BERKELEY EXTENSION ... 2.5 Built-In Functions, System Functions ... control statements. The non-procedural SQL language is set based

SQL Database Programming

Instructor: Michael Kremer, Ph.D.

E-mail: [email protected]

Web Site: http://www.ucb-access.org

Copyright © 2018

All rights reserved. This publication, or any part thereof, may not be reproduced or transmitted in any form or by any means,

electronic or mechanical, including photocopying, recording, storing, or otherwise without expressed permission.

5th Edition

I C O N K E Y

Note

Example

Warning

Page 4: UC BERKELEY EXTENSION · UC BERKELEY EXTENSION ... 2.5 Built-In Functions, System Functions ... control statements. The non-procedural SQL language is set based

TABLE OF CONTENT

CLASS 1 ..................................................................................... 1

1. INTRODUCTION TO ORACLE/SQL SERVER DATABASE PROGRAMMING................................................... 1

1.1 SQL and Database Programming .............................................................................................................. 1

1.2 Structure of Database Programs ............................................................................................................... 4

1.3 Differences between Oracle and SQL Server ............................................................................................. 6

Versions and Editions ......................................................................................................................................................................................... 6 Instances and Databases/Tablespaces ............................................................................................................................................................. 10 Instance Names vs. SIDs ................................................................................................................................................................................... 13 System Databases and System Tablespaces .................................................................................................................................................... 15

1.4 Graphical Database Management Tools ................................................................................................. 17

SQL Server Management Studio (SSMS) .......................................................................................................................................................... 17 Oracle SQL Developer ...................................................................................................................................................................................... 22

CLASS 2 ................................................................................... 25

2. LANGUAGE FUNDAMENTALS OF PL/SQL AND T-SQL ..................................................................... 25

2.1 Structure of Programming Code .............................................................................................................. 25

2.2 Basic Language Components ................................................................................................................... 29

Literal Values .................................................................................................................................................................................................... 29 Delimiters ......................................................................................................................................................................................................... 30

Comments ........................................................................................................................................................................................................ 30 Identifier .......................................................................................................................................................................................................... 30 Reserved Words ............................................................................................................................................................................................... 31

2.3 Datatypes ................................................................................................................................................. 32

Numeric Data Types ......................................................................................................................................................................................... 32

Character Data Types ....................................................................................................................................................................................... 34 Temporal Data Types ....................................................................................................................................................................................... 35 Miscellaneous Data Types ................................................................................................................................................................................ 38

2.4 Datatype Conversion ............................................................................................................................... 39

Number Conversions ........................................................................................................................................................................................ 40 Date/Time Conversion ..................................................................................................................................................................................... 43

2.5 Built-In Functions, System Functions ....................................................................................................... 47

CLASS 3 ................................................................................... 57

3. DECLARING VARIABLES/CONSTANTS ........................................................................................... 57

3.1 Variable Declaration Statement .............................................................................................................. 57

3.2 Variable Assignment Statement .............................................................................................................. 58

3.3 Constants, Default Values, NOT NULL constraint .................................................................................... 59

Page 5: UC BERKELEY EXTENSION · UC BERKELEY EXTENSION ... 2.5 Built-In Functions, System Functions ... control statements. The non-procedural SQL language is set based

4. FLOW CONTROL STRUCTURES .................................................................................................... 60

4.1 Decision Structures .................................................................................................................................. 60

The IF Statement .............................................................................................................................................................................................. 60 Short-Circuit IF Processing ............................................................................................................................................................................... 64 CASE Statement ............................................................................................................................................................................................... 67 Simple CASE ..................................................................................................................................................................................................... 67 Searched CASE ................................................................................................................................................................................................. 69

Order-Dependent Logic .................................................................................................................................................................................... 69

4.2 Loop Structures ........................................................................................................................................ 71

Simple Loop ...................................................................................................................................................................................................... 71 For Loop ........................................................................................................................................................................................................... 72 While Loop ....................................................................................................................................................................................................... 73

5. EXCEPTION HANDLING ............................................................................................................. 76

5.1 Introduction to Exception Handling ......................................................................................................... 76

5.2 Basic Exception Handling ......................................................................................................................... 77

5.3 Programmer-Defined Exceptions ............................................................................................................. 84

5.4 Built-In Error Functions ............................................................................................................................ 88

CLASS 4 ................................................................................... 93

6. STORED FUNCTIONS ................................................................................................................ 93

6.1 Introduction to Functions ........................................................................................................................ 93

6.2 Writing Basic Functions ........................................................................................................................... 95

Create Function Statement .............................................................................................................................................................................. 95 Return/Returns Datatype ................................................................................................................................................................................. 99 Function Parameters ...................................................................................................................................................................................... 102

6.3 Integration of Functions and SQL .......................................................................................................... 105

6.4 Advanced Functions ............................................................................................................................... 111

CLASS 5 ................................................................................. 117

7. STORED PROCEDURES ............................................................................................................ 117

7.1 Introduction to Stored Procedures ......................................................................................................... 117

7.2 Basic Stored Procedures ........................................................................................................................ 118

7.3 Parameters ............................................................................................................................................ 126

IN Mode ......................................................................................................................................................................................................... 128 OUT Mode ...................................................................................................................................................................................................... 129 IN OUT Mode ................................................................................................................................................................................................. 130 Parameter Association ................................................................................................................................................................................... 134

7.4 Overloading Procedures......................................................................................................................... 140

CLASS 6 ................................................................................. 145

8. USING DECLARATIVE SQL IN PROCEDURAL SQL .......................................................................... 145

Page 6: UC BERKELEY EXTENSION · UC BERKELEY EXTENSION ... 2.5 Built-In Functions, System Functions ... control statements. The non-procedural SQL language is set based

8.1 DML In Procedural SQL .......................................................................................................................... 145

Insert Statement ............................................................................................................................................................................................ 145 Update Statement .......................................................................................................................................................................................... 146 Delete Statement ........................................................................................................................................................................................... 146 Other DML Statements .................................................................................................................................................................................. 147 Attributes for DML Statements ...................................................................................................................................................................... 147 Returning Information from DML Statements ............................................................................................................................................... 150

8.2 Transaction Management ..................................................................................................................... 156

Transactions ................................................................................................................................................................................................... 156 Commit Statement ......................................................................................................................................................................................... 159 Rollback Statement ........................................................................................................................................................................................ 159 SAVEPOINT/SAVE statement .......................................................................................................................................................................... 160

8.3 Autonomous Transactions ..................................................................................................................... 165

CLASS 7 ................................................................................. 171

9. DATABASE TRIGGERS ............................................................................................................. 171

9.1 Overview of Triggers .............................................................................................................................. 171

9.2 DML Trigger Concepts ........................................................................................................................... 172

9.3 Basic DML Triggers ................................................................................................................................ 174

9.4 Advanced DML Triggers ......................................................................................................................... 184

Mutating Table Error ...................................................................................................................................................................................... 184 Status Transition Rules ................................................................................................................................................................................... 189

Simple Audit Example .................................................................................................................................................................................... 193

9.5 INSTEAD OF Triggers .............................................................................................................................. 197

CLASS 8 ................................................................................. 201

10. CURSORS .......................................................................................................................... 201

10.1 Cursor Basics ........................................................................................................................................ 201

Declaring Cursors ........................................................................................................................................................................................... 203 Opening Cursors ............................................................................................................................................................................................. 206 Fetching Cursors ............................................................................................................................................................................................. 207 Closing Cursors ............................................................................................................................................................................................... 209 Cursor Optimization Tips ................................................................................................................................................................................ 212

10.2 Cursor Parameters ............................................................................................................................... 213

10.3 Cursor Variables ................................................................................................................................... 217

10.4 Data Updates using Cursors ................................................................................................................ 223

FOR UPDATE Clause ....................................................................................................................................................................................... 223 CURRENT OF Clause ....................................................................................................................................................................................... 224

11. DYNAMIC SQL .................................................................................................................. 229

11.1 Overview of Dynamic SQL .................................................................................................................... 229

11.2 Basic Dynamic SQL ............................................................................................................................... 231

CLASS 9 ................................................................................. 237

Page 7: UC BERKELEY EXTENSION · UC BERKELEY EXTENSION ... 2.5 Built-In Functions, System Functions ... control statements. The non-procedural SQL language is set based

12. ADVANCED DYNAMIC SQL ................................................................................................... 237

12.1 Parameterized Dynamic SQL ............................................................................................................... 237

12.2 Dynamic Data Pivoting ........................................................................................................................ 241

12.3 SQL Injection ........................................................................................................................................ 246

13. ADVANCED EXCEPTION HANDLING ......................................................................................... 256

13.1 Unhandled Exceptions ......................................................................................................................... 256

13.2 Continue Past Exceptions ..................................................................................................................... 257

13.3 Exceptions and Transactions ............................................................................................................... 261

CLASS 10 ............................................................................... 267

14. ADVANCED TOPICS ............................................................................................................. 267

14.1 Optimistic/Pessimistic Concurrency Models ........................................................................................ 267

Concurrency Issues ........................................................................................................................................................................................ 267

Single/Multi- Version Databases .................................................................................................................................................................... 269 Read Uncommited Isolation ........................................................................................................................................................................... 271

Read Committed Isolation.............................................................................................................................................................................. 272 Repeatable Read Isolation ............................................................................................................................................................................. 274 Serializable Isolation ...................................................................................................................................................................................... 275 Multi-Version Database, Snapshot Isolation .................................................................................................................................................. 276 Conflict Detection .......................................................................................................................................................................................... 277 Multi-Version Database, Read Committed Snapshot ..................................................................................................................................... 278

14.2 Sending E-mail ..................................................................................................................................... 286

14.3 Other Advanced Topics ........................................................................................................................ 286

Page 8: UC BERKELEY EXTENSION · UC BERKELEY EXTENSION ... 2.5 Built-In Functions, System Functions ... control statements. The non-procedural SQL language is set based
Page 9: UC BERKELEY EXTENSION · UC BERKELEY EXTENSION ... 2.5 Built-In Functions, System Functions ... control statements. The non-procedural SQL language is set based

SQL Database Programming

1. Introduction to Oracle/SQL Server Database Programming 1 1.1 SQL and Database Programming

CLASS 1

1. Introduction to Oracle/SQL Server Database Programming

In this first chapter we will cover the basics of structured database programming languages. The

standard non-procedural language SQL is supplemented with a procedural framework on most

database platforms. Unfortunately, the procedural database language is not standardized at this

point. Therefore, every database platform is different in terms of how the procedural framework

is implemented and its syntax.

1.1 SQL and Database Programming

The non-procedural SQL language has its limitations to meet the various and complex business

needs of today’s organizations. Since SQL was standardized fairly early on (1980’s and 90’s) and

the need for procedural database processing became more evident later on, database vendors

added their own procedural framework to interact with non-procedural SQL. Several major

RDBMS vendors support a procedural dialect of SQL that adds looping, branching, and flow of

control statements.

The non-procedural SQL language is set based. A set-based approach is actually an approach

which lets you specify "what to do", but does not let you specify "how to do". That is, you just

specify your requirement for a processed result that has to be obtained from a "set of data" (be

it a simple table/view, or joins of tables/views), filtered by optional condition(s). Sometimes, the

specification of the "sets" you like to retrieve data from may be obtained by using complex

joins/subqueries/conditional case statements, but in the end, there is a set of data from which

the resultant set has to be obtained:

➢ You never have to specify "how" the data retrieval operation has to be implemented

internally

➢ You never have to specify how to implement the "joining operation" internally either

➢ You do not have to specify how to apply a filter condition against the rows

The database engine determines the best possible algorithms or processing logic to do these.

Page 10: UC BERKELEY EXTENSION · UC BERKELEY EXTENSION ... 2.5 Built-In Functions, System Functions ... control statements. The non-procedural SQL language is set based

SQL Database Programming

1. Introduction to Oracle/SQL Server Database Programming 2 1.1 SQL and Database Programming

On the other hand, procedural SQL programming works differently. Simply speaking, a

procedural approach is actually the "programmatic approach" that we are used to working with

in our daily programming life. In this approach, we tell the system "what to do" along with "how

to do" it:

➢ We query the database to obtain a result set and we write the data operational and

manipulation logic using loops, conditions, and processing statements to produce the final

result

➢ The runtime does whatever we want it to do, however we want it to do.

Using a cursor that executes on a result set row by row is a procedural approach. For example, if

you are querying your database to obtain a result set and using a cursor to navigate the result

set to do further processing row by row, you are using a procedural approach. Also, if you are

using a function in your SQL for processing each row in the result set to calculate a scalar

output, you are using a procedural approach.

The internal execution engines of databases are designed and optimized for taking "set based

instructions" as input (SQL) and executing these instructions in the best possible way (that

varies based on a lot of criteria) to produce an output. That is why "Set based approaches" are

almost always the better option.

When SQL written in a "Set based approach" is issued against the database, the query optimizer

generates an execution plan first, and then the execution engine executes the plan to retrieve

data from the physical storage and processes the output in an efficient manner. That is, there is

a single execution plan tree for each single SQL statement, be it simple or complex. Executing

that single execution plan tree is generally a faster operation.

Page 11: UC BERKELEY EXTENSION · UC BERKELEY EXTENSION ... 2.5 Built-In Functions, System Functions ... control statements. The non-procedural SQL language is set based

SQL Database Programming

1. Introduction to Oracle/SQL Server Database Programming 3 1.1 SQL and Database Programming

But, when we specify our own way of processing a result set (that is obtained by executing a

SQL statement) using another SQL statement that works on a row-by-row manner in the result

set, the database engine has to execute an execution plan for each and every row, even after

obtaining the result set by executing an execution plan.

Imagine a row-by-row operation that is executed for a result set containing 1 million rows. In

this case, the initial data retrieval operation would require an execution plan to be executed, and

later, another execution plan has to be repeated 1 million times for each row processed. That is

what happens when a function is executed for each row in a result set. An additional overhead of

using functions is the amount of stack I/O that takes place for invoking the function.

On the other hand, if you use a Cursor to process a result set row-by-row, while executing, the

cursor locks the rows in the corresponding table, and unlocks the rows when processing is done.

This involves lots of resource usage on the server, and in the case of large result sets, severely

slows down performance.

The underlying message here is that you want to minimize the amount of procedural SQL for

performance reasons. Many procedural SQL code can be rewritten into non-procedural SQL

statements using subqueries and other sophisticated, new SQL commands such as the With

clause.

Page 12: UC BERKELEY EXTENSION · UC BERKELEY EXTENSION ... 2.5 Built-In Functions, System Functions ... control statements. The non-procedural SQL language is set based

SQL Database Programming

1. Introduction to Oracle/SQL Server Database Programming 4 1.2 Structure of Database Programs

1.2 Structure of Database Programs

The structure of procedural database programs is very similar to many other programming

languages. Each program has a distinct name, you may pass arguments into the program, and

the program can also return values back to the calling environment. This part is called the

header section. The structure itself consists of a declaration section where mostly variables are

declared and the actual executable section. And last, but not least, there can be an exception

section where errors are handled in a customized manner. Leaving the exception section out will

trigger the default error handling of the environment.

In Oracle as well as in SQL Server the structure of the database programs is equal, they just

differ by the implementation or the syntax as shown in Figure 1 below.

Header

IS

Begin

Exception

End;

Declaration

Execution

Exception

Header

AS

Begin Try

End Catch

Execution

Exception

Begin Catch

End Try

Oracle PL/SQL SQL Server T-SQL

Figure 1: Program Structure Syntax Oracle vs. SQL Server

Page 13: UC BERKELEY EXTENSION · UC BERKELEY EXTENSION ... 2.5 Built-In Functions, System Functions ... control statements. The non-procedural SQL language is set based

SQL Database Programming

1. Introduction to Oracle/SQL Server Database Programming 5 1.2 Structure of Database Programs

In Oracle only, you can also divide your program into smaller, manageable components by using

nested procedures. A nested procedure is a sub program embedded in the main program that

can only be called or referenced by the main program.

Note: You can always create additional procedures that can be called from each other. The

only difference is that they are stand-alone, individual programs. A nested program resides

within the main program and belongs to the main program.

Header

IS

Begin

End;

Declaration

Execution

Oracle PL/SQL

Begin

End;

Execution

Header

ISDeclaration

Ma

in P

rog

ram S

ub

Pro

gra

m

Figure 2: Nested Program Structure in Oracle

Page 14: UC BERKELEY EXTENSION · UC BERKELEY EXTENSION ... 2.5 Built-In Functions, System Functions ... control statements. The non-procedural SQL language is set based

SQL Database Programming

1. Introduction to Oracle/SQL Server Database Programming 6 1.3 Differences between Oracle and SQL Server

1.3 Differences between Oracle and SQL Server

At this point in time, MS SQL Server runs only on the Windows© platform whereas Oracle runs

on Windows©, Linux, and the various varieties of UNIX flavored platforms (Solaris, HP-UX, AIX).

Versions and Editions

The current version of SQL Server (as of time of this writing) is 2017. Past SQL Server versions

include version 2000, 2005, 2008, 2012, 2014, 2016. Many companies are still running 2005 or

2008, both were a major release compared to version 2000.

Oracle’s current version is 12c R2. Oracle has come a long way. The version number 12 actually

represents the number of versions the Oracle database has evolved. The c stands for cloud.

Many companies are still running Oracle versions 9i at this time.

SQL Server 2017 comes with the following editions:

➢ Enterprise Editon:

The Enterprise Edition is for mission critical applications and data warehousing

➢ Web:

SQL Server Web edition is a low total-cost-of-ownership option for Web hosters to provide

scalability, affordability, and manageability capabilities for small to large scale Web

properties

➢ Standard Edition:

The Standard Edition is for basic database capabilities, reporting and analytics

➢ Express Edition:

A small footprint, embedded SQL Server engine that can be used for local data storage

and small scale system development (The Express Edition is free to download and can be

freely redistributed with a software)

➢ Developer Edition:

Every feature of the Enterprise is available in the Developer Edition (It is licensed for use

by one user at a time and is meant to be used for development and testing purposes)

Note: SQL Version 2008 contained additional editions, such as Premium, Web, Compact,

and Workgroup editions. In version 2012, the number of editions has been reduced to basically

three (besides Express and Developer editions) to simplify the selection for customers.

Page 15: UC BERKELEY EXTENSION · UC BERKELEY EXTENSION ... 2.5 Built-In Functions, System Functions ... control statements. The non-procedural SQL language is set based

SQL Database Programming

1. Introduction to Oracle/SQL Server Database Programming 7 1.3 Differences between Oracle and SQL Server

SQL Server is component based and can be installed with one or more components. The core

component is the Database Engine.

SQL Server

Database Engine

SQL Server Database Engine includes the Database Engine, the core

service for storing, processing, and securing data, replication, full-text

search, tools for managing relational and XML data, in database

analytics integration, and Polybase integration for access to Hadoop and

other heterogeneous data sources, and the Data Quality Services (DQS)

server.

Analysis Services Analysis Services includes the tools for creating and managing online

analytical processing (OLAP) and data mining applications.

Reporting Services Reporting Services includes server and client components for creating,

managing, and deploying tabular, matrix, graphical, and free-form

reports. Reporting Services is also an extensible platform that you can

use to develop report applications.

Integration Services Integration Services is a set of graphical tools and programmable

objects for moving, copying, and transforming data. It also includes the

Data Quality Services (DQS) component for Integration Services.

Master Data

Services

Master Data Services (MDS) is the SQL Server solution for master data

management. MDS can be configured to manage any domain (products,

customers, accounts) and includes hierarchies, granular security,

transactions, data versioning, and business rules, as well as an Add-in

for Excel that can be used to manage data.

Machine Learning

Services (In-

Database)

Machine Learning Services (In-Database) supports distributed, scalable

machine learning solutions using enterprise data sources. In SQL Server

2016, the R language was supported. SQL Server 2017 supports R and

Python.

Machine Learning

Server (Standalone)

Machine Learning Server (Standalone) supports deployment of

distributed, scalable machine learning solutions on multiple platforms

and using multiple enterprise data sources, including Linux, Hadoop,

and Teradata. In SQL Server 2016, the R language was supported. SQL

Server 2017 supports R and Python.

Table 1: SQL Server Components (selected)

Page 16: UC BERKELEY EXTENSION · UC BERKELEY EXTENSION ... 2.5 Built-In Functions, System Functions ... control statements. The non-procedural SQL language is set based

SQL Database Programming

1. Introduction to Oracle/SQL Server Database Programming 8 1.3 Differences between Oracle and SQL Server

For the current version of Oracle, the following editions are available:

➢ Enterprise Edition (EE):

This edition offers top performance for top money, like the SQL counterpart, every feature

and capability of the product is enabled in this edition

➢ Standard Edition 2 (SE2):

Much like the SQL Standard Edition, the Oracle Standard Edition has major features of the

product enabled and is suitable for most business applications

➢ Express Edition (XE):

This is a low footprint, small-scale, starter database for development purposes and is

licensed for free redistributions (The Express Edition is still in version 11)

➢ Oracle Database Personal Edition (PE):

Oracle Database Personal Edition supports single-user development and deployment

environments that require full compatibility with Oracle Database Standard Edition 2 and

Oracle Database Enterprise Edition.

The table below shows the different editions of both products and how they compare:

SQL Server 2012 Oracle 12c R2

Enterprise Edition Enterprise Edition

Standard Edition Standard Edition One

Express Edition Express Edition

Developer Edition Personal Edition

Table 2: Edition Comparisons SQL Server vs. Oracle

Page 17: UC BERKELEY EXTENSION · UC BERKELEY EXTENSION ... 2.5 Built-In Functions, System Functions ... control statements. The non-procedural SQL language is set based

SQL Database Programming

1. Introduction to Oracle/SQL Server Database Programming 9 1.3 Differences between Oracle and SQL Server

As with SQL Server, Oracle comes is component based and possesses many different database

management tools.

Oracle Active

Data Guard

Extends Oracle Data Guard functionality with advanced features, allowing

read-only access to data in a physical standby database to offload primary

of such tasks as reporting, ad-hoc queries, data extraction and backup,

offloading redo transport and minimizing standby impact on commit

response times (using Far Sync feature), providing option for rolling

upgrades for non-RAC customers, managing clients workload across

replicated database and improving automated service failover (using Global

Data Services), etc.

Oracle Advanced

Analytics

Allows access to in-database data mining algorithms and use of Oracle R

Enterprise functionality, an integration with open-source R statistical

programming language and environment.

Oracle

Partitioning

Allows partitioning of tables and indices, where large objects are stored in

database as a collection of individual smaller pieces at the same time

appearing on application level as a uniform data object.

Oracle On-Line

Analytical

Processing

(OLAP)

Oracle’s implementation of online analytical processing.

Oracle

Multitenant

Capability that allows database consolidation and provides an additional

abstraction layer. In a Multitenant configuration, one Oracle database

instance known as "container database" (CDB) acts as a federated

database system for a collection of up to 252 distinct portable collections of

database objects, referred to as "pluggable databases" (PDB), each

appearing to an outside client as a regular non-CDB Oracle database.

Table 3: Oracle Components (selected)

Page 18: UC BERKELEY EXTENSION · UC BERKELEY EXTENSION ... 2.5 Built-In Functions, System Functions ... control statements. The non-procedural SQL language is set based

SQL Database Programming

1. Introduction to Oracle/SQL Server Database Programming 10 1.3 Differences between Oracle and SQL Server

Instances and Databases/Tablespaces

The first architecture level difference between SQL Server and Oracle is in the concept of

instances and databases.

An instance in SQL Server terms means a self-contained application service that involves

operating system files, memory structures, background processes and registry information. An

instance is represented by a service in Windows and can be either in a running or stopped state.

When running, an instance occupies a portion of the server’s memory and also spawns a number

of background processes.

Central to a SQL Server instance are its databases. A SQL Server database is the repository of

data and the program code for manipulating that data. If an instance is not running, databases

within it cannot be accessed.

There are two types of SQL Server databases: system databases and user databases. When a

SQL Server instance is first installed, five system databases are created: master, model, msdb,

tempdb and resource. If there is more than one SQL Server instance running in a machine, each

instance will have its own dedicated set of system databases. An instance cannot start if any of

its system databases except msdb is inaccessible or corrupted. User databases on the other

hand are created by DBAs and developers after the instance has been installed and the system

databases have started. These are the databases that store an organization’s business

information.

In short, a SQL Server instance will always include databases (even if it is only the system ones)

and a database will always be associated with one (and only one) instance.

At the physical level, a SQL Server database is represented by a set of operating system files

residing in the server’s disk system. There are two types of database files: the data file and the

transaction log file. At the very minimum, a database will have one data file and one transaction

log file. A data file is the main repository of information in a SQL database. A transaction log file

on the other hand records the changes that have been applied to the data. This file is required

by SQL Server for system recovery. A data or log file will always belong to a particular database:

no two databases can share the same data or log file. If the database is large, it can have

multiple data files. Multiple data files in a database can be logically grouped into structures

known as filegroups.

With Oracle, it works in somewhat reverse direction. When Oracle starts, it works just like SQL

Server in that a portion of the server’s memory is allocated for its operation. This memory area,

known as the System Global Area (SGA), is divided into a number of distinct structures. Along

with the memory space, a number of background processes are also started that interact with

the SGA. Together, the memory space and the processes constitute an Oracle instance. Note

that the Oracle database is still not in the picture. In fact an Oracle instance could be running

Page 19: UC BERKELEY EXTENSION · UC BERKELEY EXTENSION ... 2.5 Built-In Functions, System Functions ... control statements. The non-procedural SQL language is set based

SQL Database Programming

1. Introduction to Oracle/SQL Server Database Programming 11 1.3 Differences between Oracle and SQL Server

perfectly okay without its database even being online or accessible. When installing Oracle, there

is an option to install only the software and to create the database later.

A database in Oracle is a collection of operating system files. Unlike SQL Server, an Oracle

database does not represent the logical grouping of objects; rather it is a single, collective term

for a number of files on the disk that primarily hold data.

The files that make up an Oracle database can be classified into three types: data file, redo log

file and the control file. Data files are where all the data resides. There can be any number of

data files in an Oracle database. Redo log files are like SQL Server transaction logs in that they

record every change made to the data and is used for system recovery. Control files are a

special kind of file that contains small but vital pieces of information about the database. Without

this file, the instance will not be able to open the database. Apart from data, redo log and

control files, the database will also contain a parameter file, password file and optionally, archive

log files.

Logical Physical

Oracle: Tablespaces

SQL Server: Databases

Oracle: Segments

SQL Server: N/A

Oracle: Extents

SQL Server: Extents

Oracle: Data Blocks

SQL Server: Pages

Oracle: Data Files

SQL Server: DB Files

OS Block

Figure 3: Logical and Physical Structure of SQL Server/Oracle

Page 20: UC BERKELEY EXTENSION · UC BERKELEY EXTENSION ... 2.5 Built-In Functions, System Functions ... control statements. The non-procedural SQL language is set based

SQL Database Programming

1. Introduction to Oracle/SQL Server Database Programming 12 1.3 Differences between Oracle and SQL Server

When an Oracle system starts, at first the instance is created in the memory. The instance then

connects to the database residing on disk and finally opens the database for user interaction.

When the system is shut down, the instance is erased from memory: all the memory structures

and the processes are gone, but the database will still exist in the disk, albeit in a closed state.

As said previously, it is possible to have the Oracle instance running without opening the

database – this is a major difference from SQL Server where an instance cannot start without its

systems databases being online first. However, just like SQL Server, it is impossible to connect

to an Oracle database if the instance has not started.

Generally the relationship between an Oracle instance and its database is one-to-one. An

instance will have one database associated with it. A database on the other hand can have one

or more instances accessing it. A standalone Oracle installation will comprise of a single instance

accessing a single database. Oracle installations configured as RAC (Real Application Cluster) will

have multiple instances running in different machines accessing the same database in a shared

disk.

So where does the logical grouping of database objects lie within Oracle? In SQL Server, this

logical grouping is done by the database itself. For Oracle, it is done through something called

tablespaces. An Oracle tablespace is a logical structure that groups together tables, views,

indexes and other database objects.

For example, your production Oracle database may have one tablespace dedicated for the HR

application and another tablespace for payroll. Each tablespace is physically represented by one

or more data files on disk and form a part of the database. The database is logically made up of

a number of tablespaces and the tablespaces in turn are physically made up of one or more data

files. So Oracle’s equivalent to a SQL Server database is a tablespace.

And since they are so similar in their functions, the process of creating a database in SQL Server

is quite similar to creating a tablespace in Oracle. Whether creating a database or a tablespace,

the DBA needs to specify a name first. The DBA then assigns one or more data files to the

database or tablespace and specifies the initial size and growth increments for each file.

Page 21: UC BERKELEY EXTENSION · UC BERKELEY EXTENSION ... 2.5 Built-In Functions, System Functions ... control statements. The non-procedural SQL language is set based

SQL Database Programming

1. Introduction to Oracle/SQL Server Database Programming 13 1.3 Differences between Oracle and SQL Server

Just like a SQL Server user database can be made offline or read-only, so can be an Oracle user

tablespace. And just like one or more data files in a SQL Server user database can be made

read-only, one or more data files in an Oracle user tablespace can be marked offline.

However, databases and tablespaces do differ in the following areas:

• In SQL Server, database files can be logically grouped into filegroups. Oracle tablespaces

do not have this concept.

• In SQL Server databases, each database will have its own transaction log and the log file

properties will need to be specified during database creation. For Oracle, transactions for

the whole database (that means for every tablespace) are recorded in one redo log.

Consequently, there is no provision to create individual log files for tablespaces.

• For SQL Server, the database can be created with simple recovery mode. Simple recovery

mode means the inactive portion of the database log will be truncated after each

checkpoint. Oracle has a similar concept, but it is not possible to configure that property

for individual tablespaces.

Instance Names vs. SIDs

Both SQL Server and Oracle allow multiple instances of the server software to run concurrently

on the same machine. These multiple execution contexts are completely independent of each

other: as far as one database engine is concerned, it does not know or care if another is running

on the same box.

In SQL Server, this mechanism is enabled through the concept of instances. SQL Server can run

either as a named or a default instance. The default instance has the same name as the

Windows server hosting it. Obviously, there can be only one default instance per host, but it is

possible to run multiple named instances on that same machine. A named instance is identified

in the form of HOSTNAMEINSTANCE_NAME, where each INSTANCE_NAME running on the host

has to be unique. Each instance will have its own set of binaries with some common components

shared between all.

Page 22: UC BERKELEY EXTENSION · UC BERKELEY EXTENSION ... 2.5 Built-In Functions, System Functions ... control statements. The non-procedural SQL language is set based

SQL Database Programming

1. Introduction to Oracle/SQL Server Database Programming 14 1.3 Differences between Oracle and SQL Server

SQL Server Host Oracle Host

Instance: Self-contained

application service

involving os files, memory,

processes.

Database: System

databaes(needed for

instance to run) and user

databases.

Schema: Logical

grouping of database

objects, owner and

user of objects

Instance: Only memory

allocation and processes.

Database: Collection of

OS files.

Tablespace: Logical

structure for grouping db

objects.

Figure 4: SQL Server/Oracle Server Architecture

For Oracle, it works the same way. When installing Oracle, the DBA needs to specify a Global

Database Name and a System Identifier (SID). Instance and databases are completely separate

entities in Oracle. A global database name uniquely identifies a database in the network where it

is hosted and can have a fully qualified name in the form of

database_name.network_domain_name. A SID on the other hand identifies the instance

associated with the database.

In most cases where a single instance is associated with a single database, the SID and the

database name will be the same. Oracle Real Application Cluster (RAC) environments are an

exception: RAC allows multiple instances to access the same database hosted in a shared

storage; the instance names are different from the database name in such cases. However, just

like a SQL Server machine, an Oracle database server cannot have two instances running with

the same SID. Another area of similarity is that once specified during setup, a SQL Server

instance name or an Oracle SID cannot be changed later.

A SQL Server DBA can run the following query to know the name of the instance he is currently

logged on to:

SELECT @@SERVERNAME

An Oracle DBA will run queries like the following to get the instance and database name:

SELECT INSTANCE_NAME, HOST_NAME, VERSION, DATABASE_STATUS FROM V$INSTANCE;

SELECT NAME, DATABASE_ROLE, CREATED FROM V$DATABASE;

Page 23: UC BERKELEY EXTENSION · UC BERKELEY EXTENSION ... 2.5 Built-In Functions, System Functions ... control statements. The non-procedural SQL language is set based

SQL Database Programming

1. Introduction to Oracle/SQL Server Database Programming 15 1.3 Differences between Oracle and SQL Server

System Databases and System Tablespaces

A SQL Server instances will have five system databases (four for versions before 2005) present:

master, model, msdb, tempdb and resource. An Oracle database will need a minimum of three

system tablespaces for its operation: SYSTEM, SYSAUX and TEMP.

The master and the resource databases are the central repositories of all the information SQL

Server needs to manage itself. Among many other things, it contains the system configuration

settings, list of databases and the location of their files, end-points, linked servers and user

accounts (or “logins”). System level objects are stored in a read-only database known as the

“resource” database where this information comes from.

For Oracle, SYSTEM tablespace is the equivalent of the master database. SYSTEM tablespace

contains the data dictionary, which is Oracle’s metadata about itself. The data dictionary can be

compared to SQL’s resource database. And probably you have already guessed: Oracle will not

start if the SYSTEM tablespace is unavailable or corrupted.

For a SQL Server instance, the model database is the “template” that is used for every new

database created in that instance. You can make a change to the model database and the

change will be reflected to every new database created afterwards.

For Oracle, there is no such template, but when you create a tablespace, you can specify

whether it will be a permanent tablespace or of any other type like a TEMP or an UNDO

tablespace. Permanent tablespaces are the ones that hold user data.

Page 24: UC BERKELEY EXTENSION · UC BERKELEY EXTENSION ... 2.5 Built-In Functions, System Functions ... control statements. The non-procedural SQL language is set based

SQL Database Programming

1. Introduction to Oracle/SQL Server Database Programming 16 1.3 Differences between Oracle and SQL Server

SQL Server’s tempdb is used as a “scratch pad” for the whole instance. Tempdb is created every

time the instance is restarted and destroyed every time the instance is shut down. Oracle’s TEMP

tablespace does pretty much the same task: it is used to hold large scale sort operation results.

However, SQL’s tempdb can also be used for row versioning. When enabled for a database, row

versioning ensures the database engine keeps track of each version of a data row as it is

modified. The pre-modification copy of the row is copied to a version store in the tempdb

database.

Queries requesting the data row will get the latest committed version. When a read operation

uses an isolation level based on row versioning, it does not block other transactions trying to

modify the same data. This is because the read query is not placing a shared lock on the data

rows. However, this behavior needs to be explicitly enabled for each database.

Oracle uses the same concept with a different type of tablespace – known as the UNDO

tablespace. An undo tablespace holds read-consistent copy of data that is currently being

updated by a DML statement.

As a user starts making changes to data, a pre-update version of that data is stored in the

UNDO tablespace. If another user wants to query those same rows of data, s/he will get the pre-

update version from the UNDO tablespace. Unlike SQL’s version store, this feature does not have

to be explicitly enabled – it is part of Oracle’s concurrent data access mechanism.

Finally, SQL Server’s msdb database is required for its Agent service’s operation. SQL Server

Agent is responsible for scheduled jobs, alerts, replication and log shipping among many other

things. Without the msdb database, the Agent service will not be running.

There is no clear equivalent of msdb in an Oracle. The SYSAUX tablespace is a system

tablespace, created during the installation process. It holds information such as Oracle’s

Automatic Workload Repository (AWR), spatial and multimedia data, XML database etc.

Page 25: UC BERKELEY EXTENSION · UC BERKELEY EXTENSION ... 2.5 Built-In Functions, System Functions ... control statements. The non-procedural SQL language is set based

SQL Database Programming

1. Introduction to Oracle/SQL Server Database Programming 17 1.4 Graphical Database Management Tools

1.4 Graphical Database Management Tools

We will be using the following tools to access and manipulate the database:

• Oracle SQL Developer

• SQL Server Management Studio (SSMS)

The installation instructions can be found on my web site. In this chapter we will be simply

covering the basics of how to use these tools.

SQL Server Management Studio (SSMS)

After you launch SSMS you will see the Connect to Server dialog box as shown in .Once you are

connected to the database server (SQL Express/LocalDB) you are presented with the following

UI:

Figure 5: SQL Server Management Studio

Page 26: UC BERKELEY EXTENSION · UC BERKELEY EXTENSION ... 2.5 Built-In Functions, System Functions ... control statements. The non-procedural SQL language is set based

SQL Database Programming

1. Introduction to Oracle/SQL Server Database Programming 18 1.4 Graphical Database Management Tools

The hierarchical structure of the SQL Server database contains the following main nodes:

Databases: The Databases node is the first node in SQL Server Management Studio. Within the

Databases node are one or more subnodes. The first subnode is System Databases. There are

additional subnodes for each database contained on the server.

Security: To manage users, roles and credentials for the database. Using the Security Node,

you can work with logins, add to and remove people from server roles, and create credentials.

Server Objects: Server Objects refer to a set of objects used at the server level (not at the

database level). These objects include Backup Devices, Linked Servers, and Server Triggers.

Replication: To manage database replication. (Replication is a set of technologies for copying

and distributing data and database objects from one database to another and then synchronizing

between databases to maintain consistency.)

Polybase: For big data operations, to access data from Hadoop file system.

Always On High Availability: High-availability and disaster-recovery solution that provides an

enterprise-level alternative to database mirroring

Management: This section includes policy management, SQL Server logs, and legacy services

such Data Transformation Services (DTS).

Integration Services Catalogs (SSIS): The SSIS Catalog is the one place where you will

manage SSIS projects and packages, including the configuration and monitoring.

The most important and the most frequently used node is the database node. This node lists all

the databases contained in the current server. Figure 6 shows the hierarchy structure under the

database node.

Database Node Programmability Node

Figure 6: Database & Programmability Node

For the scope of this course, we will use the Tables, Views, and the Programmability folder. In

particular, the Programmability folder is an important folder for this course since it contains the

database objects where procedural SQL code is stored.

Page 27: UC BERKELEY EXTENSION · UC BERKELEY EXTENSION ... 2.5 Built-In Functions, System Functions ... control statements. The non-procedural SQL language is set based

SQL Database Programming

1. Introduction to Oracle/SQL Server Database Programming 19 1.4 Graphical Database Management Tools

Example 1-1: Create SQL Server HR Database

1. First using Windows Explorer, navigate to c:\Temp and create a folder named ProceduralDB.

2. Open SQL Server Management Studio (SSMS). Log in using Windows Authentication mode.

3. Right-click on the Databases Folder and select New Database.

4. Type ProcDB as

a name for the

database.

5. In the Database files

section, scroll to the

right and select

c:\Temp\ProceduralDB

in the Path section.

6. Click on OK

Right-click on Database Node

Page 28: UC BERKELEY EXTENSION · UC BERKELEY EXTENSION ... 2.5 Built-In Functions, System Functions ... control statements. The non-procedural SQL language is set based

SQL Database Programming

1. Introduction to Oracle/SQL Server Database Programming 20 1.4 Graphical Database Management Tools

Example 1-1(continued): Create SQL Server HR Database

7. Under the Databases node, the database folder ProcDB is now displayed.

In the toolbar, click on the New Query button or right-click on the ProcDB database folder and

select New Query. If you clicked the New Query button in the toolbar, make sure to select the

ProcDB database from the drop-down list below.

8. A new pane is displayed on the right side.

9. Copy and paste the instructor provided sql script to create the database into this pane.

10.Now click on the Execute button in the toolbar or right-click inside the script and select

Execute to run this script.

Page 29: UC BERKELEY EXTENSION · UC BERKELEY EXTENSION ... 2.5 Built-In Functions, System Functions ... control statements. The non-procedural SQL language is set based

SQL Database Programming

1. Introduction to Oracle/SQL Server Database Programming 21 1.4 Graphical Database Management Tools

Example 1-1(continued): Create SQL Server HR Database

11.Verify in the output window at the bottom that no

errors have occurred.

12.Click on the plus sign next to the Tables folder under

the ProcDB Database to view all the database tables.

Page 30: UC BERKELEY EXTENSION · UC BERKELEY EXTENSION ... 2.5 Built-In Functions, System Functions ... control statements. The non-procedural SQL language is set based

SQL Database Programming

1. Introduction to Oracle/SQL Server Database Programming 22 1.4 Graphical Database Management Tools

Oracle SQL Developer

We will first install SQL Developer into the c:\Temp\ProceduralDB folder. Since this software is

completely JAVA based, the installation does not require any registry entries or other windows

installation requirements.

Example 1-2: Install Oracle SQL Developer

1. Copy the instructor provided zip file into c:\Temp\ProceduralDB.

2. Double-click on this zip file and then drag the folder into C:\Temp\ProceduralDB.

3. After the extraction is complete, a folder named sqldeveloper is created under the

ProceduralDB folder.

4. In this folder, you find an executable file named sqldeveloper.exe.

5. Double-click on this file to run SQL developer.

6. Create a new connection (green plus sign) and type the following information:

Password will be provided by the instructor

7. Click on connect.

Page 31: UC BERKELEY EXTENSION · UC BERKELEY EXTENSION ... 2.5 Built-In Functions, System Functions ... control statements. The non-procedural SQL language is set based

SQL Database Programming

1. Introduction to Oracle/SQL Server Database Programming 23 1.4 Graphical Database Management Tools

Example 1-2 (continued): Install Oracle SQL Developer

8. Copy and paste an instructor provided script into the right pane, the so-called SQL

worksheet. Click on the Run Script button to create the ProcDB user/schema account.

Check the Script Output at the bottom for potential errors,

unfortunately there is no overall message whether the script has

been successfully executed.

9. Create a new connection using ProcDB as Connection Name and

Username, use password hr.

10.Click on the plus sign next to ProcDB to open the object tree.

Page 32: UC BERKELEY EXTENSION · UC BERKELEY EXTENSION ... 2.5 Built-In Functions, System Functions ... control statements. The non-procedural SQL language is set based

SQL Database Programming

1. Introduction to Oracle/SQL Server Database Programming 24 1.4 Graphical Database Management Tools

As you can see, the organization of the database is completely different than in SQL Server.

There are no subnodes like in SQL Server to provide further grouping.

The important objects we will use in this course are of course tables. Besides tables, we will

place database code in Packages, Procedures, Functions, and Triggers. Figure 7 shows the object

tree in SQL Developer.

Figure 7: Oracle SQL Developer