1. Trang chủ
  2. » Công Nghệ Thông Tin

Prentice hall programming oracle triggers and stored procedures 3rd edition dec 2003 ISBN 0130850330

573 88 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 573
Dung lượng 2,69 MB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Edgar Frank Codd,Ph.D., developed the theory for relational database systems.Shortly after this development, Codd published "A RelationalModel of Data for Large Shared Data Banks" in the

Trang 1

Chapter One An Introduction to Relational Database Tables

Trang 3

In 1969, Apollo astronauts Neil Armstrong and Buzz Aldrin

walked on the moon That same year Dr Edgar Frank Codd,Ph.D., developed the theory for relational database systems.Shortly after this development, Codd published "A RelationalModel of Data for Large Shared Data Banks" in the Association

of Computing Machinery (ACM) journal, Communications of the ACM Codd's model became the foundation for a prototype; a

relational database project within IBM called System/R Studieswere performed at the IBM San Jose Research Center (now theAlmaden Research Center) and by 1974 IBM had a running

prototype relational database system This system was based

on multitable queries and multiuser access The method for

accessing data in System/R was called Structured English QueryLanguage (SEQUEL)

Following its research and development, IBM released System/R

as a prototype It was used by the MIT Sloan School of

Management and commercial organizations within the

manufacturing and inventory sectors The project demonstratedthat Codd's theories could be applied and implemented in thereal world, but ended in 1979

The success of System/R did not receive quite the same

attention the world gave the Apollo astronauts on July 20,

1969; but, System/R's success created a new paradigm in theworld of information technologya paradigm that dominates theinformation age to this day

Codd believed that programmers should be able to control theexact data used to join and construct queries from multiple

independent tables This was not the case with existing

hierarchical and network databases For these other

technologies, an application programmer would code an

algorithm to navigate through the database by accessing data

Trang 4

database modifications to these paths and new navigations

paths needed to be defined The software development processwas slow and impaired with frequent meetings on data access.Codd's model had a broad impact on the software developmentprocess

System/R was implemented by a group of highly talented andeducated software engineers They demonstrated that largeinformation systems could be built on Codd's theory of multi-independent tables and a structured query language This wasthe birth of SEQUEL Throughout the System/R project,

engineers openly published their work in technical journals and

at international conferences The policy of open publication isconsidered to have been a factor in the success of

System/Rleading to the creation of many relational databasessuch as DB2 and Oracle

IBM passed control of the System/R structured query language

to the American National Standards Institute (ANSI), a privatenonprofit organization that administers U.S voluntary

technologies Because of trademark laws, the language wasrenamed from SEQUEL to SQL for "Structured Query

Language." Today SQL is an ANSI standard and an InternationalStandards Organization (ISO) standard

In 1977, a group of engineers who had been following the

System/R project, primarily through publications, recognized itspotential and formed a company This company, Software

Development Laboratories, was later named Relational

Software This new company developed and marketed the firstcommercial version of a relational database management

system, which was based on the ANSI SQL Standard They

named their product Oracle

When project System/R ended in 1979, many information

systems relied on either hierarchical or network database

Trang 5

information systems are relational; however, hierarchical andnetwork database products have still maintained a market

presence

IMS/DB is the flagship hierarchical database of IBM RockwellInternational and IBM developed IMS in 1969 to manage datafor the National Aeronautics and Space Administration's

(NASA's) Apollo program IMS/DB continues today with a solidinternational presence It is industry-wide and serves an

estimated 200 million end users The IMS/DB database servedthe Apollo program including Apollo 11, which sent Armstrong,Aldrin, and pilot Mike Collins to the moon

Computer Associates' network database product is CA-IDMS, awidely used database serving 2,000 sites around the world Theproduct has changed in name and ownership but continues tosupport large corporate data systems that run on IBM OS/390platforms

IDMS has an interesting history John J Cullinane founded

Cullinet Software, Inc in 1968 He bought the rights to IDMS, aCODASYL database, from B.F Goodrich CODASYL, an acronymfor Conference on Data System Languages, was established in

1960 by the Department of Defense for the purpose of

standardizing languages and software applications Cullinet wasthe first software company to be listed on the New York StockExchange Computer Associates purchased Cullinet in

September of 1989 and renamed the product CA-IDMS

Although CA-IDMS is based on database network architecture,the product includes an ANSI-compliant SQL Option This optionallows applications to access IDMS data using SQL A set ofcomprehensive tools is available that includes agents for

enterprise monitoring, parallel transaction processing using IBMParallel Sysplex Cluster Technology, JDBC support for Java, andmany other eBusiness technologies The majority of IDMS

Trang 6

Oracle, predominately known for the Relational Enterprise

Server, owns two additional database products: (a) a networkdatabase product called DBMS, which is a CODASYL database,and (b) a relational database product called Rdb Both Rdb andDBMS were purchased from Digital Equipment Corporation

(DEC) prior to the acquisition of DEC by Compaq

Oracle acquired DBMS when it purchased the Rdb product

family from DEC Ken Olsen, an MIT graduate, his brother Stan,and Harlan Anderson founded DEC In addition to hardwarearchitectures such as the PDP-11, VAX, and Alpha, DEC

developed Rdb, a relational database, and also DBMS, a

network database The network database was renamed fromVAX/DBMS to DEC DBMS when version V5.0 was released onthe Alpha AXP platform

Oracle's CODASYL DBMS is a multi-user network database that,like Rdb, is optimized for the Compaq OpenVMS operating

WorkStream has a long history of running on VAX/DBMS andtoday runs primarily on VAX and Alpha servers using OracleCODASYL DBMS

Rdb7 was Oracle's first release of Rdb that was conceived andengineered as an Oracle product Oracle's Rdb7 is an enterpriserelational database optimized for digital platforms; that is, theHewlett-Packard Corporation's OpenVMS operating system andCompaq Digital UNIX When Compaq acquired DEC it replacedthe DEC version of UNIX, called Ultrix, with the product nameDIGITAL UNIXan operating system based on a 64-bit

architecture that runs on Alpha AXP platforms

Trang 7

relational technology; however, hierarchical and network

databases continue to meet the needs of enterprise networkcomputing environments IBM IMS/DB is a dominant

hierarchical database that can service high-transaction ratesand has a large install base Two network databasesCA-IDMS,licensed by Computer Associates, and DBMS, licensed by

Oracle,play critical roles in support of high-transaction

enterprise-wide systems In addition to the Oracle EnterpriseServer, Oracle Corporation also owns Rdb7, a relational

database, and DBMS, a network databaseboth products are

optimized for Compaq platforms, including Alpha AXP Today theOracle Database Server addresses the needs of small and largebusinesses that require information processing for online

transaction processing (OLTP) systems, decision support

systems (DSS), plus the eBusiness solutions These types ofapplications can operate on small, stand-alone servers, or ondistributed, high-availability architectures

Trang 9

statement

There is always an advantage to working with a standard Itmeans that your knowledge is transferable An understanding ofSQL means that your knowledge of relational database

technology can transfer between products such as Oracle andSQL Server Even within a standard there are differences SQL is

no exception

SQL differs across vendors for two reasons First, there are

"implementation defined" features in the standard; that is, forsome SQL features, the specific nature of how a feature is

implemented is "implementation defined"the standard explicitlystates this with the language "implementation defined." Thismeans the vendor has discretion in how the feature is

implemented One example is how the vendor implements thecatalog or data dictionary The standard cannot impose how avendor stores database metadata We'll see later in this chapterhow Oracle's data dictionary is a set of relational tables fromwhich we can select tremendous amounts of information, such

as information describing the tables we create

A second reason why there are differences across databases isdue to the fact that vendors do enhance their specific SQL

engines with additional capabilities that they feel benefit the

Trang 10

because DECODE is not part of the ANSI standard Another

example is the database column type CURRENCY that you find

in SQL Servernot all database products provide a CURRENCYtype These differences are minor compared to the many

consistencies across SQL products that are ensured by the ANSIstandard

In summary, you will find variations in some SQL statementsacross database enginesthis is really not a big deal For

example, if you build your expertise in SQL and you build thisknowledge in Oracle, then your knowledge will include an

understanding of inner and outer joins If you are ever

confronted with the syntax of outer joins from a SQL Server orDB2 application, where this syntax varies, then you can easilymake the knowledge transition The key issue is to recognizethat there are differences, because you are likely to becomeexposed to these differences at some point in your career

relational database refers to one based on the SQL standard

that is capable of parsing and executing SQL statements Wecan be very general in how we use the term database, but theterm relational database does convey a database that is based

on the SQL standardsomething very specific

In contrast to relational databases, there are nonrelational

Trang 11

by Oracle and is not a relational databaseit is a network

database Access to DBMS is through a language similar to SQL,but it is not SQL per ANSI standard Some databases, such asIDMS, are not relational databases, yet they provide a front endthat allows an application to access the data using SQL Having

a SQL front-end capability does not make IDMS a relational

database A relational database is relational when it conforms tothe minimum requirements of the ANSI SQL standard

Oracle is a fully SQL-compliant database An Oracle databaseconsists of many files that are preferably distributed across

several disk partitionsdistributing files minimizes IO contention

1.2.3 SQL Implementation

Given that Oracle is a relational database and we access Oracledata using SQL, how does one create and manipulate that data?You could write a Visual Basic, C, Perl, or Java program Youcould write just about any program in any language you wantand embed SQL statements in your program These statementswill insert data, change it, and delete it Our application

programs can potentially execute any valid SQL statement

against our relational database

SQL, originally called SEQUEL, designed by IBM, and based onCodd's relational theory, provided data access methods for theSystem/R project The primary purpose of SQL today is that weuse it in a manner similar to which it was used by System/R;that is, to be embedded within a programming language and toprovide data access methods against database tables for a

production system One core difference, from a day-to-day

coding perspective, between programs of today and those ofSystem/R is that we are more likely to use a more current

programming language, such as Java or C# Secondly,

programs we code today utilize enhancements to the SQL

Trang 12

standard also addresses another method referred to as DirectSQL This is where we do the simple and quick stuff

The SQL Standard specifies three approaches to SQL:

embedded SQL, modular SQL, and Direct SQL Embedded andmodular SQL are principal techniques for applications

development using a compiler languagethey are great for

applications development but impractical for simple tasks andfor learning the SQL language

If we did not have an implementation of Direct SQL and wanted

to see all the rows of a table, we would have to write an

application program, embed a SQL cursor, fetch each row intolocal variables and print those variables out to some defaultdevice A tool that implements Direct SQL allows us to displaythe same data in about 5 secondsmostly limited to how fast wecan type or drag-and-drop Rather than a compiled program,Direct SQL allows us to type a SQL SELECT statement wherebyall rows are flushed to our screen for viewing

Trang 13

Direct invocation of SQL is a mechanism for executing

direct SQL-statements, known as <direct SQL

statement>s In direct invocation of SQL, the method of

invoking <direct SQL statement>s, the method of raisingconditions that result from the execution of <direct SQL

statement>s, the method of accessing the diagnostics

information that results from the execution of <direct SQLstatement>s, and the method of returning the results areimplementation-defined

There exists a wide variety of SQL tools that implement directSQL These tools allow us to type basic SQL statements andexecute them without the additional complexity of a compiler orscripting language Some are GUI and some provide a basiccommand line interface Examples of such tools are SQL*Plus,which is bundled with the Oracle database software; SQL

Navigator®, which is licensed by Quest Software, SQL

Worksheet which is bundled with the Oracle Enterprise Manager(OEM) software; and TOAD, which is also a Quest Software

product There are many other tools as well

There is a conceptual difference between accessing Oracle, orany relational database, through a compiler language such asJava and using a direct query tool like TOAD or SQL*Plus Alltools that implement Direct SQL operate within an interactive

Trang 16

particular student ID), is returned to the SQL*Plus client where

it is flushed to your screen

The sending and receiving of data is Oracle Net8, which is

Oracle communication software and runs over TCP/IP, or othernetwork protocols SQL*Plus is written to utilize Net8, whichutilizes TCP/IP over the network

SELECT, INSERT, UPDATE, and DELETE are the most commonSQL statementsthey are the core means of manipulating data

In addition to these core data manipulation statements, the SQLstandard includes the specification for statements like: CREATEDATABASE, ALTER DATABASE, CREATE TABLE, CREATE VIEW,CREATE TRIGGER, and many other SQL commands Any validSQL statement can be executed from SQL*Plus, including theALTER DATABASE statement So, all SQL statements, not justthe common INSERT, UPDATE, DELETE, SELECT can be

executed from SQL*Plus

There are many advantages to learning SQL*Plus A few aresummarized here

You will find SQL*Plus in the Oracle bin directory of everyOracle install If you install the database on an enterpriseserver, you will find SQL*Plus in the bin directory If youinstall just the Oracle developer software on the desktop,you also have SQL*Plus This executable program is always

Trang 17

Because SQL*Plus has been available from Oracle through

so many versions, the program has an extremely wide userbase It is popular for several reasons, one of which is thefact that it has been around for so many years

SQL*Plus is a simple command line interface program It'snot a GUI interface; you do not have multiple scrollablewindows that show the SQL statement in one window andthe query result in another window Although GUI featureshave their advantages, the simplicity of SQL*Plus becomesadvantageous when you need to build scripts You can build

a library of SQL*Plus scripts, each an ASCII text file, andthose scripts will run on Oracle in UNIX or on Oracle in

Windows

SQL*Plus has all the features of a powerful scripting

language It supports argument passing, command files,and nested command files You can host out to other

languages (e.g., embed a Windows script host file or Kornshell script within a SQL*Plus script) SQL*Plus scripts can

be incorporated into other scripting programs (i.e.,

SQL*Plus scripts can be embedded in Korn shell scripts;illustrated in Chapter 2) These features make the languagehighly useful for tool building

In summary, although SQL*Plus is an excellent tool to firstexperience SQL and Oracle, it is also widely used as a

development and administration tool People who use

SQL*Plus initially use it to introduce themselves to Oracle,but years later they're still using it for applications

development and database administration

Trang 18

Database tables are the most fundamental structure in a

database If you were asked to work on a new Oracle database,you would initially wonder how many tables there are in theapplication It makes a difference whether there are six tables

or 600 tables, at least in terms of how much there is for you tolearn about the application before you can be a productive

developer

The examples in this text are based on a demo student datamodel The model stores data for students, courses, and

professors Chapter 4 graphically illustrates this model includingthe SQL create scripts A key table in this model is the

STUDENTS table

An initial plan for a STUDENTS table requires answering thequestion: "What attributes should be stored for each student?"

diagram In production, the table can be described with the

Trang 19

1.3.1 Data Model View

The data model view of a table identifies the logical attributes ofthe entity being stored in the database (Figure 1-2) This

includes a key attribute, which is the primary key The primarykey is unique for each instance of the entity If the table storesstudents, every student will have a unique primary key Thedata model view is a graphical representation

Figure 1-2 Students Table in a Data Model.

1.3.2 Create Table Script

Once the model is complete and all attributes are defined, theentity must be created in the database SQL is used to create atable This step transitions the logical concept to a physical

Trang 20

to create the STUDENTS table, the SQL*Plus describe commandwould return the following:

SQL> desc students

Name Null? Type

STUDENT_ID NOT NULL VARCHAR2(10) STUDENT_NAME NOT NULL VARCHAR2(30) COLLEGE_MAJOR NOT NULL VARCHAR2(15) STATUS NOT NULL VARCHAR2(15)

- - -1.3.4 Table Data

Trang 23

The SQL language is primarily used to create tables and

manipulate table data; however, the language includes

statements that perform other functions SQL statements areused to create database accounts, size and tune for

performance, and perform administration tasks An Oracledatabase first comes into existence with a SQL statement, theCREATE DATABASE statement The following describes thecategories of SQL statements

1.4.1 Data Definition Language (DDL)

CREATE TABLE is a DDL statement DDL statements define ourdatabase objects and result in updates to the Oracle data

dictionary They create, modify, and delete objects such as atables, views, stored procedures, database triggers, databaselinks, and dozens of other objects in the database

DDL statements can alter existing database objects includingtables Columns and constraints can be added with the ALTERTABLE command Should we choose to add a column for astudent's age we can execute the following SQL statement,which alters the definition of the table in the database datadictionary

ALTER TABLE students ADD (age NUMBER(3));

1.4.2 Data Manipulation Language (DML)

DML manipulates data with the following statements:

Trang 24

consistent read image A user making uncommitted changes tothe database through INSERT, UPDATE, and DELETE statementsdoes not block a user who issues a SELECT during that sametime frame Transactions such as an INSERT that are not

committed to the database do not interfere with concurrentSELECT statements against the same tables A SELECT

statement is guaranteed a consistent image The results

returned to the user from a SELECT statement are guaranteed

to be from the most recent consistent image, which will be therows as of the most recent COMMIT A COMMIT statement

establishes a new checkpoint by which table data is updated to

a consistent point in time

1.4.3 Transaction Control

Transaction control statements allow you to bundle a group ofDML statements under an all-or-nothing domain That is, "allstatements complete successfully," or if one statement fails,then all statements fail as one group Examples of transaction

Trang 25

default display format for columns with a DATE type to include:

day, month, year, hours, minutes, and seconds

ALTER SESSION SET NLS_DATE_FORMAT='dd-mon-yyyy hh24:mi:ss';

Trang 26

System Control statements are largely database administrativecommands The SQL statements are used to open the database,shut down the database, and perform administrative commandssuch as resize datafiles or switch log files The following is asystem control statement used to set the number of Oracle jobqueue processes

ALTER SYSTEM SET job_queue_processes=4 SCOPE=BOTH;

Trang 27

The most common datatypes are VARCHAR2, NUMBER, andDATE These types handle basic column types for characterstrings, numbers, and columns with a time dimension Theexamples in this text, including the data model in Chapter 4,use these three types Oracle provides many other datatypeswith powerful features

Some datatypes are supplemented with built-in functions andpackages Built-in functions are available to convert a DATEbetween time zones Large objects, such as CLOB and BLOBtypes, are supported with a PL/SQL built-in package,

DBMS_LOB which enables the manipulation of 4-gigabyte

objects The following lists the datatypes available for columndefinitions in a table

1.5.1 Character

VARCHAR2(n)

The VARCHAR2 is the general string type used to store strings

of up to 4,000 characters A field of this type is, internally, avariable length string It only uses the minimum necessary

bytesno padding The parameter n is required For the column

STUDENT_NAME, in the STUDENTS table, we can store a name

up to 30 characters

Oracle version 6 and earlier versions used a VARCHAR that was

Trang 28

CHAR(n)

This type is used to store a fixed-length string Inserts willright-pad blanks to the value if the length of the inserted value

is less than the length of the CHAR declarationthis impactsprogrammers writing SQL with WHERE clauses who do notexpect column values to be padded This datatype is rare

NCHAR(n), NVARCHAR2(n)

These types are used only to store Unicode (unified encoding)character data, which is applicable for Oracle applications thatsupport multiple languages

1.5.2 Number

NUMBER, NUMBER(a), NUMBER(a,b)

Trang 29

digit whole number:

a person then you would use the following to store any three-CREATE TABLE TEMP (age NUMBER(3));

Should you be storing stock prices with a maximum of a milliondollars (seven places to the left of the decimal point) and fourplaces to the right of the decimal point, you would use:

CREATE TABLE TEMP (PRICE NUMBER(11,4));

Scale is optional and, if not used, the values are stored as

whole numbers Fractions, when inserted, are rounded to thenearest whole number To specify a scale only, use:

CREATE TABLE TEMP (PRICE NUMBER(*,4));

Trang 31

28-JUL-03 01.25.56.000122 PM

TIMESTAMP can be declared with an optional parameter Theparameter "a" represents the number of digits included in thefractional part of the timestamp

TIMESTAMP WITH TIME ZONE, TIMESTAMP WITH LOCAL TIME ZONE

INTERVAL DAY TO SECOND, INTERVAL YEAR TO MONTH

TIMESTAMPS can incorporate time zones Applications spreadover multiple time zones are likely to use date columns of thistype The INTERVAL type represents a time difference to aparticular significance The following creates a table with

Trang 32

Use a CLOB to store large-character objects The limit is fourgigabytes A VARCHAR2 is limited to 4,000 characters

Character-based columns needing more than four gigabytesshould be CLOBs

Use the BLOB to store four gigabytes of binary data

A student writing sample can be a column in the student table

If writing samples are documents that exceed 4,000 charactersthey are CLOB-type columns The table and column are createdwith the following:

Trang 33

FROM student_writing_samples

WHERE student_id = 'A101';

dbms_lob.open(v_writing_sample, dbms_lob.lob_readonly); the_length := dbms_lob.getlength(v_writing_sample);

Trang 35

||'<COURSE>Math 101</COURSE>'

Trang 36

1.5.6 LONG and RAW Types

LONG

Oracle introduced support for large objects in version 8 Prior tothis release, strings that exceeded the bounds of a VARCHAR2

Trang 37

DBMS_LOB package

RAW, LONG RAW

Similar to the LONG type, the RAW and LONG RAW datatypesserved a useful purpose prior to LOB support with Oracle 8 TheRAW and LONG RAW types were used for manipulating binarydatathe Oracle built-in package, DBMS_LOB, provides advancedsupport for manipulating binary data

1.5.7 ROWID Type

ROWID, UROWID

A ROWID contains composite information that allows Oracle toidentify, form a single ROWID value, where a specific row in atable is located (i.e., the tablespace, file, database block, andspecific location within that block) You can declare a table with

a column of type ROWID, populate that table with ROWID

values, and extract information about the value using the Oraclebuilt-in package DBMS_ROWID The UROWID is a universal

ROWID structure that supports non-Oracle tables

Trang 38

statement includes the host pathnames of one or more hostfiles that are to be created The CREATE TABLESPACE statementcreates the files mentioned in the statement, formats the files,and stores information in the Oracle data dictionary The datadictionary information tracks the fact that a tablespace is made

up of specific files

Once the tablespace is created, the CREATE TABLE statementcan reference the tablespace name in the create statement.From this point on, Oracle will use the files of that tablespacefor row storage Figure 1-3 illustrates this architecture showingthat tables and tablespaces are logical entities whereas the

datafiles are the ultimate physical component

Figure 1-3 Tables in a Tablespace.

Trang 39

To replicate the environment in Figure 1-3 create the

Trang 40

A single application usually has all tables in one tablespace

There are circumstances where multiple tablespaces are used.Multiple tablespaces are driven by a variety of issues includinghighly demanding physical storage requirements and

partitioning The following summarizes some remaining topics

on tablespaces

There is a standard, known as the Optimal Flexible

Architecture (OFA) The OFA standard recommends thatdatabase files fit into a directory structure where the parentdirectory name is the same name as the database name,plus other reasonable considerations The aforementionedexample violates this convention only to simplify the

example

The datafile D:\student_data.dbf did not exist prior to theCREATE TABLESPACE statement This file is created duringthe execution of the CREATE TABLESPACE statement It ispossible to create a tablespace on an existing datafilethisrequires a REUSE clause in the syntax

A tablespace can consist of multiple files For example, ifyou need 20M you can have two 10M files

The datafiles in the CREATE TABLESPACE statement are

formatted by Oracle You'll notice that a CREATE

TABLESPACE statement on a 2G datafile takes relativelylonger that a 2M datafile This is because Oracle formats thedatafile using its own internal block structure

The aforementioned example is simple and may imply astrict architecturesuch as dealing with space when you fill

Ngày đăng: 26/03/2019, 17:11

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN