本文介绍了有没有办法避免使用约束或触发器在特定表上删除行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法避免在使用约束的特定表格上删除行?



我想要(例如)如果id为0 ,1或2



这是为了避免用户删除应用程序的主帐户,我想避免它,即使有人尝试它(错误)使用sql直接。



谢谢!



EDIT: b
$ b

这个问题的整个想法是不要触摸应用程序。这不是一个安全问题,我只需要知道是否可以做我所要求的约束或SQL Server具有的任何其他事情(它不需要是一个标准的数据库解决方案)。



编辑2:



代码示例非常非常感谢:D

解决方案

至于在约束中强制实现这一点,我的解决方案是创建一个依赖表,因此引用的行不能被删除。

  CREATE TABLE NoKillI(
id INT NOT NULL,FOREIGN KEY(id)REFERENCES帐户(ID)ON DELETE RESTRICT
);
INSERT INTO NoKillI(id)VALUES(0);
INSERT INTO NoKillI(id)VALUES(1);
INSERT INTO NoKillI(id)VALUES(2);

现在没有人可以删除帐户中的行 id值为0,1或2,除非它们首先删除 NoKillI 中的相应行。您可以使用SQL权限限制依赖表的删除。


Is there a way to avoid row deletion on an specific table using constrains?

I'd like to (for example) deny row deletion if the id is 0,1 or 2

This is in order to avoid users deleting master accounts for an application, and I'd like to avoid it even if someone tries it (by mistake) using sql directly.

Thanks!

EDIT:

The whole idea of this question is not to touch the application. It's not a matter of security, I just need to know if It's possible to do what I asked with constrains or any other thing that SQL Server has (It does not need to be an standard db solution).

EDIT 2:

Code samples are very, very appreciated :D

解决方案

As far as enforcing this in a constraint, my solution would be to create a dependent table, so referenced rows cannot be deleted.

CREATE TABLE NoKillI (
  id INT NOT NULL, FOREIGN KEY (id) REFERENCES Accounts(id) ON DELETE RESTRICT
);
INSERT INTO NoKillI (id) VALUES (0);
INSERT INTO NoKillI (id) VALUES (1);
INSERT INTO NoKillI (id) VALUES (2);

Now no one can delete rows in Accounts with id values 0, 1, or 2, unless they first delete the corresponding rows in NoKillI. You can restrict deletions against the dependent table using SQL privileges.

这篇关于有没有办法避免使用约束或触发器在特定表上删除行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 05:09