Emulating MySQL ENUM Columns

The MySQL enum column type is useful for enforcing constraints on your text data columns. We’ll look at how to emulate this functionality in Oracle using standard SQL constraints. As an example, let’s look at a simple MySQL comments table.

CREATE TABLE comments(
comment_text text,
comment_type ENUM ('spam', 'approved')
)

MySQL will ensure that your comment_type field only contains one of those two values. I do want to warn you that in typical MySQL fashion, only a warning is issued when you try to insert a value that is not allowed and the field value is set to the empty string. How this is allowed or acceptable behaviour baffles me.


mysql> insert into comments values('hi', 'ham');
Query OK, 1 row affected, 1 warning (0.01 sec)

mysql> select * from comments;
+--------------+--------------+
| comment_text | comment_type |
+--------------+--------------+
| hi | |
+--------------+--------------+
1 row in set (0.00 sec)

To create a true enum in Oracle, we simply make the comment_type column varchar2 and add a constraint to it.

CREATE TABLE comments(
comment_text varchar2(4000),
comment_type varchar2(8),
CONSTRAINT cons_comment_type
CHECK (comment_type IN ('spam', 'approved'))
)

Now Oracle won’t let you insert values into the comment_type field that aren’t in the approved list, the expected behaviour of an enum.

SQL> insert into comments values ('hi', 'ham');
insert into comments values ('hi', 'ham')
*
ERROR at line 1: ORA-02290: check constraint (ORABLOG.CONS_COMMENT_TYPE) violated

Oracle once again comes through at helping you ensure data integrity. The MySQL way lets you limit values to the ones you choose AND the empty string. Yet another reason to make the switch to Oracle today!

Note that this article came out of my effort to port the Trackback Validator Plugin to Orablog to reduce the amount of spam trackbacks we receive! There is no license attached to the plugin so I’m checking with the authors to see if I can post my changes as part of the Orablog distribution.

MySQL to Oracle Date and Time Helper Functions

When porting an application from MySQL to Oracle you have to be aware of the different ways that dates and times are handled.

In this post we’ll be looking at making porting easier by providing a set of date and time functions ported to Oracle in order to reduce the number of SQL queries to modify.

Continue reading ‘MySQL to Oracle Date and Time Helper Functions’

PHP Conference Update: Oracle is free and easy to use!

We’ve almost made it through the first day of the PHP conference in Germany. The conference is well organised and a smashing success. Thanks to everyone who has stopped by the booth and said hello!

We’ve noted some points that people are generally surprised to learn when talking to us:

  • Oracle XE is free to develop on, distribute and deploy
  • MySQL is not free to distribute or deploy with non-GPL software
  • Oracle XE is relatively easy to install and manage

The first two points are rather important. If you build an intranet application and deploy it inside your organisation, your users can request the source code and legally you have to give it to them. It’s not likely that they will know that you’re running the GPL version of MySQL so that they have the right to request it but legally you have to provide the source code upon request. Oracle XE doesn’t have this constraint and it’s free!

The second issue is ease of use. It is true that Oracle is quite complex and if you build a serious application using it you will require the services of an Oracle DBA at some point in time. I’ve seen many MySQL installations in my time and I can confidently say that almost every one should have used the services of a MySQL DBA to tune it correctly. DBA issues aside, Oracle XE provides an easy to use, web-based GUI for performing most tasks. It’s only when you delve outside of your standard CRUD operations, like modifying the default backup strategy, that Oracle-specific knowledge comes in to play.

We hope that you’re enjoying the conference and if you haven’t stopped by and said hello yet then please do!

Bug Fixes and Updated RMAN Configuration

My previous article on implementing incremental RMAN backups on XE had some problems:

  • I failed to set a recovery window so that RMAN could accurately determine which backups were okay to delete
  • I referred to my tag as whole_db_cpy in my copy and whole_db_copy in my incremental backup

This combination led to a very small database taking up 8 Gigabytes of backup space. I could verify the backups and the data was all there but I wasn’t using my space as efficiently as I could have.  By setting the recovery window to seven days, RMAN will ensure that I retain the appropriate backups to restore data within that window.  Since this script runs once a day, this should equate to at most one full backup and 8 incremental backups.
I have updated the article so that it is now correct. Please let me know if you find anything else that is wrong with the article!

Auto Increment Fields

Oracle as such does not have an auto increment field type. You can however emulate the auto increment field type using a trigger and a sequence. You can also retrieve the auto incremented value back from an insert statement. This article will show you all you need to emulate auto increment fields in Oracle.

Continue reading ‘Auto Increment Fields’

Modifying Oracle XE’s backup.sh Script to do Incremental Backups

One of the primary reasons for choosing Oracle over MySQL is Oracle’s rich set of enterprise features. This article looks at the builtin backup features of Oracle provided by the Recovery Manager (RMAN).

Since we are migrating from MySQL to Oracle, let’s compare RMAN to MySQL’s backup offerings. The two main programs for backing up MySQL databases are mysqldump and mysqlhotcopy. The mysqldump program performs a logical backup and the mysqlhotcopy program performs a physical backup.

Oracle databases (even XE!) come with a robust backup and recover tool called RMAN. I could try to summarise RMAN myself but Oracle has done such a great job already that I’ve decided to just quote them.

Continue reading ‘Modifying Oracle XE’s backup.sh Script to do Incremental Backups’

Column Names

One of the major items to note when converting an application from using MySQL to Oracle is the way in which column names are handled. Generally MySQL returns column names lowercased and Oracle generally returns column names uppercased.

Notice the keyword in both cases being ‘generally’. As with most things to do with MySQL and Oracle everything is not always as easy as it seems. Continue reading ‘Column Names’

Moving to Oracle

One of the most frustrating things about being a small company in 1999 was not having a decent database at an affordable price.  Oracle and DB2 were too expensive so we were left with MySQL and PostgreSQL.  Postgres was reliable in those days but rather slow.  MySQL was extremely fast but left out some basic relational database ‘features’ such as, well, relationships.  We decided that we could build safeguards into the software to enforce relationships but we couldn’t do anything to improve the performance of Postgres so we embarked upon a journey of many years with MySQL.  And we weren’t alone.  If you wanted cheap, you went with MySQL.
Fast forward to 2006 and MySQL supports relationships, Postgres is fast and Oracle XE is free!  We just started getting back together, so to speak, with Oracle and the first week was a love-hate tug of war!  Oracle is truly an enterprise quality database with an amazing range of features.  The down side is that you have to think like an enterprise, not a nimble and constantly changing web development company.

We’ll be chronicling our adventures here so that others can benefit too.  Coming soon: Zend Core for Oracle, database tuning, converting WordPress from MySQL to Oracle (orablog - what powers this site!) and creating a backup plan with RMAN.

Cheers!