Skip to content
Advertisement

limit column char trigger plsql

I’m starting to learn PLSQL and created a very very simple table, I’m trying to get familiar with functions and triggers.

My table:

Now to the question, I want to limit before insert or update new row (biuefer) so that the first name must be 8 characters, not less, not more. If you try to insert a row with a name less or more than 8 character I want a message printed out.

Advertisement

Answer

If you wanted to implement such constraint, you would typically use a check constraint rather than a trigger:

Notes:

  • that there is no point declaring a varchar(30) if you don’t allow more than 8 characters anyway, so I shrinked that

  • you want varchar2 rather than varchar (this Oracle datatype is recommended in new code)

  • you should declare the lenght of the column with char – the default is byte, which can have suprising behavior if your data has multi-byte characters

But since you explained that you are playing around and want to learn how to do that with a trigger, here is how the code would look like:

Within the trigger code, you can use pseudo table :new to access the values that are currently being updated or inserted; you would wrap the check in a if statement and raise the error accordingly.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement