01.09.2016 Views

Beginning Oracle Database 11g Administration From Novice to Professional

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

38<br />

CHAPTER 2 SQL AND PL/SQL<br />

Introduction <strong>to</strong> PL/SQL<br />

<strong>Oracle</strong> gives us the ability <strong>to</strong> s<strong>to</strong>re programs in the database—and these programs are<br />

often written by using a proprietary <strong>Oracle</strong> language called Procedural Language/SQL, or<br />

PL/SQL. PL/SQL offers the entire suite of structured programming mechanisms such as<br />

condition checking, loops, and subroutines. Of course, just like programs written in other<br />

languages, PL/SQL programs communicate with <strong>Oracle</strong> by using SQL.<br />

Here is an example of a PL/SQL program <strong>to</strong> delete a supplier record and any associated<br />

quote records—first a flow chart in Figure 2-5, and then the actual program itself in<br />

Listing 2-21. 6<br />

PROCEDURE delete_supplier<br />

(suppliername_in IN VARCHAR)<br />

DELETE FROM supplier<br />

WHERE suppliername =<br />

suppliername_in;<br />

Yes<br />

ROWCOUNT > 0 ?<br />

No<br />

No<br />

Error?<br />

Yes<br />

No<br />

SQLCODE = -2292?<br />

Child records were found.<br />

Delete those first.<br />

Yes<br />

Display success<br />

message<br />

Display error<br />

message<br />

Display error<br />

message<br />

DELETE FROM quote<br />

WHERE suppliername =<br />

suppliername_in;<br />

Yes<br />

Error?<br />

No<br />

Now try<br />

deleting the<br />

parent again.<br />

Display error<br />

message<br />

DELETE FROM supplier<br />

WHERE suppliername =<br />

suppliername_in;<br />

Yes<br />

Error?<br />

No<br />

End<br />

Display error<br />

message<br />

Display success<br />

message<br />

Figure 2-5. Flow chart for the procedure<br />

6. If the and tables are related by referential constraints and the <br />

option is included in the definition of the table, <strong>Oracle</strong> will au<strong>to</strong>matically delete a record<br />

when the corresponding record is deleted. For the purposes of this example, we assume that<br />

this option has not been used.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!