mysql中的datetime设置了默认值current_timestamp,每次新增、更新自动设置为当前时间。dm8要怎么设置?
达梦目前没有类似的功能,但可通过增加触发器实现类似的功能,提供一个demo参考:
CREATE OR REPLACE TRIGGER confirmation_auto_update_time --创建或替换 名称为confirmation_auto_update_time的触发器
before INSERT OR UPDATE ON user_confirmation --新增和修改执行前出发,对象目标:TestTragger表
FOR EACH ROW --行级触发器,每影响一行触发一次
BEGIN
IF INSERTING THEN --插入数据操作
:NEW.update_time :=SYSDATE;
ELSIF UPDATING then --修改数据操作
:NEW.update_time :=SYSDATE;
END IF;
END;
/
在dm中,插入可以用current_timestamp做默认值,插入时可以自动插入当前时间,但是更新时不会,更新时可以使用触发器更新记录时自动设置字段的值。以下是一个创建触发器的示例:
-- 创建一个名为example_table的表 CREATE TABLE example_table ( id NUMBER, created_at TIMESTAMP DEFAULT current_timestamp, updated_at TIMESTAMP DEFAULT current_timestamp ); -- 创建一个用于自动更新updated_at字段值的触发器 CREATE OR REPLACE TRIGGER example_table_trg BEFORE UPDATE ON example_table FOR EACH ROW BEGIN IF UPDATING THEN :NEW.updated_at := SYSTIMESTAMP; END IF; END; / insert into EXAMPLE_TABLE(ID) VALUES(1); insert into EXAMPLE_TABLE(ID) VALUES(2); insert into EXAMPLE_TABLE(ID) VALUES(3); commit; select * from EXAMPLE_TABLE; update EXAMPLE_TABLE set ID=4 where id=1; commit; select * from EXAMPLE_TABLE;