Two autoincrements columns or autoincrement and same value in other column

后端 未结 3 1602
情深已故
情深已故 2020-12-22 12:47

I need two columns in table that would have same value on insert. Is there any way to do it from database side?

3条回答
  •  甜味超标
    2020-12-22 13:33

    Define a before or after insert trigger and assign the value of the 2nd field in the trigger.

    If the 1st field is an auto increment column, then you need to use an after insert trigger. If your application assigns value to the 1st field, then you can use a before insert trigger.

    However, I would no necessarily duplicate the value on insert. You can leave the 2nd field as null on insert, which would mean that its value is the same as the 1st field's. The only drawback of this approach is that it may be more difficult to create joins on the 2nd field.

提交回复
热议问题