RelationalDBDesign
RedhatLinuxSys Seomining
prev next prev next
Course navigation
Lesson 2What is a function?
Objective Uses of Oracle function/identify correct syntax.
List the uses of a function and identify correct syntax.
A function is a set of PL/SQL code that is stored with a name inside the database. Like other objects created in the database, a function is created and owned by an Oracle user. Once created, it can be executed the same way that Oracle's built-in functions are executed: by calling it by name within a query or some other SQL or PL/SQL statement. A function usually accepts one or more parameters and always returns a value.
Advantages of functions
A stored function is one that is stored as a named object in the database. One advantage of using a stored function is that the function is executed within the server as opposed to executed within the client. The function can take advantage of higher processing power and of local access to additional data within the server.
Advantage of Functions
Functions can be defined independently within a PL/SQL block without being stored inside the database. These kinds of functions are used in applications and might be executed on the client side. The stored functions we are discussing in this module, however, are much more common--they are stored within the database itself, which makes them reusable and shareable.
Uses of Oracle functions
Functions are commonly used to:
  1. Perform complex calculations and return the results
  2. Accept an unformatted data field and return it in a standard format, such as a Social Security Number
  3. Accept a key field and return summarized data from related tables
Functions can be used in SQL commands just about anywhere you can use an expression. For example, you can use them within the SET clause of an INSERT command, or within the SELECT or WHERE clauses of a query.
Stored function restrictions
There are some restrictions in using and coding stored functions.
The following table shows the capabilities and restrictions of stored functions.

Capability of stored function Restriction of stored function
Can be used within SQL commands (such as queries) and within a PL/SQL block so long as the function returns a standard Oracle datatype (number, date, varchar2, char) Cannot modify data directly, so they cannot include INSERT, UPDATE, or DELETE commands
Can only be used within a PL/SQL block if the function returns a Boolean datatype, or other PL/SQL datatype (other than the standard Oracle datatypes listed above) Cannot be used inside a CHECK constraint

The next lesson explains the proper syntax for creating a stored function.
Course navigation