Oracle® Database 2 Day Developer's Guide, 11g Release 1 (11.1) Part Number B28843-01 |
|
|
View PDF |
This chapter introduces you to application development with Oracle Database.
This chapter contains the following sections:
You are an Oracle Database developer, someone whose has the responsibility of creating or maintaining the database components of an application that uses the Oracle technology stack. The discussion in this and following sections assumes that you, or someone else in your organization, must know how to architect multiuser applications (two tier or multitier) and understands the advantages of using a relational database for the data persistence tier.
As a database developer, you need to know how to implement the data model that the application requires, how to implement the rules for data integrity, and how to implement the specified functions for accessing and manipulating the application data.
You know already that you can access an Oracle Database only through a client program, and that the SQL language is that client program's interface to the Oracle Database. You will learn how to access the Oracle Database by using two clients that are packaged with the Oracle Database and designed for developers: SQL Developer and SQL*Plus. Both allow you to issue the SQL statements you need in order to create and test your application's database component without doing any client programming. Programming such clients is outside the scope of this discussion.
To briefly review widely accepted best practices in software engineering, you must define APIs that model business functions, and hide the implementation. Oracle Database supports this practice by letting you specify the APIs as PL/SQL subprograms. The implementation is tables, indexes, constraints, triggers, and the various SQL statements that modify and fetch table rows. By embedding these SQL statements in PL/SQL subprograms, and by using Oracle's schema and privilege mechanisms, you can securely hide the implementation from the client programs. Many of Oracle's major customers follow this practice strictly: client programs are allowed to access the database only by calling PL/SQL subprograms. Some customers relax this rule by allowing the client to issue raw SQL SELECT
statements, but requiring it to call PL/SQL subprograms for all business functions that make changes to the database.
This general discussion sets the charter for your job as an Oracle Database developer:
You need to know about the various types of objects that you can create in the database, as described in "Exploring Database Objects".
You need to know the SQL that is used to manage these objects: CREATE
, ALTER
, TRUNCATE
, and DROP
. This is known as data definition language (DDL), and is described in "Creating and Using Database Objects".
You need to know the SQL language that is used to maintain application data: INSERT
, UPDATE
, DELETE
, and MERGE
. This is known as data manipulation language (DML), and is described in "Querying and Manipulating Data".
You need to know the SQL language for querying data: SELECT
statement and its various clauses, as described in "Retrieving Data with Queries".
You need to know about transactions, and the SQL language for controlling them: COMMIT
, SAVEPOINT
, and ROLLBACK
, as described in "Controlling Transactions".
You need to know how to write PL/SQL subprograms and procedural code that use DDL, DML, transaction control, and queries, as described in "Developing and Using Stored Procedures" and "Using Triggers".
You need to know how to manage your deliverables and how to instantiate your application in several different databases for the purposes of development itself, unit testing, integration testing, end-user acceptance testing, education, and ultimately for deploying your application in a production environment. This information is in "Deploying a Database Application".
See Also:
Oracle Database Concepts for information about application architecture
This section introduces Oracle Database schemas.
See Also:
Oracle Database groups related types of information into logical structures that are called schemas. When you connect to the database by providing your user name and password, you name the schema and indicate that you are its owner. Schemas contain tables, which are the basic units of data storage in the database. Using a table, you can query for information, update it, insert additional data, and delete. Each table contains rows that represent the individual data records. The table rows are composed of columns that represent the various fields of the record.
In addition to tables, schemas contain many other objects. Indexes are optional structures that can improve the performance of data retrieval from tables. Indexes are created on one or more columns of a table, and are automatically maintained in Oracle Database. See "Creating and Using Tables".
Depending on your business needs, you can create a view that combines information from several different tables into a single presentation. Such views can rely on information from other views as well as tables. See "Using Views".
In an application where all records of the table must be distinct, a sequence can generate a serial list of unique integer numbers for a numeric column that represents the ID of each record. See "Using Sequences".
A synonym is an alias for any table, view, sequence, procedure, and so on. Synonyms are often used for security and convenience, such as masking the ownership of an object or simplifying SQL statements. See "Using Synonyms".
Schema-level procedures and functions, and also packages, are collectively known as stored procedures. Stored procedures are blocks of code that are actually stored in the database. They are callable from client applications that access a relational database system. See "Developing and Using Stored Procedures".
Triggers are procedural code that is automatically executed by the database when specified events occur in a particular table or view. Triggers can restrict access to specific data, perform logging, or audit data. See "Using Triggers".
See Also:
Oracle Database Concepts for a comprehensive introduction to all schema objects
The hr
schema is one of the sample schemas that can be installed as part of Oracle Database. The hr
sample schema contains information about employees, their departments and locations, their work histories, and other related information. Like all schemas, the hr
schema has tables, views, indexes, procedures, functions, and all other possible attributes of an Oracle Database schema.
You will be using and extending the hr
schema to learn how to develop applications with Oracle Database.
See Also:
Oracle Database Sample Schemas for an in-depth description of the hr
sample schema
In this section, you will learn about two programming languages for direct data access (SQL and PL/SQL), two development tools (SQL Developer and SQL*Plus), a sample data set (hr
schema), and how to connect to an instance of Oracle Database.
There are two broad families of computer languages: declarative language that describes what should be done, and imperative language that describes how things should be done. You are probably already familiar with the Structured Query Language, or SQL, the database-independent language for defining database objects and operations. SQL is a set-based, high-level declarative computer language; it describes a problem by stating criteria for the desired data. Using SQL statements, you can query tables to display data, create and modify objects, and perform a large variety of administrative tasks. When you issue a SQL command, the SQL language compiler automatically generates a procedure to access the database and perform the desired task.
In contrast, imperative computer languages, such as C, C++, and Java, describe how to solve the problem by finding the necessary data; they describe computation as statements that change a program state and can therefore solve a much broader set of problems.
Procedural Language SQL, or PL/SQL, is a native Oracle language extension to SQL. It bridges the gap between declarative and imperative program control by adding procedural elements, such as conditional control and iterative flow constructs. Like SQL, PL/SQL has a built-in treatment of the relational database domain. PL/SQL enables you to declare constants and variables, define procedures and functions, use collections and object types, trap runtime errors, and create functions, packages, procedures and triggers that can be stored on the database for reuse by applications that are authored in any of the Oracle programmatic interfaces.
For more information about PL/SQL, see the PL/SQL Oracle Technology Network site at
SQL Developer is a graphical user interface for accessing your instance of Oracle Database. SQL Developer supports development in both the SQL and PL/SQL languages. It is available in the default installation of Oracle Database. You will use SQL Developer, both through its navigation hierarchy and through the SQL worksheet.
Before you run SQL Developer, ensure that you have Java 1.5.0 installed. At a command prompt, enter the following command:
java -version
Output similar to the following should appear:
java version "1.5.0_06" Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_06-b05) Java HotSpot(TM) Client VM (build 1.5.0_06-b05, mixed mode, sharing)
To start SQL Developer:
In Linux:
Click the Application menu (on Gnome) or the K menu (on KDE).
Select Oracle - ORACLE_HOME, then Application Development, then SQL Developer.
In Windows:
From the Start menu, select All Programs.
Select Oracle - ORACLE_HOME, then Application Development, then SQL Developer.
When prompted, enter the full path to the Java executable.
For example: C:\jdk1.5.0\bin\java.exe
You only need to specify this path the first time you start SQL Developer.
After the splash screen appears, SQL Developer starts.
For more information about SQL Developer, see the SQL Developer Oracle Technology Network site at
http://www.oracle.com/technology/products/database/sql_developer/index.html
SQL*Plus is installed together with Oracle Database. SQL*Plus has a command-line interface for accessing Oracle Database. You can also access SQL*Plus within SQL Developer.
To use SQL*Plus on Windows systems:
Click the Start icon at the bottom left corner of your screen, and select Run.
In the Run window, in the text prompt, enter cmd
. Click OK.
In the cmd.exe
window, at the c:>\
prompt, enter sqlplus
and press the Enter button of your keyboard.
SQL*Plus starts and prompts you to authenticate your connection to the database.
Your screen looks something like this:
C:\>sqlplus SQL*Plus: Release 11.1.0.1.0 - Production on Tue April 3 10:10:11 2007 Copyright (c) 1982, 2007, Oracle. All rights reserved. Enter user-name:
Enter your user name and press Enter.
Your screen looks something like this:
Enter password:
Enter your password and press Enter. Note that entering your user name and password in this manner is secure because your password is not visible on the screen.
The system connects you to a database instance, and shows a SQL prompt.
Your screen looks something like this:
Connected to: Oracle Database 11g Enterprise Edition Release 11.1.0.1.0 - Production With the Partitioning, OLAP and Data Mining options
You can now start using the SQL command prompt.
To close the SQL*Plus session, at the SQL prompt enter the exit
command. Note that you are not shutting down the Oracle Database instance.
SQL> exit
Your screen looks something like this:
Disconnected from Oracle Database 11g Enterprise Edition Release 11.1.0.1.0 With the Partitioning, OLAP and Data Mining options
Remember that in Oracle Database, the user and the name of the schema to which the user connects are the same. This section shows how to create a connection to the hr
schema, one of the sample schemas that ship with Oracle Database. To begin, you must unlock the hr
account.
This section contains the following topics:
By default, when the hr
schema is installed, it is locked and its password is expired. Before you can connect to Oracle Database using the hr
schema, a user with administrator privileges needs to unlock the hr
account and reset its password.
The following steps show how to unlock the hr
account and change its password.
To unlock the hr account and change its password:
Start a new SQL* Plus session, and login as a user with administrative privileges, such as user SYSTEM
. See "Introducing SQL*Plus".
At the SQL prompt, enter the following statement:
Remember to choose a password that is secure. See Oracle Database Security Guide for guidelines on choosing passwords.
SQL> ALTER USER hr ACCOUNT UNLOCK IDENTIFIED BY password;
The system confirms that the hr
account is unlocked and its password changed:
User altered
When the hr
account is unlocked, you can create a new hr connection using the new password that you set in "Unlocking a User Account".
To create an HR Connection in SQL*Plus:
Close the current connection to Oracle database. See step 6 of "Introducing SQL*Plus"3.
Start SQL*Plus. In the cmd.exe
window, at the c:>\
prompt, enter sqlplus
and press the Enter button of your keyboard.
At the SQL prompt, enter hr
, and then the password.
The system connects you to a database instance through the hr
schema.
You can close both the connection and the command window.
See Also:
When the hr
account is unlocked, you can use it to access the hr
schema inside the Oracle Database. In this section, you will be working with Oracle SQL Developer.
To create an HR connection in SQL Developer:
Start SQL Developer.
In the Connections pane, click the New Connection icon.
In the upper part of the New/Select Database Connection window, enter this information:
For Connection Name, enter hr_conn
.
For Username, enter hr
.
For Password, enter the password that the system administrator created after unlocking the hr
account. Note that the password text is masked.
Do not check the Save Password option.
Under the Oracle tab of the New/Select Database Connection window, enter this information:
For Role, select Default.
For Connection Type, select Basic.
For Hostname, enter localhost
.
For Port, enter 1521
.
For SID, enter orcl
.
At the bottom of the New/Select Database Connection window, click Test.
The connection is tested. At the bottom of the New/Select Database Connection window, Status is changed to Success.
At the bottom of the New/Select Database Connection window, click Connect.
The Oracle SQL Developer window appears, with a new hr_conn
connection.
You have successfully established a connection to the hr
schema.
This section introduces other development environments and languages that you may choose for developing your own applications.
Oracle Data Provider for .NET, Oracle Database Extensions for .NET and Oracle Developer Tools for Visual Studio .NET
Oracle Data Provider for .NET (ODP.NET), is a .NET data provider that uses and extends the Microsoft .NET Framework Class Library. ODP.NET uses the .NET Framework to expose provider-specific features and data types, so its use of native Oracle Database APIs bring the features of Oracle Database to .NET applications.
Oracle Database Extensions for .NET provide a Microsoft Common Language Runtime (CLR) host for Oracle Database, data access through ODP.NET classes, and the Oracle Deployment Wizard for Visual Studio .NET. Because CLR runs as an external process on Oracle Database server, this integration enables you to run .NET stored procedures and functions on Oracle Database in Microsoft Windows XP, 2000, and 2003. These stored procedures and functions can be authored in any .NET language, such as C# and VB.NET, and then deployed in Oracle Database using the Oracle Deployment Wizard for .NET, just like PL/SQL or Java stored procedures.
Oracle Developer Tools provide a graphical user interface to access Oracle Database functionality through Visual Studio .NET. Oracle Developer Tools include Oracle Explorer for browsing the database schema, wizards and designers for creating and altering schema objects, the ability to automatically generate code by dragging schema objects onto the .NET design form, and a PL/SQL editor with integrated context-sensitive help. Additionally, the Oracle Data Window enables you to perform routine database tasks and test stored procedures in the Visual Studio environment, while the SQL Query Window executes SQL statements and scripts.
For an introduction to .NET application development with Oracle Database, see Oracle Database 2 Day + .NET Developer's Guide.
Further Oracle Database .NET documentation includes Oracle Data Provider for .NET Developer's Guide and Oracle Database Extensions for .NET Developer's Guide.
For complete information about Oracle Database .NET APIs, ODP.NET, Oracle Developer Tools, downloads, tutorials, and related information, see the .NET Oracle Technology Network site at
http://www.oracle.com/technology/tech/dotnet/
PHP
The Hypertext Preprocessor, PHP, is a powerful interpreted server-side scripting language for quick Web application development. PHP is an open source language that is distributed under a BSD-style license. PHP is designed for embedding Oracle Database access requests directly into HTML pages.
For an introduction to PHP application development with Oracle Database, see the Oracle Database 2 Day + PHP Developer's Guide.
For complete information about Oracle Database PHP APIs and related information, see the PHP Oracle Technology Network site at
http://www.oracle.com/technology/tech/php/
Oracle Application Express
Oracle Application Express, APEX, is an application development and deployment tool that enables you to quickly create secure and scalable Web applications even if you have limited previous programming experience. The embedded Application Builder tool assembles an HTML interface or a complete application that uses database objects, such as tables or stored procedures, into a collection of pages that are linked together through tabs, buttons, or hypertext links. See Oracle Database 2 Day + Application Express Developer's Guide for complete information on APEX.
For complete information about APEX and related information, see the APEX Oracle Technology Network site at
http://www.oracle.com/technology/products/database/application_express/
Oracle Call Interface and Oracle C++ Call Interface
Oracle Call Interface (OCI) is the native C language API for accessing Oracle Database directly from C applications. See Oracle Call Interface Programmer's Guide for complete information on OCI.
Oracle C++ Call Interface (OCCI) is the native C++ language API for accessing Oracle Database directly from C++ applications. Very similar to the OCI, OCCI supports both relational and object-oriented programming paradigms. See Oracle C++ Call Interface Programmer's Guide for complete information on OCCI.
The OCI and OCCI Software Development Kits are also installed as part of the Oracle Instant Client, which enables you to run applications without installing the standard Oracle client or having an ORACLE_HOME
. Your applications will work without modification, while using significantly less disk space. Oracle Instant Client is available from the Instant Client Oracle Technology Network site at
http://www.oracle.com/technology/tech/oci/instantclient/
For complete information about Oracle Database OCI and related information, see the OCI Oracle Technology Network site at
http://www.oracle.com/technology/tech/oci/
For complete information about Oracle Database OCCI and related information, see the OCCI Oracle Technology Network site at
http://www.oracle.com/technology/tech/oci/occi/
Oracle Java Database Connectivity
Oracle Java Database Connectivity (JDBC) is an API that enables Java to send SQL statements to an object-relational database, such as Oracle Database. Oracle Database JDBC provides complete support for the JDBC 3.0 and JDBC RowSet (JSR-114) standards, advanced connection caching for both XA and non-XA connections, exposure of SQL and PL/SQL data types to Java, and fast SQL data access.
Like OCI and OCCI, JDBC is part of the Oracle Instant Client installation, which is available from the Instant Client Oracle Technology Network site at
http://www.oracle.com/technology/tech/oci/instantclient/
For more information about JDBC APIs, see the Sun Developer Network site at
http://java.sun.com/javase/technologies/database/
For complete information about Oracle Database JDBC APIs, drivers, support and de-support notices, and similar information, see the Oracle Technology Network site at
http://www.oracle.com/technology/tech/java/sqlj_jdbc/
For an introduction on how to use Java to access and modify data in Oracle Database, see Oracle Database 2 Day + Java Developer's Guide.
Open Database Connectivity
Open Database Connectivity (ODBC) is a set of database access APIs that connect to the database, prepare, and then run SQL statements on Oracle Database. An application that uses an ODBC driver can access non-uniform data sources, such as spreadsheets and comma-delimited files.
The Oracle ODBC driver conforms to ODBC 3.51 specifications. It supports all core APIs and a subset of Level1 and Level 2 functions. Microsoft supplies the Driver manager component for the Windows platform. The Oracle Database driver for unix platforms is available on the ODBC Oracle Technology Network site at
http://www.oracle.com/technology/tech/windows/odbc/
For information about unixODBC standards and the latest Driver manager, see the unixODBC site at
For information about using the Oracle ODBC driver with Windows, see Oracle Services for Microsoft Transaction Server Developer's Guide .
For information about how to use the Oracle ODBC driver on Linux, see Oracle Database Administrator's Reference for Linux and UNIX.
Like OCI, OCCI, and JDBC, ODBC is part of the Oracle Instant Client installation, which is available from the Instant Client Oracle Technology Network site at