database programming procedural database programming ( pl/sql and

35
SQL DATABASE PROGRAMMING ( PL/SQL AND T - SQL) Database Programming Instructor: Michael Kremer, Ph.D. Technology & Information Management Class 2

Upload: others

Post on 12-Sep-2021

45 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Database Programming PROCEDURAL DATABASE PROGRAMMING ( PL/SQL AND

SQL DATABASE PROGRAMMING ( PL/SQL

AND T-SQL)

Database Programming

Instructor: Michael Kremer, Ph.D.Technology & Information Management

Class 2

Page 2: Database Programming PROCEDURAL DATABASE PROGRAMMING ( PL/SQL AND

AGENDA

2. Language Fundamentals of PL/SQL and T-SQL

2.1 Structure of Programming Code

2.2 Basic Language Components

2.3 Datatypes

2.4 Datatype Conversion

2.5 Built-in Functions, System Functions

Page 3: Database Programming PROCEDURAL DATABASE PROGRAMMING ( PL/SQL AND

SQL Database Programming (PL/SQL and T-SQL)

2. LANGUAGE FUNDAMENTALS OF PL/SQL AND

T-SQL

Page 4: Database Programming PROCEDURAL DATABASE PROGRAMMING ( PL/SQL AND

2.1 STRUCTURE OF PROGRAMING CODE

Stored procedures (SP) hold the majority of DB code

A SP can be compared to a sub routine, sub program, or method

SP is defined by a name followed by parentheses

Inside the parentheses are parameters (In, Out)

SP Name and parameters are also called the header

Declaration section follows the header, used to declare variables

and directives

Main block is the executable section

Last section is the exception/error handling section

As in many other programming environments, structuring db

programs is very important in terms of performance and

maintenance

25

Page 5: Database Programming PROCEDURAL DATABASE PROGRAMMING ( PL/SQL AND

2.1 STRUCTURE OF PROGRAMING CODE

26

Page 6: Database Programming PROCEDURAL DATABASE PROGRAMMING ( PL/SQL AND

2.1 STRUCTURE OF PROGRAMING CODE

27

Page 7: Database Programming PROCEDURAL DATABASE PROGRAMMING ( PL/SQL AND

2.1 STRUCTURE OF PROGRAMING CODE

Structure of SP in both environments is not too different

Variable declaration is slightly different:

SQL Server Declare @variable_name datatype (@ sign required)

Oracle variable_name datatype

Executable Section:

Executable portion is enclosed in Begin and End keywords

Oracle: End statement followed by name of SP

Assignment operation is quite different!

SQL Server Use SET keyword

Oracle Use := operator

Convert to character:

SQL Server CONVERT function

Oracle TO_CHAR

28

Page 8: Database Programming PROCEDURAL DATABASE PROGRAMMING ( PL/SQL AND

2.2 BASIC LANGUAGE COMPONENTS

Procedural DB language is comprised of:

Literal values

Delimiters

Comments

Identifiers

Reserved Words or Keywords

Literals Values

Alphanumeric, hexadecimal, or numeric constant

String literal values are enclosed in straight single or double quotation (SS

only, if QUOTED_IDENTIFIER = OFF) marks

29

Page 9: Database Programming PROCEDURAL DATABASE PROGRAMMING ( PL/SQL AND

2.2 BASIC LANGUAGE COMPONENTS

Delimiters

Double quotes are used for delimited identifiers, special kind of delimiter to allow the use of reserved keywords as identifiers or the use of blank spaces in identifiers Try to avoid this!

In SS, use QUOTED_IDENTIFIER option to set string quote behavior: If ON, then double quotes can only be used for delimited identifier

If OFF, then also for string literals

Semicolon delimiter marks the end of executable statement

Comments

Use -- for line comments or /* … */ for block comments

Identifiers

Program Name (Stored Procedure, Trigger, Function, etc.)

Constant or variable

Exception

Cursor

Reserved Word

30

Page 10: Database Programming PROCEDURAL DATABASE PROGRAMMING ( PL/SQL AND

2.2 BASIC LANGUAGE COMPONENTS

Identifier (continued)

In SS, the pound (#) symbol as the first character denotes a temporary

object

In SS, variable names must start with the at (@) sign

Identifiers are case insensitive unless they are quoted identifiers:

ExpertsExchange and ExpertSexChange are identical for the compiler

Reserved Words

Procedural language recognizes certain identifiers as having special

meaning: Reserved Words (Begin, End)

System settings, built-in functions, etc. (GETDATE(), SYSDATE)

Again, these names cannot be used for identifiers unless they are quoted

identifiers (not recommended)

31

Page 11: Database Programming PROCEDURAL DATABASE PROGRAMMING ( PL/SQL AND

2.3 DATATYPES

Variables/Constants must be declared as a datatype

Oracle is strongly typed (with a few exceptions), meaning the compiler

performs checking rather than run-time engine

SQL Server is not strongly-typed

Procedural environment defines datatypes that do not exist in the

database Solely designed for

use in the procedural

environment

Numeric Data Types

Numeric Datatypes in

SQL Server:

Usually, you use: INTEGER for whole numbers

FLOAT for floating point decimals

MONEY for currency

32

Page 12: Database Programming PROCEDURAL DATABASE PROGRAMMING ( PL/SQL AND

2.3 DATATYPES

In Oracle, the NUMBER datatype is the most commonly used one

Use it to store integer, fixed-point and floating point decimals

Oracle has many sub datatypes based on the main types, they are

simply alternates to comply with ISO SQL, SQL/DS, and DB2 datatypes

Sometimes they do offer

additional functionality

Oracle Numeric Datatypes:

33

Page 13: Database Programming PROCEDURAL DATABASE PROGRAMMING ( PL/SQL AND

2.3 DATATYPES

Character Data Types

Two general forms of character datatypes:

Strings of single-byte characters

Strings of Unicode characters (uses several bytes)

Furthermore, strings

can have fixed or

variable lengths

SQL Server Character

Datatypes:

34

Page 14: Database Programming PROCEDURAL DATABASE PROGRAMMING ( PL/SQL AND

2.3 DATATYPES

If content of CHAR(n) is less than n, the rest of the string is padded

with blanks

VARCHAR is always stored in its actual length

In general, use VARCHAR datatype

Oracle Character

Datatypes:

35

Page 15: Database Programming PROCEDURAL DATABASE PROGRAMMING ( PL/SQL AND

2.3 DATATYPES

Temporal Data Types

Date/Time datatype is complicated: Highly formatted and many

different rules (leap days/years, daylight savings time, holidays)

Procedural DB environment provides support for SQL standard interval

arithmetic, datetime literals, and comprehensive functions to

manipulate date and time

information

SQL Server Date/Time

datatypes:

36

Page 16: Database Programming PROCEDURAL DATABASE PROGRAMMING ( PL/SQL AND

2.3 DATATYPES

Default date value in SS: ‘mmm dd yyyy’ is controlled by SET

DATEFROMAT statement

Oracle Date/Time

Datatypes:

Example of Oracle

Date/Time Values:

37

Page 17: Database Programming PROCEDURAL DATABASE PROGRAMMING ( PL/SQL AND

2.3 DATATYPES

Miscellaneous Data Types

Additional datatypes that do not fit into the previous categories:

38

Page 18: Database Programming PROCEDURAL DATABASE PROGRAMMING ( PL/SQL AND

2.4 DATATYPE CONVERSION

In general, we humans like to see all data as strings

For this very reason, we have to perform conversions between various datatypes

Mostly we are referring to numbers and dates as strings and vice versa

But also within the number datatype, we convert, for example, floating-point to fixed-point decimals

In many cases, you can leave it up to the programming environment to automatically convert implicit conversion (not recommended)

When using specific conversion functions explicit conversion

In SS, there are three functions: CAST, CONVERT, FORMAT

In Oracle, only CAST

Additionally, there are TO_[datatype] functions:

TO_NUMBER, TO_CHAR, TO_DATE

These functions are more flexible and powerful

39

Page 19: Database Programming PROCEDURAL DATABASE PROGRAMMING ( PL/SQL AND

2.4 DATATYPE CONVERSION

Number Conversions

Oracle format model elements:

If expression is of type Money or SmallMoney, apply following styles:

40

Page 20: Database Programming PROCEDURAL DATABASE PROGRAMMING ( PL/SQL AND

2.4 DATATYPE CONVERSION

When expression is float or

real, apply following styles:

41

Page 21: Database Programming PROCEDURAL DATABASE PROGRAMMING ( PL/SQL AND

2.4 DATATYPE CONVERSION

42

Page 22: Database Programming PROCEDURAL DATABASE PROGRAMMING ( PL/SQL AND

2.4 DATATYPE CONVERSION

Date/Time Conversion

Date/Time values always need to be formatted since date/time values

are stored in a specific, non-human format in the database.

Therefore, you need to convert from database dates into strings and

from strings into database dates.

43

Page 23: Database Programming PROCEDURAL DATABASE PROGRAMMING ( PL/SQL AND

2.4 DATATYPE CONVERSION

Date/Time String:

String Date/Time:

You can also use default

format for each data type

44

Page 24: Database Programming PROCEDURAL DATABASE PROGRAMMING ( PL/SQL AND

2.4 DATATYPE CONVERSION

45

Page 25: Database Programming PROCEDURAL DATABASE PROGRAMMING ( PL/SQL AND

2.4 DATATYPE CONVERSION

46

Page 26: Database Programming PROCEDURAL DATABASE PROGRAMMING ( PL/SQL AND

2.5 BUILT-IN FUNCTIONS, SYSTEM FUNCTIONS

Review of most important built-in functions (covered in more detail in

Managing Data(bases) using SQL course)

Functions can be categorized by data type (string, number, date) and

special categories such as mathematical, financial, statistical, etc

There are differences in those functions in Oracle and SQL Server,

however, you should be able to solve any business problem using

procedural SQL with those functions in any platform

String Functions

47

Page 27: Database Programming PROCEDURAL DATABASE PROGRAMMING ( PL/SQL AND

2.5 BUILT-IN FUNCTIONS, SYSTEM FUNCTIONS

48

Page 28: Database Programming PROCEDURAL DATABASE PROGRAMMING ( PL/SQL AND

2.5 BUILT-IN FUNCTIONS, SYSTEM FUNCTIONS

49

Page 29: Database Programming PROCEDURAL DATABASE PROGRAMMING ( PL/SQL AND

2.5 BUILT-IN FUNCTIONS, SYSTEM FUNCTIONS

50

Page 30: Database Programming PROCEDURAL DATABASE PROGRAMMING ( PL/SQL AND

2.5 BUILT-IN FUNCTIONS, SYSTEM FUNCTIONS

Mathematical and Numeric Functions

51

Page 31: Database Programming PROCEDURAL DATABASE PROGRAMMING ( PL/SQL AND

2.5 BUILT-IN FUNCTIONS, SYSTEM FUNCTIONS

52

Page 32: Database Programming PROCEDURAL DATABASE PROGRAMMING ( PL/SQL AND

2.5 BUILT-IN FUNCTIONS, SYSTEM FUNCTIONS

53

Page 33: Database Programming PROCEDURAL DATABASE PROGRAMMING ( PL/SQL AND

2.5 BUILT-IN FUNCTIONS, SYSTEM FUNCTIONS

Date/Time Functions

54

Page 34: Database Programming PROCEDURAL DATABASE PROGRAMMING ( PL/SQL AND

2.5 BUILT-IN FUNCTIONS, SYSTEM FUNCTIONS

55

Page 35: Database Programming PROCEDURAL DATABASE PROGRAMMING ( PL/SQL AND

2.5 BUILT-IN FUNCTIONS, SYSTEM FUNCTIONS

56