Click here to Skip to main content
Click here to Skip to main content

How to Guarantee a Fixed Amount of Rows in a Table in Oracle

, 19 May 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
This tip describes how to guarantee a fixed amount of rows in a table by using triggers in Oracle.

Introduction

Occasionally, you may need to have a fixed amount of rows in a single table. For example, you may have a configuration type of table which should contain only one row and applications rely on that fact. To ensure that the amount of rows isn't changed, you can use a trigger to prevent insertions and deletions.

Implementation

Let's first create a table with one row. The table is like the following:

-- Create the test table
CREATE TABLE SingleRow (
   SomeNumber NUMBER,
   SomeText   VARCHAR2(100)
); 

And add a single row into it:

-- Add a single row
INSERT INTO SingleRow (SomeNumber, SomeText) VALUES (1, 'A'); 

After adding the desired rows (one row in this case), let's add a trigger on the table:

-- Define the trigger
CREATE OR REPLACE TRIGGER SingleRow_Trigger
BEFORE INSERT OR DELETE
ON SingleRow
FOR EACH ROW
BEGIN
   RAISE_APPLICATION_ERROR(-20000, 'Table can contain only 1 row');
END; 

So the trigger above fires upon INSERT and DELETE. The sole purpose of the trigger is to generate an error if the amount of data is about to change.

The trigger is fired before the modification and a custom error with number -20000 is raised.

So what happens now if the amount of rows is going to be changed. Executing the following statement...

-- Try to add a row
INSERT INTO SingleRow (SomeNumber, SomeText) VALUES (2, 'B');  

...produces an error message like the next one. Note that the error is received via SQL Developer so the description is larger than it would be in some other tool or program.

Error starting at line 2 in command:
INSERT INTO SingleRow (SomeNumber, SomeText) VALUES (2, 'B')
Error report:
SQL Error: ORA-20000: Table can contain only 1 row
ORA-06512: at "TEST.SINGLEROW_TRIGGER", line 2
ORA-04088: error during execution of trigger 'TEST.SINGLEROW_TRIGGER'
20000. 00000 -  "%s"
*Cause:    The stored procedure 'raise_application_error'
           was called which causes this error to be generated.
*Action:   Correct the problem as described in the error message or contact
           the application administrator or DBA for more information. 

The same error message is received if a DELETE statement is issued.

What If Changes Need to be Done Momentarily

Sometimes you may need to make changes to the amount of rows. This hardly applies to a single row scenario but in other cases, the total amount of rows may need to be changed.

The easiest way to do this is to use a ALTER TRIGGER...DISABLE statement, make the modifications and then enable the trigger again.

So deleting the example row in the table succeeds with the following snippet:

-- Delete the row
-- Disable the trigger
ALTER TRIGGER SingleRow_Trigger DISABLE;

-- Delete the row
DELETE FROM SingleRow;

-- Enable the trigger
ALTER TRIGGER SingleRow_Trigger ENABLE; 

History

  • 19th May, 2012: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Mika Wendelius
Architect
Finland Finland
I've been a programmer since mid 80's using languages like assembler, C/C++, PL/I (mainframe environment), pascal, VB (I know, I know, no comments please) and C# and utilizing different techniques and tools.
 
However I'm specialized in databases and database modeling. Mostly I have used products like Oracle (from version 6), SQL Server (from version 4.2), DB2 and Solid Server (nowadays an IBM product).
 
For the past 10+ years my main concerns have been dealing with different business processes and how to create software to implement and improve them. At my spare time (what ever that actually means) I'm also teaching and consulting on different areas of database management, development and database oriented software design.

Comments and Discussions

 
GeneralMy vote of 5 PinmemberVolynsky Alex21-May-12 3:03 
GeneralRe: My vote of 5 PinmvpMika Wendelius22-May-12 9:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.141223.1 | Last Updated 19 May 2012
Article Copyright 2012 by Mika Wendelius
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid