Sql:如果字段值低于某个值,则阻止更新

问题描述 投票:0回答:1

我想在我的表中添加一个约束:

create table asset (
   id number(19,0) not null,
    name varchar2(255 char),
    description varchar2(255 char),
    type varchar2(255 char),
    height number(10,0),
    width number(10,0),
    bytes blob,
    primary key (id)
);

在存储之后,ID为0到8的资产:

ALTER TABLE ASSET ADD CONSTRAINT check_if_id_bigger_than_eight CHECK (ID > 8)

为了避免进一步修改ID为0到8的行,遗憾的是上面的工作没有用,因为它不允许有违反约束的数据,有没有简单的方法呢?

sql constraints h2 flyway database-trigger
1个回答
0
投票

此触发器可能有效(sql server语法)

create trigger TR_8 
on asset
after insert, update

as
if exists(
 select * -- checks if new or modified datum has id between 0 and 8
 from inserted 
 where inserted.id between 0 and 8
)
begin
 RAISERROR ('Ids between 0 and 8 must stay untouched', 16, 10); --if so, rollback the transaction giving an error
 rollback transaction
end
© www.soinside.com 2019 - 2024. All rights reserved.