Simple Pleasures…rlwrap and SQL*Plus Command Line editing on Linux

Fat, bald, likes a drink and a smoke. No, not me. That’s a description of Darren Lehman, the new coach of the Australian Cricket Team.
Sounds like a good bloke to me.
As a cricket fan, with the Ashes as the highlight of the sporting summer, I’m getting a horrible sense of deja vu.
If you read the press, Australian and English, you might be forgiven for thinking that the series is a foregone conclusion.
Yes, England should win, on paper. However, unless the groundsmen at the relevant venues have been doing something very innovative, the Tests themselves will be played on grass.
In order to take my mind off some of the more worrying parallels with this series and the one in 1989 – when Alan Border and a bunch of Aussie no-hopers demolished England 4-0 – I’ve been looking at one of those niggling little problems that I always mean to get sorted but never quite get round to.

Generally speaking, I much prefer Linux to Windows. There is however, on area where Windows has the upper hand.
When you’re working in SQL*Plus, Windows allows command line recall and editing by default. This feature is not present in Linux by default.
However, Linux, being Linux, there is a handy utility that can implement this functionality. It’s called rlwrap.
What I’m going to cover here is :

  • a recap of built-in SQL*Plus editing capabilities
  • Using rlwrap with SQL*Plus
  • The joys of TAB-Completion

Continue reading

PL/SQL – A Programmer’s Introduction – or Welcome to the Dark Side

This week, the Open Source Karma has been cast-aside. We’re going proprietary in a big way. We’re going to the very heart of Oracle’s power, deep inside the RDBMS – yes – it’s PL/SQL.

This post is dedicated to ( and essentially co-written by) Simon. Yes, my long-time best mate, long-time Luton Town fan, long-time Teradata expert and long time everything really ( we’ll he’s not as young as he was).
After all these years, Simon has become a bit curious about this PL/SQL thing I’m always going on about and would like to know more.
It is this desire – and large amounts of beer – that has persuaded him to play the Igor to my mad scientist and have a wander through this very quick guide to the language at the heart of most Oracle applications. In fact we came up with several possible descriptions of Simon’s role in this post, but he had a “hunch” that this was the right one.
So for him, and any other programmers who want to get up and running with PL/SQL, but don’t need to be told what a variable is, what follows is – not so much a PL/SQL 101 – as a PL/SQL 23-and-a-bit. Continue reading

Getting the Quiz Machine to Pay Out – talking to your database from a shell script

I was in the pub the other day with my mate Simon. It’s surprising just how many of my posts have their genesis in such a setting. For the benefit of any prospective employers ( and my Mum), I put this down to the company and the mental stimulation of working out just exactly how you collect your winnings from the Quiz Machine.
For anyone who does not have first-hand experience of English Pub Quiz machines, the trick is either to a) get the barman to pay you from the till or b) have about your person a rather large hammer.

Fortunately, the hammer wasn’t required on this particular occasion, which is just as well as neither of us had brought one ( it’s not really that kind of pub). The reason for our lack of success eventually became apparent. After a conversational odyssey through the Bedfordshire countryside (the vicissitudes of Luton Town) via Table Mountain (England’s prospects for the World Cup), Simon – definitely the brains of the operation in Quiz Machine terms – confessed to wrestling with one of those perennial problems that are an occupational hazard of the Database Specialist’s art.

Yes, as well as being a bit of a whizz on the Science and Nature stuff, Simon is a long-time Teradata expert. Continue reading

Passing parameters in SQL*Plus – or What’s the opposite of a Chick Flick ?

Watching Iron Man 2 the other night, I was somewhat surprised by a brief appearance on screen of someone who looked remarkably like Larry Ellison.
No, he wasn’t the villain although – depending on what Oracle ends up doing to MySQL – he could be a candidate for the role in Iron Man 3.
As a result of riding this cinematic rollercoaster, I experienced two profound revalations.
The first is that, despite it’s poor relation status in the array of tools available to the Oracle developer, SQL*Plus can still be incredibly useful ( OK, I’ve always thought this – but I’m flashing my Poetic License here and no, it hasn’t expired yet).
The second is that the opposite of a Chick Flick must be a Bloke Buster. I know, I should really just stick to the programming stuff.
Continue reading

Slave Driving – Getting SQL*Plus to do it for you

In my haste to play around in my shiny new Oracle XE instance, I’ve forgotten to set the Default Tablespace for any new users I create.
As a result I’ve got few tables and indexes in the SYSTEM tablespace. Fortunately, I don’t have to go through the drudgery of moving all of them by hand, I can get SQL*Plus to do it for me. Continue reading

Installing Oracle XE on Ubuntu 9.10

Right, time to install XE on Ubuntu 9.10. Here goes….

Before we go any further, I think I should point out that I’m running the 32-bit desktop version of Ubuntu – Ubuntu Server is for another machine on another day.

My reasons for installing Oracle are simply to have a database to play around with, so I don’t have to worry about multiple concurrent connections, or even setting up the Oracle client on machines I want to be able to connect from. I’ll be connecting from the machine I’m running the database on.

Update - since I wrote this post, Oracle have released 11gXE. You can find installation instructions for this newer version for Mint and Ubuntu here.

Something else worth mentioning is that I’ve got a bit of “previous” on this score.

I installed XE on Ubuntu 8.10 a while back, and hit a few issues with being able to run the menu options that get added to the Applications menu.

Something else I noticed about the process was the need to run lots of commands that were doing stuff I wasn’t necessarily up to speed on. This meant lots of referencing various man pages to make sure I wasn’t doing anything rash.

So this time, I’m going to try and do as much as possible through GUI tools…and try to get everything working properly.

One of the big pluses in getting Oracle to run on Linux is that you can then mess around doing clever things with shell scripts. However, interacting with the database via a script does require that you’ve configured everything to allow SQL*Plus to be invoked from the command line.

Bearing all this in mind, I’m approaching the task this time with the following objectives :-

  • Get Oracle XE up and running with minimal command-line intervention
  • Get the menus working from the Ubuntu desktop
  • Get SQL*Plus up and running from the shell

Getting the software

Right, first thing we need is the software itself. Rather than following the usual routine of going to the oracle site and downloading the software, I thought I’d see if I could take advantage of the Linux software package management utilities.

We’re in luck. Oracle does indeed provide a repository containing a variety of goodies, including XE.

So, what we’ll need to do first is to add the repository to the package repository database.

In 9.10, there’s this new utility called the Ubuntu Software Centre, so I thought I’d start there.

  1. From the Ubuntu Applications menu, select Ubuntu Software Centre
  2. From the Edit menu, select Software sources…At this point you’ll be prompted for your password
  3. In the Software Sources window, select the Other Software Tab.
  4. Click Add

In the pop-up window, type in the following as the APT line :-

deb http://oss.oracle.com/debian unstable main non-free

and click Add Source

You should now see this repository in the list.

  1. Check the box next to the repository and click Close
  2. Close the Ubuntu Software Centre window

If you want to double-check that the repository is now added, just open a Terminal session and type :-

cat /etc/apt/sources.list

You should see the new repository as the last entry in this list.

Unfortunately, the Ubuntu Software Centre doesn’t recognise this repository , so in order to actually obtain the software, we’ll need to use Synaptic Package Manager
To actually download the software from the repository :-

  1. From the Ubuntu System menu select Administration and then Synaptic Package Manager. Enter your password when prompted.
  2. In the Quick search field, type oracle-xe
  3. Remember, I’m going to be running the database and connecting to it all from one machine, so I don’t need the oracle-xe-client package. So, as I just want the Western European Edition, I just select the oracle-xe package.
    NOTE – if you want a language other than English, I think you need to choose the oracle-xe-universal package instead.
  4. Once you’ve marked the appropriate package, hit the Apply button.
  5. Synaptic now downloads two packages – libaio and oracle-xe.

Once the download is complete, we’re ready to install the software.

Installation

At this point, we really do need to go to the command line.

In a new Terminal session type the following :-

sudo /etc/init.d/oracle-xe configure
You will now be prompted for 4 bits of information :-

  • the Oracle Application Express (APEX) http port ( defaults to 8080)
  • the port for the database (TNS) listener ( 1521)
  • the password to be used for the SYS and SYSTEM database accounts
  • whether or not you want the database to start on boot (y)

NOTE – I’ve made a couple of changes to the default – I already have Apache installed so I need to use a different port. Also, I want the database to start when I choose, rather than automatically.

The TNS listener port can safely be set to 1521 ( or 1526) as these ports are known as defaults for the TNS listener so no other automatically configured software on your system should be using it.

The session looks something like this.

Oracle Database 10g Express Edition Configuration

-------------------------------------------------

This will configure on-boot properties of Oracle Database 10g Express

Edition. The following questions will determine whether the database should

be starting upon system boot, the ports it will use, and the passwords that

will be used for database accounts. Press <Enter> to accept the defaults.

Ctrl-C will abort.

Specify the HTTP port that will be used for Oracle Application Express [8080]:8081

Specify a port that will be used for the database listener [1521]:

Specify a password to be used for database accounts. Note that the same

password will be used for SYS and SYSTEM. Oracle recommends the use of

different passwords for each database account. This can be done after

initial configuration:

Confirm the password:

Do you want Oracle Database 10g Express Edition to be started on boot (y/n) [y]:n

Starting Oracle Net Listener...Done

Configuring Database... Done

Starting Oracle Database 10g Express Edition Instance...Done

Installation Completed Successfully.

To access the Database Home Page go to "http://127.0.0.1:8081/apex"

To check that the database is up and running, try accessing your database home page via a web browser.
If the page comes up, you’re up and running.

Getting the Menu Options to Work

You’ll also notice that the Ubuntu Applications Menu now has an entry called Oracle Database 10g Express Edition which has a number of options in it’s sub-menu…none of which will work right now.

In order to run these options, you’ll need to make yourself a member of the os DBA group that’s been created as part of the installation.

To do this :-

  1. Go to the Ubuntu System menu and select Administration and then Users and Groups
  2. Click the button with the picture of the keyring and enter your password
  3. Go to the dba group and then click Properties
  4. Click the checkbox next to your user
  5. Exit the Users and Groups tool.

Hey presto, the menu options will now work.

To test this :-

  1. Go to Applications / Oracle Database 10g Express Edition and click Stop Database This will stop the tns listener, shutdown the database, and the http listener.
  2. To verify this, try accessing the Database Home Page again. This time you should get a Page Not Found error.
  3. Now go back to the menu and this time click on Start Database. Give it a few seconds then try to access the Database Home Page again.
    This time, it should come up. If it doesn’t, it’s possible that the database is still starting up, so give it a bit more time and then try again.

Getting SQL*Plus to run from the shell

There is a menu option in the Oracle Database 10g Express Edition menu that lets you connect to the database via SQL*Plus.

All you need to do is click on this then at the SQL prompt type :-

connect system/password_you_set_during_install@XE
What I’m trying to do here is a bit different. I want to be able to invoke SQL*Plus from a Terminal session ( or a batch script).

Once again, we have to go to the command line to achieve this.

Remember, Oracle “sort of” supports Ubuntu as an OS for XE, but only sort of. The oracle supplied shell script that sets the environment variables to allow it’s command line utilities ( including SQL*Plus) to run, isn’t especially happy in the bash shell. So, you need to make a minor change to one line to get it to execute.

The file in question is /usr/lib/oracle/xe/app/oracle/product/10.2.0/server/bin/oracle_env.sh

Open this file in whatever editor takes your fancy and change the line :-

NLS_LANG=`$ORACLE_HOME/bin/nls_lang.sh`

To :-

NLS_LANG=`. $ORACLE_HOME/bin/nls_lang.sh`

Assuming you’re using the default Ubuntu setup ( bash is your shell), you can either copy the contents of this file wholesale into the .bashrc file in your home directory, or you can point to it in this file. Either way, after saving the change to .bashrc the next time you start a terminal session, you’ll find three new environment variables set on startup. To check this, open a Terminal session and type

echo $ORACLE_HOME

This should return /usr/lib/oracle/xe/app/oracle/product/10.2.0/server/

echo $ORACLE_SID

This should return XE

echo $NLS_LANG

This will return the character set for your database setup ( in my case ENGLISH_UNITED KINGDOM.AL32UTF8 )

And finally, you can now invoke SQL*Plus from the command line :-

$ sqlplus uid/pwd

SQL*Plus: Release 10.2.0.1.0 - Production on Mon Dec 7 01:16:38 2009

Copyright (c) 1982, 2005, Oracle. All rights reserved.

Connected to:

Oracle Database 10g Express Edition Release 10.2.0.1.0 - Production

SQL>

Conclusion…and an acknowledgement

Oracle XE is now up and running on Ubuntu. Now I can do some real damage :-D

My initial ( mostly) successful installation attempt on 8.10 was done with reference to the guide at http://mediakey.dk/~cc/ubuntu-howto-install-oracle/

If you prefer your installations done on the command line, this is worth checking out, but I hope I’ve brought this a bit more up to date.

One final note of caution – Oracle XE is pretty much delivered “as is”. Whilst it is wonderfully useful for playing around with an Oracle database, it does have some limitations. Not least of these is that, unlike it’s big brother (currently Oracle 11g Release 2), Oracle don’t produce quarterly security patches for it. This means that you should think carefully before you go and use it in a production environment.

I was lucky enough to attend a presentation by Alexander Kornbrust at UKOUG in 2006 in which he graphically demonstrated these limitations. You can find his presentation here.

Edit – Cannot reach Database Home Page after restart

Hmmm….I thought that was too easy !
After re-starting my computer, I found I could no longer get the Database Home Page coming up.
After much cursing and puzzlement, I’ve tracked down the problem to the listener.ora file.

This file is used to control the configuration of the TNS Listener which process traffic to APEX, which sits inside the database.

To fix this annoying glitch…

Open up a Terminal session

cd $ORACLE_HOME/network/admin
sudo gedit listener.ora

change the line

(ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC_FOR_XE))

To

# (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC_FOR_XE))

Effectively you are commenting out this line.

Save and exit.

Go to the Start Database option in the Oracle Menu (under applications).

You need to give it a couple of minutes because the TNS listener gets started after the database does.

You should now be able to get to the Home Page.

Please let me know if this works for you.

UPDATE – I’ve now written an Installation Troubleshooting Guide, which you may want to check out if things still aren’t working as they should.
There’s also an introduction to PL/SQL here if you’re interested.