插入时基于其他列的计算字段值 [英] Computed field value based on other column while insertion
问题描述
我有一个表,其中有一个 ID
列(主键自动递增)和一个名称列.
I have a table where I have an ID
column(primary key auto increment) and one more column for name.
我想在插入时根据生成的 ID 列值以格式自动填充名称列的值
I want to fill name column's value automatically while insertion based on generated ID column value in format
<IDColumnValue>_School
我知道有两种方法可以做到这一点
I am aware of the two ways to do this
- 使用触发器
- 先插入行,然后根据插入的行 ID 列值更新其列值
但实际上我想让这个字段不可为空,但要使用第二个选项,我必须使它可以为空.
But actually I want to make this field Non Nullable but to use the second option I will have to make it nullable.
在插入行时是否有任何直接的方法可以做到这一点,以便我可以使字段不可为空?
Is there any direct way to do this while inserting row so that I can have the field non nullable?
推荐答案
它适用于 firebird 但我认为它必须在 MySQL 中工作 因为 MySQL 有 new/old
操作符.带有字段 B 和 C(非空)的表 XYZ 的波纹管触发器.
It work for firebird but I think it must work in MySQL because MySQL have new/old
operators. Bellow trigger for table XYZ with fields B and C (not null).
CREATE OR ALTER trigger xyz_bi0 for xyz
active before insert position 0
AS
begin
new.c=new.b||' some text';--this construction must work in MySQL
end
这篇关于插入时基于其他列的计算字段值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!