Set default value as column value

132 Views Asked by At

i want to set default value as column for existing table, example : create table t(a Number(22), b NUMBER(22);

i jsut want to set default value for column B as column A.

I know we cannot do like this, could any one suggest an alternate way to do so..

2

There are 2 best solutions below

0
On

You could use an ON INSERT trigger:

CREATE OR REPLACE TRIGGER T_BI
  BEFORE INSERT ON T
  FOR EACH ROW
BEGIN
  IF :NEW.B IS NULL THEN
    :NEW.B := :NEW.A;
  END IF;
END T_BI;

Share and enjoy.

0
On

Why don't set the default value directly in the code ?

if(Obj.B == null)
Obj.B = Obj.A

I think is not the role of the database to express this kind of rule.