Fred’s Blog

November 17, 2006

PL/SQL Best Practices Seminar

Filed under: Databases — fred @ 2:59 pm

Here are some notes from Steven Feuerstein’s Best Practice PL/SQL seminar held at St. John’s University in New York on October 16, 2006. This is not a complete blow-by-blow of the seminar, just a few points that especially piqued my interest.

Tools Discussed

Code-writing methodology

  • First, write a functioning code stub.
  • Then define your tests using the new (free but beta) Code Tester tool. DO NOT use dbms_output to test your code!
  • After you have defined all your test cases, start filling up the stub with code. As each piece of functionality is added, run your tests and proceed only if the desired output for a given test is received.
  • This is a revolutionary way to write code, IMO, provided the Code Tester utility can really deliver as promised. I’ll be checking it out.

Encapsulate SQL behind a standard PL/SQL API

  • Since Java is fully able to work with Oracle cursors, there is absolutely no reason to permit SQL in Java code. Hide SQL within packages. That way data model changes do not break application code.
  • There have been tools for generating standard database interfaces available in Java for years (newer frameworks like Ruby on Rails provide object-oriented interfaces out-of-box, with no code generation required). Now there is at least one tool available for PL/SQL: QNXO
  • Downside: changes to the encapsulating packages invalidate all other code that relies on them, necessitating recompilation.

Use bulk binding (BULK COLLECT, FORALL) instead of conventional binding (cursor FOR loops)

  • Downside: can exhaust PGA
  • 10g optimizing compiler converts cursor FOR loops into bulk binds (unless it is disabled via the PLSQL_OPTIMIZE_MODE parameter. It is enabled by default. To enable: plsql_optimize_mode=2)

Exception Handling and Logging

  • Write your exception handling FIRST, before writing code.
  • The built-in logging functionality is lame. Devise your own logging tables to capture error information.
  • Log information from newer features like dbms_utility.format_error_stack
  • Hide exception-handling within your API. That way, as your error logging methodology becomes more robust, you don’t have to change alot of your application code.
  • Don’t hard-code your custom exception numbers (-20000 to -20999) in the code; use a package declaration to alias these with meaningful constant names. Or, dispense with the numbers altogether and just use string literals! This actually works.
  • Downside to using constants: everytime you add a new error constant, ALL code that relies on the package must be recompiled.

General advice

  • Always use packages instead of standalone procedures/functions (variables, constants and cursors can persist throughout a session with packages).
  • Become intimate with the functions provided in the standard package.
  • Use the NOCOPY hint with IN OUT procedure arguments to emulate “pass by reference” and thereby speed up getting large datasets out of a procedure.
  • Avoid SELECTing INTO individual variables. Use %ROWTYPE variables instead. (if the cursor’s SELECT list changes, there is potentially less code to change).
  • If you must use individual variables (e.g. you are creating a “fullname” variable by concatenating FNAME || ‘ ‘ || LNAME), declare this as a SUBTYPE and hide the definition behind a package interface. Makes code changes much easier if your definition of “fullname” changes or if the field definitions for FNAME and LNAME ever change.
  • Always identify END statements with the name of the block. If it’s an anonymous block, you can name it thus:

<< foo >>
BEGIN

END foo;

  • dbms_sql.describe_cursor tells you what columns are available in a cursor.
  • Declare functions as DETERMINISTIC wherever possible, so that they can be used in function-based indexes.

November 9, 2006

Three Normal Forms tutorial revised

Filed under: Databases — fred @ 3:03 pm

After two months in the wilderness, my tutorial on the Three Normal Forms is back online.

I took it down because in the original, it had some serious flaws. While not factually inaccurate, the section pertaining to NF1 was vague and misleading, and actually belonged in the NF2 section. I have corrected and expanded the NF1 discussion, and I hope I have not obscured the simplicity of the original tutorial.

Thanks especially to Richard Moignard whose helpful feedback finally inspired me to make the changes.

August 12, 2006

Oracularly Certified

Filed under: Databases — fred @ 3:09 pm

One of my projects this summer was to get Oracle Certified. I already felt myself fairly strong in most aspects of Oracle database administration due to a 9-month course I took at BMCC. I found myself having to explain too often that this was not the “official” Oracle certification, however, so I finally decided I had better get on the bandwagon and pay for some exams if I was going to be taken seriously as a DBA.

While the test isn’t exactly easy, the subject matter is frankly not very deep compared to what we did in the BMCC course. I also had a few qualms with the book I used to prepare for the exam, OCA: Oracle 10g Administration I Study Guide. Most noticable was the presence of several different voices in the text– understandable in a book written by 4 people, but a little more editing could have been done to smooth out the edges. This is especially true in the Flashcard app that comes with the book; some sections were written with careful explanations of the answers while others just said “true” or “false” without comment.

Also annoying were a few factual errors in the text, and vague, misleading, or incomplete answers to questions (for example, in practice test 1, question 18, there is no NAMES.DEFAULT_DIRECTORY setting in the sqlnet.ora file, guys)… I frequently found myself arguing with the book. It is hard to call this a flaw, however, because it inspired me to dig deeper into the Oracle documentation to find out the “real deal”.

Despite my misgivings I found the book useful and, on the whole, well written. I had a couple of local Oracle installations that I could play around with, but the book provides copious screenshots of the GUI tools that should allow people without adequate server resources to get a fair sense of what to expect. One final note of caution is that the book is geared for 10g Release 2, and I did all my practicing on a R.2 server, but Oracle is not actually using the Release 2 exam until November 1 2006. So I studied a lot of material that wasn’t even on the exam. I didn’t notice that until the night before.

Well, anyhow, I passed! And I now have the right to have one of these tatooed on my forehead:

10goca_logo.gif

Powered by WordPress