Distributed Database Applications COSC 5050 Week One.

Slides:



Advertisements
Similar presentations
BD05/06 PL/SQL  Introduction  Structure of a block  Variables and types  Accessing the database  Control flow  Cursors  Exceptions  Procedures.
Advertisements

AN INTRODUCTION TO PL/SQL Mehdi Azarmi 1. Introduction PL/SQL is Oracle's procedural language extension to SQL, the non-procedural relational database.
PL/SQL. Introduction to PL/SQL PL/SQL is the procedure extension to Oracle SQL. It is used to access an Oracle database from various environments (e.g.
Stored Procedure Language Stored Procedure Overview Stored Procedure is a function in a shared library accessible to the database server can also write.
Library example CREATE TABLE books ( isbn VARCHAR2(13) PRIMARY KEY, title VARCHAR2(200), summary VARCHAR2(2000), author VARCHAR2(200), date_published DATE,
PL/SQL (Procedural Language extensions to SQL) Prepared by: Manoj Kathpalia Edited by: M V Ramakrishna.
Lecture-5 Though SQL is the natural language of the DBA, it suffers from various inherent disadvantages, when used as a conventional programming language.
Using Oracle PL/SQL PL/SQL stands for Procedural Language/SQL. PL/SQL extends SQL by adding constructs found in procedural languages, resulting in a structural.
1 Copyright © 2004, Oracle. All rights reserved. Creating Stored Procedures.
PL/SQL Agenda: Basic PL/SQL block structure
Matthew P. Johnson, OCL1, CISDD CUNY, F20041 OCL1 Oracle 10g: SQL & PL/SQL Session #7 Matthew P. Johnson CISDD, CUNY Fall, 2004.
Oracle SQL*plus John Ortiz. Lecture 10SQL: Overview2 Overview  SQL: Structured Query Language, pronounced S. Q. L. or sequel.  A standard language for.
Matthew P. Johnson, OCL2, CISDD CUNY, January OCL2 Oracle 10g: SQL & PL/SQL Session #8 Matthew P. Johnson CISDD, CUNY Fall, 2004.
Matthew P. Johnson, OCL4, CISDD CUNY, Sept OCL4 Oracle 10g: SQL & PL/SQL Session #6 Matthew P. Johnson CISDD, CUNY June, 2005.
2 Copyright © 2004, Oracle. All rights reserved. Creating Stored Functions.
PL/SQL Introduction Database 1. Practice. Sample Database The schema of the sample database is the following: Drinkers (name, occupation, birthday, salary)
PL/SQL Bulk Collections in Oracle 9i and 10g Kent Crotty Burleson Consulting October 13, 2006.
Dr. James Dullea, CSC8490 Introduction to PL/SQLSlide 1 of 36 7From Prof. Dullea CSC8490 Introduction to PL/SQL Module 01-9 Revised: June 12, 2005 Dr.
Introduction to PL/SQL. Procedural Language extension for SQL Oracle Proprietary 3GL Capabilities Integration of SQL Portable within Oracle data bases.
Advanced Databases Advanced PL/SQL Programming: Procedure, Function and Package.
Distributed Database Applications COSC 5050 Week Three.
PL / SQL P rocedural L anguage / S tructured Q uery L anguage Chapter 7 in Lab Reference.
SQL Training Procedures & Functions. Confidential & Proprietary Copyright © 2009 Cardinal Directions, Inc. DB Procedures & Functions Procedures and Functions.
Lecture 4 PL/SQL language. PL/SQL – procedural SQL Allows combining procedural and SQL code PL/SQL code is compiled, including SQL commands PL/SQL code.
Oracle10g Developer: PL/SQL Programming1 Objectives Programming fundamentals The PL/SQL block Define and declare variables Initialize variables The NOT.
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 2 Basic PL/SQL Block Structures.
CPS120: Introduction to Computer Science Lecture 19 Introduction to SQL.
1 Copyright © 2004, Oracle. All rights reserved. Introduction to PL/SQL.
Overview · What is PL/SQL · Advantages of PL/SQL · Basic Structure of a PL/SQL Block · Procedure · Function · Anonymous Block · Types of Block · Declaring.
PL SQL Block Structures. What is PL SQL A good way to get acquainted with PL/SQL is to look at a sample program. PL/SQL combines the data manipulating.
Distributed Database Applications COSC 5050 Week Seven.
Lecture 8 Creating Stored Functions. Objectives  After completing this lesson, you should be able to do the following:  What is Function?  Types of.
Matthew P. Johnson, OCL3, CISDD CUNY, June OCL3 Oracle 10g: SQL & PL/SQL Session #8 Matthew P. Johnson CISDD, CUNY June, 2004.
CS178 Database Management PL/SQL session 8 References: ORACLE 9i PROGRAMMING A Primer Rajshekhar Sunderraman.
PL / SQL By Mohammed Baihan. What is PL/SQL? PL/SQL stands for Procedural Language extension of SQL. PL/SQL is a combination of SQL along with the procedural.
PL/SQL Oracle's Database Programming Language. Remember: Set serveroutput on With serveroutput off (default) executing procedure: With serveroutput on:
PL/SQL Procedural Language / Structured Query Language.
1 Copyright © 2004, Oracle. All rights reserved. Introduction to PL/SQL.
PL/SQL Block Structure DECLARE - Optional Variables, cursors, user-defined exceptions BEGIN - Mandatory SQL Statements PL/SQL Statements EXCEPTIONS - Optional.
Distributed Database Applications COSC 5050 Week Five.
Chapter 15 Introduction to PL/SQL. Chapter Objectives  Explain the benefits of using PL/SQL blocks versus several SQL statements  Identify the sections.
Database Application Development using PL/SQL Programming.
Database Technology Jing Shen.
Retrieving Data in PL/SQL. 2 home back first prev next last What Will I Learn? In this lesson, you will learn to: –Recognize the SQL statements that can.
Creating Functions. V 12 NE - Oracle 2006 Overview of Stored Functions A function is a named PL/SQL block that returns a value A function can be stored.
Handling Exceptions. Objectives What is exception Types of exceptions How to handle exceptions Trapping pre defined oracle errors.
Distributed Database Applications COSC 5050 Week Six.
PL/SQL programming Procedures and Cursors Lecture 1 [Part 2]
PRACTICE OVERVIEW PL/SQL Part Your stored procedure, GET_BUDGET, has a logic problem and must be modified. The script that contains the procedure.
Text TCS INTERNAL Oracle PL/SQL – Introduction. TCS INTERNAL PL SQL Introduction PLSQL means Procedural Language extension of SQL. PLSQL is a database.
Kingdom of Saudi Arabia Ministry of Higher Education Al-Imam Muhammad Ibn Saud Islamic University College of Computer and Information Sciences Overview.
Introduction to PL/SQL N. Dimililer. About PL/SQL –PL/SQL is an extension to SQL with design features of programming languages. –Data manipulation and.
STORED PROCEDURE & STORED FUNCTION Politeknik Telkom 2012.
CS422 Principles of Database Systems Oracle PL/SQL Chengyu Sun California State University, Los Angeles.
1 Copyright © 2004, Oracle. All rights reserved. PL/SQL Programming Concepts: Review.
PLSQL Cont…. Most Common Oracle Data Types VARCHAR2 –Stores variable-length character data. –Takes a required parameter that specifies a maximum length.
Creating Stored Functions
Interacting with the Oracle Server
Creating Stored Procedures and Functions
Interacting with the Oracle Server
Oracle11g: PL/SQL Programming Chapter 2 Basic PL/SQL Block Structures.
Database Application Development
Introduction to PL/SQL
Handling Exceptions.
SQL PL/SQL Presented by: Dr. Samir Tartir
Database Management Systems 2
PL/SQL – Session 1 of 4 Instructor: Jim Cody
PL/SQL week10.
Stored Procedure Language
Database Application Development
Presentation transcript:

Distributed Database Applications COSC 5050 Week One

Jiangping Wang Webster UniversityDistributed Database Applications Outline Introduction Course overview Oracle client environment Data dictionary Language fundamentals Program control

Jiangping Wang Webster UniversityDistributed Database Applications Introduction Oracle database PL/SQL Accessing Oracle server Database objects DDL and DML statements

Jiangping Wang Webster UniversityDistributed Database Applications PL/SQL Many Oracle applications are built using client-server architecture The Oracle database resides on the server PL/SQL is like any other programming language PL/SQL is not a stand-alone programming language PL/SQL is a part of the Oracle RDBMS

Jiangping Wang Webster UniversityDistributed Database Applications PL/SQL Highly structured, readable, accessible language Standard and portable language Embedded language High-performance, highly integrated database language

Jiangping Wang Webster UniversityDistributed Database Applications Advantages of PL/SQL

Jiangping Wang Webster UniversityDistributed Database Applications SQL Example The semicolon terminates CREATE, INSERT, SELECT, and DROP statements CREATE TABLE STUDENT ( FIRST_NAME VARCHAR2(20), LAST_NAME VARCHAR2(20) ); INSERT INTO STUDENT VALUES (‘JOHN’, ‘LUCKY’); SELECT FIRST_NAME, LAST_NAME FROM STUDENT; DROP TABLE STUDENT;

Jiangping Wang Webster UniversityDistributed Database Applications PL/SQL Example Page 7, 8, 9, 39 examples When Oracle reads a PL/SQL block, a semicolon marks the end of the individual statement within the block It is not a block terminator! The “/” executes the PL/SQL block

Jiangping Wang Webster UniversityDistributed Database Applications Integration with SQL DECLARE l_book_count INTEGER; BEGIN SELECT COUNT(*) INTO l_book_count FROM books WHERE author LIKE '%Feuerstein, Steven%'; DBMS_OUTPUT.PUT_LINE( 'Steven have written (or co-written) ' || l_book_count || ' books.'); UPDATE books SET author = REPLACE (author, 'Steven', 'Stephen') WHERE author LIKE '%Feuerstein, Steven%'; END;

Jiangping Wang Webster UniversityDistributed Database Applications Control and Conditional Logic CREATE OR REPLACE PROCEDURE pay_out_balance( account_id_in IN accounts.id%TYPE) IS l_balance_remaining NUMBER; BEGIN LOOP l_balance_remaining := account_balance (account_id_in); IF l_balance_remaining < 1000 THEN EXIT; ELSE apply_balance ( accont_id_in, l_balance_remaining); END IF; END LOOP; END pay_out_balance;

Jiangping Wang Webster UniversityDistributed Database Applications When Things Go Wrong CREATE OR REPLACE PROCEDURE check_account(account_id_in IN accounts.id%TYPE) IS l_balance_remainingNUMBER; l_balance_below_minimum EXCEPTION; l_account_nameaccounts.name%TYPE; BEGIN SELECT name INTO l_account_name FROM accounts WHERE id = account_id_in; l_balance_remaining := account_balance (account_id_in); DBMS_OUTPUT.put_line ( 'Balance for ' || l_account_name || ' = ' || l_balance_remaining); IF l_balance_remaining < 1000 THEN RAISE l_balance_below_minimum; END IF; EXCEPTION WHEN NO_DATA_FOUND THEN log_error (...); WHEN l_balance_below_minimum THEN log_error (...); RAISE; END;

Jiangping Wang Webster UniversityDistributed Database Applications Creating a Stored Program CREATE OR REPLACE FUNCTION wordcount (str IN VARCHAR2) RETURN PLS_INTEGER AS words PLS_INTEGER := 0; len PLS_INTEGER := NVL(LENGTH(str),0); inside_a_word BOOLEAN; BEGIN FOR i IN 1..len + 1 LOOP IF ASCII(SUBSTR(str, i, 1)) len THEN IF inside_a_word THEN words := words + 1; inside_a_word := FALSE; END IF; ELSE inside_a_word := TRUE; END IF; END LOOP; RETURN words; END;

Jiangping Wang Webster UniversityDistributed Database Applications Executing, Showing, and Dropping a Stored Program BEGIN DBMS_OUTPUT.PUT_LINE( 'There are ' || wordcount(CHR(9)) || ' words in a tab'); END; SELECT * FROM USER_OBJECTS; DESC wordcount SELECT TEXT FROM USER_SOURCE WHERE NAME = 'WORDCOUNT'; DROP FUNCTION wordcount;

Jiangping Wang Webster UniversityDistributed Database Applications Access Oracle Oracle server (service and host name) cronus, icarus.webster.edu Oracle client Oracle 11g client download SQL*Plus SQL Developer Application Express (APEX) Workspace Your login and password

Jiangping Wang Webster UniversityDistributed Database Applications Oracle SQL*Plus

Jiangping Wang Webster UniversityDistributed Database Applications Oracle SQL Developer

Jiangping Wang Webster UniversityDistributed Database Applications Oracle APEX

Jiangping Wang Webster UniversityDistributed Database Applications Data Dictionary USER_ ALL_ DBA_ select view_name from all_views where view_name like 'USER%';

Jiangping Wang Webster UniversityDistributed Database Applications Language Fundamentals PL/SQL block structure Modularization Scope Anonymous blocks Named blocks Procedures Functions Scope and visibility

Jiangping Wang Webster UniversityDistributed Database Applications PL/SQL Block Structure Header Declaration section Execution section Exception section PROCEDURE get_happy (ename_in IN VARCHAR2) IS hiredata DATE; BEGIN hiredate := SYSDATE – 2; INSERT INTO employee (emp_name, hiredate) VALUES (ename_in, hiredate); EXCEPTION WHEN dup_val_in_index THEN DBMS_OUTPUT.PUT_LINE (‘Cannot insert.’); END;

Jiangping Wang No header Begin with either DECLARE or BEGIN Cannot be called Webster UniversityDistributed Database Applications Anonymous Blocks BEGIN DBMS_OUTPUT.PUT_LINE (SYSDATE); END; DECLARE l_right_now DATE := SYSDATE; BEGIN DBMS_OUTPUT.PUT_LINE (l_right_now); END;

Jiangping Wang Procedures Functions Webster UniversityDistributed Database Applications Named Blocks procedure Add_employee (ssninvarchar2, fnameinvarchar2, lnameinvarchar2, dept_numinnumber, codeinnumber, sup_ssninvarchar2) is begin insert into employee values (ssn, fname, lname, dept_num, code, sup_ssn); end; -- Add_employee

Jiangping Wang Webster UniversityDistributed Database Applications Named Blocks function get_department(ssn_in in employee.ssn%type) return department.dept_name%type is l_dept_name department.dept_name%type; begin select dept_name into l_dept_name from department inner join employee on employee.dept_num = department.dept_num where ssn = ssn_in; dbms_output.put_line('department name: ' || l_dept_name); return l_dept_name; exception when no_data_found then dbms_output.put_line( 'no such employee or not in any department!'); return null; end;

Jiangping Wang Named Blocks in DB2 Webster UniversityDistributed Database Applications CREATE PROCEDURE sum( IN p_a INTEGER, IN p_b INTEGER, OUT p_s INTEGER) LANGUAGE SQL BEGIN SET p_s = p_a + p_b; END; CALL sum(100,200,?);

Jiangping Wang Webster UniversityDistributed Database Applications Language Fundamentals PL/SQL character set Case-insensitive language Identifiers Up to 30 characters in length Must start with a letter Can include $, _, and # Cannot contain spaces PL/SQL keywords Comments Single-line comments using “--” Multi-line comments using /* … */

Jiangping Wang Webster UniversityDistributed Database Applications PL/SQL Character Set TypeCharacters LettersA-Z, a-z Digits0-9 Symbols~ # $ % * ( ) _ - + = | : ; " ',. ? / ^ WhitespaceTab, space, newline, carriage return

Jiangping Wang Distributed Database Applications Program Control IF statements CASE LOOP WHILE loop Cursor loop

Jiangping Wang Distributed Database Applications IF Statement IF salary > THEN give_bonus (employee_id, 500); END IF; IF salary <= THEN give_bonus (employee_id, 0); ELSE give_bonus (employee_id, 500); END IF; declare salary number := 40000; begin IF salary > THEN dbms_output.put_line('Salary is greater than 40000'); ELSE dbms_output.put_line('Salary is not greater than 40000'); END IF; end; /

Jiangping Wang Distributed Database Applications IF Statement declare salary number := &salary; begin IF salary BETWEEN AND THEN dbms_output.put_line('Give bonus 1500.'); ELSIF salary BETWEEN AND THEN dbms_output.put_line('Give bonus 1000.'); ELSIF salary > THEN dbms_output.put_line('Give bonus 500.'); ELSE dbms_output.put_line('Give bonus 0.'); END IF; end; /

Jiangping Wang Distributed Database Applications CASE Statement declare salary_level number := &salary; begin case salary_level when 1 then dbms_output.put_line('give bonus 1500.'); when 2 then dbms_output.put_line('give bonus 1000.'); when 3 then dbms_output.put_line('give bonus 500.'); else dbms_output.put_line('give bonus 0.'); end case; end; / Simple CASE statement

Jiangping Wang Searched CASE statement Distributed Database Applications CASE Statement declare salary number := &salary; begin case when salary between and then dbms_output.put_line('give bonus 1500.'); when salary between and then dbms_output.put_line('give bonus 1000.'); when salary > then dbms_output.put_line('give bonus 500.'); else dbms_output.put_line('give bonus 0.'); end case; end; /

Jiangping Wang CASE expression Distributed Database Applications CASE Statement declare salary number := &salary; bonus_amount number; begin bonus_amount := case when salary BETWEEN AND THEN 1500 when salary BETWEEN AND THEN 1000 when salary > THEN 500 else 0 end; dbms_output.put_line( 'Give bonus ' || bonus_amount || '.'); end; /

Jiangping Wang Distributed Database Applications Loop Statement Simple loop FOR loop WHILE loop Cursor FOR loop

Jiangping Wang Distributed Database Applications Simple Loop PROCEDURE set_all_ranks (max_rank_in IN INTEGER) IS ranking_level NUMBER (3) := 1; BEGIN LOOP EXIT WHEN ranking_level > max_rank_in; set_rank (ranking_level); ranking_level := ranking_level + 1; END LOOP; END set_all_ranks; Use EXIT or EXIT WHEN to exit loop

Jiangping Wang Distributed Database Applications For Loop PROCEDURE set_all_ranks (max_rank_in IN INTEGER) IS ranking_level NUMBER (3) := 1; BEGIN FOR ranking_level IN 1.. max_rank_in LOOP set_rank (ranking_level); END LOOP; END set_all_ranks; Reverse loop: FOR counter IN REVERSE 1.. max LOOP … END LOOP;

Jiangping Wang Distributed Database Applications WHILE Loop PROCEDURE set_all_ranks (max_rank_in IN INTEGER) IS ranking_level NUMBER (3) := 1; BEGIN WHILE ranking_level <= max_rank_in LOOP set_rank (ranking_level); ranking_level := ranking_level + 1; END LOOP; END set_all_ranks;

Jiangping Wang Distributed Database Applications Cursor FOR Loop Cursor with simple loop DECLARE CURSOR name_cur IS SELECT lname, fname FROM employee WHERE ssn like '8%'; name_rec name_cur%ROWTYPE; BEGIN OPEN name_cur; LOOP FETCH name_cur INTO name_rec; EXIT WHEN name_cur%NOTFOUND; DBMS_OUTPUT.PUT_LINE( name_rec.fname || ' ' || name_rec.lname); END LOOP; CLOSE name_cur; END; /

Jiangping Wang Distributed Database Applications Cursor FOR Loop Cursor with simple loop DECLARE CURSOR occupancy_cur IS SELECT pet_id, room_number FROM occupancy WHERE occupied_dt = TRUNC(SYSDATE); occupancy_rec occupancy_cur%ROWTYPE; BEGIN OPEN occupancy_cur; LOOP FETCH occupancy_cur INTO occupancy_rec; EXIT WHEN occupancy_cur%NOTFOUND; update_bill (occupancy_rec.pet_id, occupancy_rec.room_number); END LOOP; CLOSE occupancy_cur; END;

Jiangping Wang Distributed Database Applications Cursor FOR Loop Cursor For loop DECLARE CURSOR occupancy_cur IS SELECT pet_id, room_number FROM occupancy WHERE occupied_dt = TRUNC(SYSDATE); BEGIN FOR occupancy_rec IN occupancy_cur LOOP update_bill (occupancy_rec.pet_id, occupancy_rec.room_number); END LOOP; END;

Jiangping Wang Distributed Database Applications Cursor FOR Loop Cursor FOR loop DECLARE CURSOR name_cur IS SELECT lname, fname FROM employee WHERE ssn like '8%'; BEGIN FOR name_rec IN name_cur LOOP DBMS_OUTPUT.PUT_LINE( name_rec.fname || ' ' || name_rec.lname); END LOOP; END; /

Jiangping Wang Webster UniversityDistributed Database Applications Homework Given the table layout, write the SQL to create the table Create a script file to load database table Create an anonymous PL/SQL block to retrieve data from above database table Create a simple procedure in your DB2 Company schema to retrieve employee data Project proposal