Open Source RDBMS - Seamless, Scalable, Stable and Free

English | Login |Register

CUBRID Serials

The goal of this tutorial is to introduce some of the CUBRID features regarding Serial objects. This article will also show some "tricks" users can use in combination with Serials.

Overview

CUBRID Serials are a special type of database objects, which are used for creating and accessing a unique sequence number.

Serials have specific properties:

  • Explicit serials are serials created via CREATE SERIAL statement. They are not related with a particular table so can be used in multiple tables.
  • Implicit serials are automatically created by CUBRID whenever an AUTO_INCREMENT attribute has been assigned to a column.
  • Once created, all users can get the Serials values by using CURRENT_VALUE and NEXT_VALUE methods/pseudo-columns.

Database serials can be very useful for:

  • Creating a unique sequence number in multi-user environment.
  • Auto-generate a unique id for a field as a default value for that field.
  • Generate artificial primary keys.
  • Enforce unique id's across client applications.

Many database systems have nowadays support for similar type of objects, for example:

Warning: Even if they look very similar in concepts, there are some differences between various databases implementations of this concept. For instance, when comparing CUBRID and ORACLE implementations, there are different behaviors when using parallel user sessions in combination with retrieving the current value of the sequence.

AUTO_INCREMENT and Serial

So what is the connection between AUTO_INCREMENT and Serials in CUBRID?

Actually, even if they don’t seem to be related at a first sight, there actually are! When we create a new table in CUBRID, we have an option to specify an AUTO_INCREMENT attribute for a column just like in other RDBMS:

image001.jpg

However, if you look at the Serials list shown below, there are no changes. Still two serials which were created for the demodb database which is the demo database created automatically during CUBRID installation.

image002.png

No additional serial has been created, because we have not executed any CREATE SERIAL statement. However, if we look at the content of the system table db_serial, you will notice that an extra record has been created:

image003.jpg

The reason for this is very simple:

Whenever an AUTO_INCREMENT attribute is defined, CUBRID automatically creates an "implicit" Serial object.

This "implicit" AUTO_INCREMENT serial will be used by the system to handle the values in the associated column.

Summarizing, we have two types of Serials in CUBRID:

  • "Explicit" – created via CREATE SERIAL statements
  • "Implicit" – create automatically by the system for AUTO_INCREMENT columns

Modifying the system "implicit" serials

The system implicit serials (those automatically created for the AUTO_INCREMENT columns) behave in many ways the same as the "explicit" serials (the ones created using the CREATE SERIAL statement).

For example, you can use the current_value and next_values as usual:

image004.png

We can even modify the current_value/next_value sequence:

image005.png

However, if you try to update directly the db_serial table, you will get an error:

image006.png

And this is the default behavior stating with CUBRID version 8.1.0.

Warning: Unless you have good reasons or you know exactly what you are doing, it is not advisable to alter the default behavior of the system "implicit" AUTO_INCREMENT serials.

An AUTO_DECREMENT column

Theoretically, we can define only AUTO_INCREMENT columns, right…?

But what if we need an AUTO_DECREMENT column…? …Is there any way we can get one in CUBRID?

The first thing a user would try to do would probably be specifying a negative increment value in the column definition. However, CUBRID Manager will warn you about this and will not allow to proceed.

image007.png

Syntax

Here is the syntax how AUTO_INCREMENT should be used.

AUTO_INCREMENT [(seed, increment)]
  • seed: The initial value from which the number starts. Only positive integers are allowed. The default is 1.
  • increment: The increment value of each row. Only positive integers are allowed. The default value is 1.

But, we can do a little hack - manually update the db_serial table, via an ALTER SERIAL statement.

image008.png

And voilà - the results are exactly what we wanted – now we have an AUTO_DECREMENT up & running!

image009.png

Serials via Triggers

There is another way to get a more complex "default" value for a table column, besides using an AUTO_INCREMENT (or an AUTO_DECREMENT as we showed above), and that is using triggers.

Using triggers, not only we can implement the AUTO_INCREMENT functionality, but we can implement more complex rules regarding the values automatically used for the column.

Let’s start by creating a table – t_my_ai (note that we have not defined an AUTO_INCREMENT):

CREATE TABLE "t_my_ai"(
"id" integer,
"txt" character varying(16)
);

Next step - let’s create a basic default serial:

image010.png

And now we will create a trigger which will update automatically the id column in the table t_my_ai, the same way an AUTO_INCREMENT attribute would do, but we will make slight modifications. We want every next id be a square of the consecutive number. Obviously, this functionality can’t be achieved using AUTO_INCREMENT or Serials definitions… …but a trigger can do the job!

CREATE TRIGGER "trg_update_ai"
AFTER INSERT ON "t_my_ai"
IF [obj].[id] is null 
EXECUTE AFTER 
update [t_my_ai] set [id]=(select s_my_incr.next_value * s_my_incr.current_value) where ([id] is null );

Now, let’s test it:

image011.png

Quite interesting, right…?

Summary

We hope we showed you in this tutorial some interesting ways to use CUBRID Serials and how to create your own customized serials-alike functionalities!

You can even extend this functionality to deal with non-numeric data types – it is up to you to figure out, whenever you need and auto-value functionality if maybe a serial is a best fit, eventually combined with a trigger

We definitely encourage you to explore all these possibilities in CUBRID!

Links & Resources

CUBRID Online Manual

http://www.cubrid.org/webmanual/3.1/

CUBRID Serials Manual

http://www.cubrid.org/manual/830/en/SERIAL

CUBRID Developer's Course

http://www.slideshare.net/cubrid/cubrid-developers-course

Auto Increment

http://www.w3schools.com/sql/sql_autoincrement.asp

This concludes the CUBRID Serials tutorial. Please let us know your feedback and remember to periodically check the CUBRID web site – www.cubrid.org/tutorials - for more tutorials and resources.




You are either using a very old browser or a browser that is not supported.
In order to browse cubrid.org you need to have one of the following browsers:



Internet Explorer: Mozilla Firefox: Google Chrome: