Database Programming with PL/SQL

Similar documents
Database Programming with PL/SQL

VHDL Lexical Elements

Database Programming with PL/SQL

Database Programming with SQL

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

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

ORACLE: PL/SQL Programming

PL / SQL Basics. Chapter 3

Database Programming with PL/SQL

Working with Columns, Characters and Rows. Copyright 2008, Oracle. All rights reserved.

Overview of PL/SQL. About PL/SQL. PL/SQL Environment. Benefits of PL/SQL. Integration

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

LECTURE 02 INTRODUCTION TO C++

Standard 11. Lesson 9. Introduction to C++( Up to Operators) 2. List any two benefits of learning C++?(Any two points)

Writing PL/SQL Executable Statements. Copyright 2007, Oracle. All rights reserved.

GIFT Department of Computing Science Data Selection and Filtering using the SELECT Statement

RESTRICTING AND SORTING DATA

Introduction to Computer Science and Business

Chapter 1 CONTROL STRUCTURES. SYS-ED/ Computer Education Techniques, Inc.

Restricting and Sorting Data. Copyright 2004, Oracle. All rights reserved.

STRUCTURED QUERY LANGUAGE (SQL)

COSC 304 Introduction to Database Systems SQL DDL. Dr. Ramon Lawrence University of British Columbia Okanagan

Using DDL Statements to Create and Manage Tables. Copyright 2004, Oracle. All rights reserved.

Creating and Managing Tables Schedule: Timing Topic

The Warhol Language Reference Manual

Chapter 4. Basic SQL. Copyright 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Chapter 2: Introduction to C++

Course Outline and Objectives: Database Programming with SQL

Using DDL Statements to Create and Manage Tables. Copyright 2004, Oracle. All rights reserved.

Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen

Introduction to Computer Science and Business

Restricting and Sorting Data. Copyright 2004, Oracle. All rights reserved.

JME Language Reference Manual

Chapter 4. Basic SQL. SQL Data Definition and Data Types. Basic SQL. SQL language SQL. Terminology: CREATE statement

ADVANTAGES. Via PL/SQL, all sorts of calculations can be done quickly and efficiently without use of Oracle engine.

XQ: An XML Query Language Language Reference Manual

BASIC ELEMENTS OF A COMPUTER PROGRAM

Deepak Bhinde PGT Comp. Sc.

RMS Report Designing

Intro to Programming & C Why Program? 1.2 Computer Systems: Hardware and Software. Why Learn to Program?

Database Programming with PL/SQL

Database Programming with SQL 5-1 Conversion Functions. Copyright 2015, Oracle and/or its affiliates. All rights reserved.

Contents I Introduction 1 Introduction to PL/SQL iii

! A literal represents a constant value used in a. ! Numbers: 0, 34, , -1.8e12, etc. ! Characters: 'A', 'z', '!', '5', etc.

Using the Set Operators. Copyright 2006, Oracle. All rights reserved.

ACCESS isn t only a great development tool it s

REVIEW. The C++ Programming Language. CS 151 Review #2

Using DbVisualizer Variables

Oracle Database 10g: Introduction to SQL

Lab #2 CIS 208A - PL/SQL Course

CSC Web Programming. Introduction to SQL

Oracle Database: SQL and PL/SQL Fundamentals NEW

Oracle 1Z Oracle Database 11g SQL Fundamentals I. Download Full Version :

Typescript on LLVM Language Reference Manual

Lesson B Objectives IF/THEN. Chapter 4B: More Advanced PL/SQL Programming

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 3

Integer Data Types. Data Type. Data Types. int, short int, long int

Oracle Database: SQL and PL/SQL Fundamentals Ed 2

SELF TEST. List the Capabilities of SQL SELECT Statements

Full file at

BIT Java Programming. Sem 1 Session 2011/12. Chapter 2 JAVA. basic

INDEX. 1 Basic SQL Statements. 2 Restricting and Sorting Data. 3 Single Row Functions. 4 Displaying data from multiple tables

Getting started with Java

Oracle Database 11g: SQL and PL/SQL Fundamentals

Getting Information from a Table

C++ Programming: From Problem Analysis to Program Design, Third Edition

Datatypes, Variables, and Operations

chapter 2 G ETTING I NFORMATION FROM A TABLE

Database Foundations. 6-4 Data Manipulation Language (DML) Copyright 2015, Oracle and/or its affiliates. All rights reserved.

Database Programming with SQL

Database Programming with PL/SQL

Java Notes. 10th ICSE. Saravanan Ganesh

Table of Contents. PDF created with FinePrint pdffactory Pro trial version

ARG! Language Reference Manual

5. Single-row function

Using DDL Statements to Create and Manage Tables. Copyright 2006, Oracle. All rights reserved.

Overview: Programming Concepts. Programming Concepts. Names, Values, And Variables

Overview: Programming Concepts. Programming Concepts. Chapter 18: Get With the Program: Fundamental Concepts Expressed in JavaScript

Contents. Jairo Pava COMS W4115 June 28, 2013 LEARN: Language Reference Manual

SQL Functionality SQL. Creating Relation Schemas. Creating Relation Schemas

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program

1 Lexical Considerations

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

Objectives. In this chapter, you will:

2 PL/SQL - fundamentals Variables and Constants Operators SQL in PL/SQL Control structures... 7

QUark Language Reference Manual

Exact Numeric Data Types

IT 374 C# and Applications/ IT695 C# Data Structures

SQL: Data De ni on. B0B36DBS, BD6B36DBS: Database Systems. h p:// Lecture 3

COSC344 Database Theory and Applications. Lecture 6 SQL Data Manipulation Language (1)

JOSE LUIS JUAREZ VIVEROS com) has a. non-transferable license to use this Student Guide

The SPL Programming Language Reference Manual

Programming in C++ 4. The lexical basis of C++

Tables From Existing Tables

MATVEC: MATRIX-VECTOR COMPUTATION LANGUAGE REFERENCE MANUAL. John C. Murphy jcm2105 Programming Languages and Translators Professor Stephen Edwards

BoredGames Language Reference Manual A Language for Board Games. Brandon Kessler (bpk2107) and Kristen Wise (kew2132)

Petros: A Multi-purpose Text File Manipulation Language

Ex: If you use a program to record sales, you will want to remember data:

Digital Design Using VHDL Using Xilinx s Tool for Synthesis and ModelSim for Verification Part II

CSC Web Programming. Introduction to JavaScript

Transcription:

Database Programming with PL/SQL 2-2

Objectives This lesson covers the following objectives: List and define the different types of lexical units available in PL/SQL Describe identifiers and identify valid and invalid identifiers in PL/SQL Describe and identify reserved words, delimiters, literals, and comments in PL/SQL 3

Purpose A spoken language has different parts of speech. Each part of speech (such as an adjective, noun, and verb) is used differently and must follow rules. Similarly, a programming language has different parts of speech that are used differently and must follow rules. These parts of speech are called lexical units. 4

Lexical Units in a PL/SQL Block Lexical units: Are the building blocks of any PL/SQL block Are sequences of characters including letters, digits, tabs, returns, and symbols Can be classified as: Identifiers Reserved words Delimiters Literals Comments 5

Identifiers An identifier is the name given to a PL/SQL object, including any of the following: Procedure Function Variable Exception Constant Package Record PL/SQL table Cursor Do not be concerned if you do not know what all of the above objects are. You will learn about PL/SQL objects throughout this course. 6

Identifiers Highlighted Several identifiers are highlighted in the PL/SQL code shown below. PROCEDURE print_date IS BEGIN END; v_date VARCHAR2(30); SELECT TO_CHAR(SYSDATE,'Mon DD, YYYY') INTO v_date FROM DUAL; DBMS_OUTPUT.PUT_LINE(v_date); Key: Variables Packages Procedures Functions 7

Identifier Properties Identifiers: Maximum 30 characters in length Must begin with a letter May include $ (dollar sign), _ (underscore), or # (hashtag) May not contain spaces Identifiers are NOT case sensitive 8

Valid and Invalid Identifiers Examples of valid identifiers: First_Name LastName address_1 ID# Total_$ primary_department_contact Examples of invalid identifiers: First Name Contains a space Last-Name Contains invalid symbol "-" 1st_address_line Begins with a number Total_% Contains invalid symbol "%" primary_building_department_contact More than 30 characters 9

Reserved Words Reserved words are words that have special meaning to the Oracle database. Reserved words cannot be used as identifiers in a PL/SQL program. 10

Partial List of Reserved Words The following is a partial list of reserved words. ALL CREATE FROM MODIFY SELECT ALTER DATE GROUP NOT SYNONYM AND DEFAULT HAVING NULL SYSDATE ANY DELETE IN NUMBER TABLE AS DESC INDEX OR THEN ASC DISTINCT INSERT ORDER UPDATE BETWEEN DROP INTEGER RENAME VALUES CHAR ELSE INTO ROW VARCHAR2 COLUMN EXISTS IS ROWID VIEW COMMENT FOR LIKE ROWNUM WHERE Note: For more information, refer to the PL/SQL User s Guide and Reference. 11

Using Reserved Words What happens when you try to use a reserved word as an identifier in a PL/SQL program? DECLARE date DATE; BEGIN SELECT ADD_MONTHS(SYSDATE,3) INTO date FROM dual; END; ORA-06550: line 4, column 37: PL/SQL: ORA-00936: missing expression ORA-06550: line 4, column 3: PL/SQL: SQL Statement ignored 2. date DATE; 3. BEGIN 4. SELECT ADD_MONTHS(SYSDATE,3) INTO date 5. FROM DUAL; 6. END; 12

Delimiters Delimiters are symbols that have special meaning. Simple delimiters consist of one character. Symbol Meaning + addition operator - subtraction/negation operator * multiplication operator / division operator = equality operator ' character string delimiter ; statement terminator 13

Delimiters Compound delimiters consist of two characters. Symbol Meaning <> inequality operator!= inequality operator concatenation operator - - single-line comment indicator /* beginning comment delimiter */ ending comment delimiter ** exponent := assignment operator 14

Literals A literal is an explicit numeric, character string, date, or Boolean value that might be stored in a variable. Literals are classified as: Character (also known as string literals) Numeric Boolean 15

Character Literals Character literals: May include any printable character in the PL/SQL character set: letters, numerals, spaces, and symbols Typically defined using the VARCHAR2 data type Must be enclosed by character string delimiters (') Can be composed of zero or more characters Are case sensitive; therefore, PL/SQL is NOT equivalent to pl/sql 16

Character Literals The following are examples of character literals being assigned to variables. The literals are the characters between the single quotes (the character string delimiters) and are shown here in red text for emphasis. DECLARE v_firstname v_classroom v_course_id BEGIN VARCHAR2(30) := 'John'; VARCHAR2(4) := '12C'; VARCHAR2(8 := 'CS 101'; 17

Numeric Literals Literals that represent numbers are numeric literals. Numeric literals can be a simple value (ex. 5, -32.5, 127634, 3.141592) Scientific notation also may be used (ex. 2E5, meaning 2*(10 to the power of 5). Typically defined using the NUMBER data type 18

Numeric Literals The following are examples of numeric literals being assigned to variables (and one constant). The literals are shown here in red text for emphasis. DECLARE v_classroom NUMBER(3) := 327; v_grade NUMBER(3) := 95; v_price NUMBER(5) := 150; v_salary NUMBER(8) := 2E5; c_pi CONSTANT NUMBER(7,6) := 3.141592; BEGIN 19

Boolean Literals Values that are assigned to Boolean variables are Boolean literals. TRUE, FALSE, and NULL are the Boolean literals. DECLARE v_new_customer v_fee_paid v_diploma BEGIN BOOLEAN := FALSE; BOOLEAN := TRUE; BOOLEAN := NULL; Note that character string delimiters are not required. 20

Comments Comments explain what a piece of code is trying to achieve. Well-placed comments are extremely valuable for code readability and future code maintenance. It is good programming practice to comment code. Comments are ignored by PL/SQL. They make no difference to how a PL/SQL block executes or the results it displays. 21

Syntax for Commenting Code Two ways to indicate comments in PL/SQL When commenting a single line, use two dashes (--) When commenting multiple lines, begin the comment with /* and end the comment with */ DECLARE -- converts monthly salary to annual salary v_montly_sal NUMBER(9,2); v_annual_sal NUMBER(9,2); BEGIN -- begin executable section /* Compute the annual salary based on the monthly salary input from the user */ v_annual_sal := v_monthly_sal * 12; END; -- end block 22

Terminology Key terms used in this lesson included: Lexical units Identifiers Reserved words Delimiters Literals Comments 23

Summary In this lesson, you should have learned how to: List and define the different types of lexical units available in PL/SQL Describe identifiers and identify valid and invalid identifiers in PL/SQL Describe and identify reserved words, delimiters, literals, and comments in PL/SQL 24