如何防止数据库用户通过触发器删除所有表中的数据

发布于 2024-10-11 07:12:54 字数 61 浏览 1 评论 0原文

专家您好 如何防止数据库用户使用触发器删除表中的任何数据? 我只想管理员从表中删除数据

谢谢

Hi Experts
How I can prevent database user deleting any data in tables using triggers?
I want just Admin delete Data from tables

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甜中书 2024-10-18 07:12:54

嗯,取消用户权限吗?如果您不希望他们做某事,请“禁止”他们这样做……这就是我们拥有权限的原因。

以下是有关如何撤销权限的详细信息:

http://msdn.microsoft.com/ en-us/library/ms186308.aspx

Umm take away that users permission? If you don't want them doing something, 'disallow' them that right... thats why we have permissions.

Here are details on how to revoke permissions:

http://msdn.microsoft.com/en-us/library/ms186308.aspx

任性一次 2024-10-18 07:12:54

您想使用触发器有什么特殊原因吗?

您只需删除要限制的用户的 DELETE 权限即可。请查看此处的“权限”部分: http://msdn.microsoft .com/en-us/library/ms189835.aspx

编辑:既然你说你确实想使用触发器(但我真的认为你应该重新考虑),你可以创建一个表,例如:

CREATE TABLE Restricted_Users 
(
  user_name VARCHAR(40) PRIMARY_KEY -- Use a size appropriate to your requirements
)

所有表上创建INSTEAD OF DELETE触发器(这将是一件苦差事),它会检查中的USER_NAME() >Restricted_Users 表,如果它们EXIST,您可以调用RAISERROR 来回滚事务并向用户显示一条消息。

请记住,您必须在添加到数据库的所有新表上维护这些触发器,并在每次从数据库中添加/删除用户时维护 Restricted_Users 表中的用户列表。

使用 SQL Server 中可用的权限系统(这就是它的设计目的)以及为表设置了适当权限的角色,会简单得多。然后,在添加新用户时,您只需将他们分配给适当的角色,系统就会为您处理删除权限。

Any particular reason you want to use triggers?

You could simply remove the DELETE permission from the users you want to restrict. Have a look at the "Permissions" section here: http://msdn.microsoft.com/en-us/library/ms189835.aspx

EDIT: Since you say you do want to use triggers (but I really think you should reconsider) you can create a table such as:

CREATE TABLE Restricted_Users 
(
  user_name VARCHAR(40) PRIMARY_KEY -- Use a size appropriate to your requirements
)

Create INSTEAD OF DELETE triggers on all your tables (that's going to be a chore) which checks for the USER_NAME() in the Restricted_Users table and if they EXIST you can call RAISERROR to cause the transaction to be rolled back and display a message to the user.

Remember you will have to maintain these triggers on all new tables added to the database as well as maintaining the list of users in the Restricted_Users table whenever you add/remove users from the database.

It would be a lot simpler to use the permission system available in SQL Server (it's what it's designed for) using roles with appropriate permissions set for the tables. Then, when adding new users you only have to assign them to the appropriate role and the delete permissions are handled for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文