Home > database >  Oracle - convert a value to 0 after INSERT
Oracle - convert a value to 0 after INSERT

Time:12-05

I have a table called Army.There are the attributes:

army

  • army_name VARCHAR(50) PRIMARY KEY
  • number_of_soliders INTEGER;

I need to convert number_of_soliders to 0 after inserting to this table (or allow only 0 to be inserted).

How to accomplish this?

CodePudding user response:

Since this is part of a school project, you need to use a CREATE TRIGGER statement and modify the value BEFORE INSERT ON army and do that FOR EACH ROW.

In the trigger's PL/SQL block, you need to use the :new record and set the number_of_soldiers attribute to be zero using :NEW.number_of_soldiers := 0;.

The documentation for CREATE TRIGGER is here.

CodePudding user response:

You can try altering the column and set default value as 0.

So, when you insert army_name, the row will by default have number_of_soliders as 0.

ALTER TABLE ARMY MODIFY number_of_soliders default 0;
  • Related