database interfaces · copyright © 2004, oracle. all rights reserved. example pl/sql block declare...

27
Copyright © 2004, Oracle. All rights reserved. Database Interfaces

Upload: others

Post on 16-Oct-2020

7 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Database Interfaces · Copyright © 2004, Oracle. All rights reserved. Example PL/SQL Block DECLARE qty_on_hand NUMBER(5); BEGIN SELECT quantity INTO qty_on_hand FROM inventory

Copyright © 2004, Oracle. All rights reserved.

Database Interfaces

Page 2: Database Interfaces · Copyright © 2004, Oracle. All rights reserved. Example PL/SQL Block DECLARE qty_on_hand NUMBER(5); BEGIN SELECT quantity INTO qty_on_hand FROM inventory

Copyright © 2004, Oracle. All rights reserved.

Objectives

After completing this lesson, you should be able to do the following:• Use SQL*Plus and iSQL*Plus to access the Oracle

Database 10g• Describe the logical structure of tables• Use SQL to query, manipulate, and define data• Identify common database interfaces

Page 3: Database Interfaces · Copyright © 2004, Oracle. All rights reserved. Example PL/SQL Block DECLARE qty_on_hand NUMBER(5); BEGIN SELECT quantity INTO qty_on_hand FROM inventory

Copyright © 2004, Oracle. All rights reserved.

What Is SQL?

SQL provides statements for a variety of tasks, including:• Querying data • Inserting, updating, and deleting rows in a table • Creating, replacing, altering, and dropping objects • Controlling access to the database and its objects

SQL unifies all of the preceding tasks in one consistent language.

Page 4: Database Interfaces · Copyright © 2004, Oracle. All rights reserved. Example PL/SQL Block DECLARE qty_on_hand NUMBER(5); BEGIN SELECT quantity INTO qty_on_hand FROM inventory

Copyright © 2004, Oracle. All rights reserved.

Using SQL

There are several tools for interfacing with the database using SQL:• Oracle SQL*Plus and iSQL*Plus • Oracle Forms, Reports, and Discoverer• Oracle Enterprise Manager • Third-party tools

Page 5: Database Interfaces · Copyright © 2004, Oracle. All rights reserved. Example PL/SQL Block DECLARE qty_on_hand NUMBER(5); BEGIN SELECT quantity INTO qty_on_hand FROM inventory

Copyright © 2004, Oracle. All rights reserved.

Enterprise Manager: Seeing the SQL

Page 6: Database Interfaces · Copyright © 2004, Oracle. All rights reserved. Example PL/SQL Block DECLARE qty_on_hand NUMBER(5); BEGIN SELECT quantity INTO qty_on_hand FROM inventory

Copyright © 2004, Oracle. All rights reserved.

What Is SQL*Plus?

• Command-line tool• Used interactively or in batch mode

$ sqlplus /nologSQL*Plus: Release 10.1.0.2.0 - Production on Tue Feb 17 06:17:14 2004Copyright (c) 1982, 2004, Oracle. All rights reserved.SQL> connect ricEnter password:Connected.SQL> SELECT * FROM dual;

D-XSQL>

Page 7: Database Interfaces · Copyright © 2004, Oracle. All rights reserved. Example PL/SQL Block DECLARE qty_on_hand NUMBER(5); BEGIN SELECT quantity INTO qty_on_hand FROM inventory

Copyright © 2004, Oracle. All rights reserved.

What Is iSQL*Plus?

Page 8: Database Interfaces · Copyright © 2004, Oracle. All rights reserved. Example PL/SQL Block DECLARE qty_on_hand NUMBER(5); BEGIN SELECT quantity INTO qty_on_hand FROM inventory

Copyright © 2004, Oracle. All rights reserved.

Using iSQL*Plus

Page 9: Database Interfaces · Copyright © 2004, Oracle. All rights reserved. Example PL/SQL Block DECLARE qty_on_hand NUMBER(5); BEGIN SELECT quantity INTO qty_on_hand FROM inventory

Copyright © 2004, Oracle. All rights reserved.

Describing Data

Page 10: Database Interfaces · Copyright © 2004, Oracle. All rights reserved. Example PL/SQL Block DECLARE qty_on_hand NUMBER(5); BEGIN SELECT quantity INTO qty_on_hand FROM inventory

Copyright © 2004, Oracle. All rights reserved.

Querying Data

The SELECT has three basic parts:• The SELECT List• The FROM clause• The WHERE condition (optional)

Page 11: Database Interfaces · Copyright © 2004, Oracle. All rights reserved. Example PL/SQL Block DECLARE qty_on_hand NUMBER(5); BEGIN SELECT quantity INTO qty_on_hand FROM inventory

Copyright © 2004, Oracle. All rights reserved.

Sorting the Data

SQL> SELECT last_name, department_id, phone_number2 FROM employees3 ORDER BY last_name;

LAST_NAME DEPARTMENT_ID PHONE_NUMBER--------------- ------------- --------------------Abel 80 011.44.1644.429267Ande 80 011.44.1346.629268Atkinson 50 650.124.6234Austin 60 590.423.4569Baer 70 515.123.8888Baida 30 515.127.4563Banda 80 011.44.1346.729268

Page 12: Database Interfaces · Copyright © 2004, Oracle. All rights reserved. Example PL/SQL Block DECLARE qty_on_hand NUMBER(5); BEGIN SELECT quantity INTO qty_on_hand FROM inventory

Copyright © 2004, Oracle. All rights reserved.

Joining Tables

Getting data from more than one table

Page 13: Database Interfaces · Copyright © 2004, Oracle. All rights reserved. Example PL/SQL Block DECLARE qty_on_hand NUMBER(5); BEGIN SELECT quantity INTO qty_on_hand FROM inventory

Copyright © 2004, Oracle. All rights reserved.

Manipulating Data

SQL> INSERT INTO employees2 (EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,PHONE_NUMBER,3 HIRE_DATE,JOB_ID,SALARY,COMMISSION_PCT,4 MANAGER_ID,DEPARTMENT_ID)5 VALUES6 (9999,'Bob','Builder','[email protected]',NULL,sysdate,7 'IT_PROG’,NULL,NULL,100,90);

1 row created.

SQL> UPDATE employees SET SALARY=60002 WHERE EMPLOYEE_ID = 9999;

1 row updated.

SQL> DELETE from employees2 WHERE EMPLOYEE_ID = 9999;

1 row deleted.

Page 14: Database Interfaces · Copyright © 2004, Oracle. All rights reserved. Example PL/SQL Block DECLARE qty_on_hand NUMBER(5); BEGIN SELECT quantity INTO qty_on_hand FROM inventory

Copyright © 2004, Oracle. All rights reserved.

Defining Data

Page 15: Database Interfaces · Copyright © 2004, Oracle. All rights reserved. Example PL/SQL Block DECLARE qty_on_hand NUMBER(5); BEGIN SELECT quantity INTO qty_on_hand FROM inventory

Copyright © 2004, Oracle. All rights reserved.

Overview of Transactions

Transaction 1 Transaction 2COMMIT;

Page 16: Database Interfaces · Copyright © 2004, Oracle. All rights reserved. Example PL/SQL Block DECLARE qty_on_hand NUMBER(5); BEGIN SELECT quantity INTO qty_on_hand FROM inventory

Copyright © 2004, Oracle. All rights reserved.

Transaction Control Statements

SQL> SELECT * FROM local_temp;no rows selected

SQL> INSERT INTO local_temp VALUES2 (SYSDATE, 76, 58);

1 row created.

SQL> SELECT * from local_temp;TEMP_DATE HI_TEMP LO_TEMP--------- ---------- ----------27-OCT-03 76 58

SQL> ROLLBACK;Rollback complete.

SQL> SELECT * FROM local_temp;no rows selected

Page 17: Database Interfaces · Copyright © 2004, Oracle. All rights reserved. Example PL/SQL Block DECLARE qty_on_hand NUMBER(5); BEGIN SELECT quantity INTO qty_on_hand FROM inventory

Copyright © 2004, Oracle. All rights reserved.

Locking Data

Oracle Database 10g automatically locks data so that only one user can make changes at a time.

Page 18: Database Interfaces · Copyright © 2004, Oracle. All rights reserved. Example PL/SQL Block DECLARE qty_on_hand NUMBER(5); BEGIN SELECT quantity INTO qty_on_hand FROM inventory

Copyright © 2004, Oracle. All rights reserved.

Other Statement Categories

• Session control statements: Manage the properties of a user session

• System control statement: Manages the properties of an Oracle instance

• Embedded SQL statements: SQL statements within a procedural language program

Page 19: Database Interfaces · Copyright © 2004, Oracle. All rights reserved. Example PL/SQL Block DECLARE qty_on_hand NUMBER(5); BEGIN SELECT quantity INTO qty_on_hand FROM inventory

Copyright © 2004, Oracle. All rights reserved.

What Is PL/SQL?

PL/SQL is a block-structured language, which extends SQL with:• Declarations:

– Variables– Constants– Cursors

• Control structures:– Conditional control– Iterative control– Sequential control

• Error handling

Page 20: Database Interfaces · Copyright © 2004, Oracle. All rights reserved. Example PL/SQL Block DECLARE qty_on_hand NUMBER(5); BEGIN SELECT quantity INTO qty_on_hand FROM inventory

Copyright © 2004, Oracle. All rights reserved.

Example PL/SQL Block

DECLAREqty_on_hand NUMBER(5);

BEGINSELECT quantity INTO qty_on_hand FROMinventory

WHERE product = 'TENNIS RACKET'FOR UPDATE OF quantity;IF qty_on_hand > 0 THEN -- check quantity

UPDATE inventory SET quantity = quantity - 1WHERE product = 'TENNIS RACKET';INSERT INTO purchase_recordVALUES ('Tennis racket purchased', SYSDATE);

ELSEINSERT INTO purchase_recordVALUES ('Out of tennis rackets', SYSDATE);

END IF;COMMIT;

END;

Page 21: Database Interfaces · Copyright © 2004, Oracle. All rights reserved. Example PL/SQL Block DECLARE qty_on_hand NUMBER(5); BEGIN SELECT quantity INTO qty_on_hand FROM inventory

Copyright © 2004, Oracle. All rights reserved.

Uses of PL/SQL

Blocks of PL/SQL are used in:• Anonymous blocks• Functions• Procedures• Packages• Triggers• Object types

Page 22: Database Interfaces · Copyright © 2004, Oracle. All rights reserved. Example PL/SQL Block DECLARE qty_on_hand NUMBER(5); BEGIN SELECT quantity INTO qty_on_hand FROM inventory

Copyright © 2004, Oracle. All rights reserved.

What Is Java?

Java is an industry-standard, object-oriented programming language. It includes the following concepts:• A Java Virtual Machine (JVM), which provides

platform independence• Automated storage management techniques• Language syntax that borrows from C and

enforces strong typing

Page 23: Database Interfaces · Copyright © 2004, Oracle. All rights reserved. Example PL/SQL Block DECLARE qty_on_hand NUMBER(5); BEGIN SELECT quantity INTO qty_on_hand FROM inventory

Copyright © 2004, Oracle. All rights reserved.

Oracle and Java

A PL/SQL function:

CallableStatement cstmt = conn.prepareCall("{? = CALL balance(?)}");cstmt.registerOutParameter(1, Types.FLOAT);cstmt.setInt(2, acctNo);cstmt.executeUpdate();

float acctBal = cstmt.getFloat(1);

FUNCTION balance (acct_id NUMBER) RETURN NUMBER ISacct_bal NUMBER;BEGINSELECT bal INTO acct_bal FROM accts

WHERE acct_no = acct_id;RETURN acct_bal;

END;

Calling the function with Java:

Page 24: Database Interfaces · Copyright © 2004, Oracle. All rights reserved. Example PL/SQL Block DECLARE qty_on_hand NUMBER(5); BEGIN SELECT quantity INTO qty_on_hand FROM inventory

Copyright © 2004, Oracle. All rights reserved.

What Is OCI?

OCI provides for:• The Oracle Call Interface (OCI) is how all database

features are made accessible to application developers.

• OCI makes scalable and high-performance applications possible.

• Higher-level APIs and tools use OCI indirectly for database access.

Page 25: Database Interfaces · Copyright © 2004, Oracle. All rights reserved. Example PL/SQL Block DECLARE qty_on_hand NUMBER(5); BEGIN SELECT quantity INTO qty_on_hand FROM inventory

Copyright © 2004, Oracle. All rights reserved.

Other APIs

• Java Database Connectivity (JDBC)• Pro*C/C++• Pro*COBOL• Oracle C++ Interface (OCCI)• Open Database Connectivity (ODBC)• Oracle Data Provider for .NET (ODP.NET)• Oracle Objects for OLE (OO4O)

Page 26: Database Interfaces · Copyright © 2004, Oracle. All rights reserved. Example PL/SQL Block DECLARE qty_on_hand NUMBER(5); BEGIN SELECT quantity INTO qty_on_hand FROM inventory

Copyright © 2004, Oracle. All rights reserved.

Summary

In this lesson, you should have learned how to: • Use SQL*Plus and iSQL*Plus to access Oracle

Database 10g• Describe the logical structure of tables• Use SQL to query, manipulate, and define data• Identify common database interfaces

Page 27: Database Interfaces · Copyright © 2004, Oracle. All rights reserved. Example PL/SQL Block DECLARE qty_on_hand NUMBER(5); BEGIN SELECT quantity INTO qty_on_hand FROM inventory

Copyright © 2004, Oracle. All rights reserved.

Practice 4: Using SQL

This practice covers using iSQL*Plus to:• Describe tables• Select from tables• Update a table• Delete from a table• Undo changes