sql-server中通过sql查询删除多个表的数据
我有一个主桌Staff
。我还有其他表,例如 staffStudy
、StaffPenison
、visitInfo
等。我没有在这些表之间建立任何关系。我想知道如果我删除了员工表,那么是否可以从其他表中删除该员工的所有信息? Staff_id
用于表示特定员工的信息。任何帮助表示赞赏。
i have a master table Staff
. i have other table also such as staffStudy
, StaffPenison
, visitInfo
etc. i haven't made any relation between these table. i want to know that if I delete the Staff table then is it possible to delete all the information of that staff from other tables? Staff_id
is use to represent the information of particular staff. any help appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
创建一个从 12 个表中删除的存储过程。
现在,您的按钮单击事件可以只调用此存储过程,如果您将表添加到模型中,则只需更改该过程,而不必更改应用程序的所有副本。
在这里没有外键关系似乎很奇怪;虽然我同意其他人的观点,即级联不属于生产环境,因此它无助于解决这个特定问题,但 FK 约束出于许多其他原因很有用。这是一个有意识的选择吗?
Create a stored procedure that deletes from the 12 tables.
Now your button click event can just call this stored procedure, and if you add tables to the model, you just have to change the procedure instead of changing all copies of the application.
It seems strange that you wouldn't have foreign key relationships here; while I agree with others that cascade doesn't belong in production, so it wouldn't help solve this specific problem, but FK constraints are useful for many other reasons. Was this a conscious choice?
以下是触发器示例。
但是,请确保删除子表具有商业意义。如果一名工作人员离开(并被删除),如果他也失去了养老金,他可能会感到非常沮丧。
其他评论
正如 Mitch 所建议的,将额外的删除放在客户端代码中是有意义的,因为数据库触发器可能会导致其他程序员可能不知道的一些删除。但是,如果您在客户端代码中执行 12 个附加查询,请务必将它们包装在事务中。如果 3-6 个删除查询成功,但其他查询失败,会对您的系统产生什么影响?
The following is a sample trigger.
However, be sure it makes business sense to delete the child tables. If a Staff member leaves (and is deleted), he might get mighty upset if he loses his pension too.
Additional comments
As Mitch suggested, putting the additional deletes in Client code makes sense, because the database trigger could cause some deletions other programmers might not be aware of. However, if you do the 12 additional queries in your client code, be sure to wrap them in a transaction. What would be the impact on your system if 3-6 of the delete queries succeeded, but the others didn't?