sql-server中通过sql查询删除多个表的数据

发布于 2024-12-05 02:02:47 字数 212 浏览 0 评论 0原文

我有一个主桌Staff。我还有其他表,例如 staffStudyStaffPenisonvisitInfo 等。我没有在这些表之间建立任何关系。我想知道如果我删除了员工表,那么是否可以从其他表中删除该员工的所有信息? 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 技术交流群。

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

发布评论

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

评论(2

鹤仙姿 2024-12-12 02:02:47

创建一个从 12 个表中删除的存储过程。

CREATE PROCEDURE dbo.Staff_Delete
    @Staff_ID INT
AS
BEGIN
    SET NOCOUNT ON;

    BEGIN TRANSACTION;

    BEGIN TRY
      DELETE dbo.VisitInfo WHERE Staff_ID = @Staff_ID;
      DELETE dbo.StaffPension WHERE Staff_ID = @Staff_ID;
      DELETE dbo.StaffStudy WHERE Staff_ID = @Staff_ID;
      /* ...other tables... */
      DELETE dbo.Staff WHERE Staff_ID = @Staff_ID;
      COMMIT TRANSACTION;
    END TRY
    BEGIN CATCH
      ROLLBACK TRANSACTION;
    END CATCH
END
GO

现在,您的按钮单击事件可以只调用此存储过程,如果您将表添加到模型中,则只需更改该过程,而不必更改应用程序的所有副本。

在这里没有外键关系似乎很奇怪;虽然我同意其他人的观点,即级联不属于生产环境,因此它无助于解决这个特定问题,但 FK 约束出于许多其他原因很有用。这是一个有意识的选择吗?

Create a stored procedure that deletes from the 12 tables.

CREATE PROCEDURE dbo.Staff_Delete
    @Staff_ID INT
AS
BEGIN
    SET NOCOUNT ON;

    BEGIN TRANSACTION;

    BEGIN TRY
      DELETE dbo.VisitInfo WHERE Staff_ID = @Staff_ID;
      DELETE dbo.StaffPension WHERE Staff_ID = @Staff_ID;
      DELETE dbo.StaffStudy WHERE Staff_ID = @Staff_ID;
      /* ...other tables... */
      DELETE dbo.Staff WHERE Staff_ID = @Staff_ID;
      COMMIT TRANSACTION;
    END TRY
    BEGIN CATCH
      ROLLBACK TRANSACTION;
    END CATCH
END
GO

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?

山人契 2024-12-12 02:02:47

以下是触发器示例。

CREATE TRIGGER Delete_Staff_tables
ON Staff
FOR DELETE
AS
BEGIN
   DECLARE @StaffID INT
   SELECT @StaffID = Staff_ID FROM Deleted
   DELETE FROM StaffStudy WHERE staff_ID = @StaffID
   DELETE FROM StaffPension WHERE staff_ID = @StaffID
   -- Etc
END

但是,请确保删除子表具有商业意义。如果一名工作人员离开(并被删除),如果他也失去了养老金,他可能会感到非常沮丧。

其他评论
正如 Mitch 所建议的,将额外的删除放在客户端代码中是有意义的,因为数据库触发器可能会导致其他程序员可能不知道的一些删除。但是,如果您在客户端代码中执行 12 个附加查询,请务必将它们包装在事务中。如果 3-6 个删除查询成功,但其他查询失败,会对您的系统产生什么影响?

The following is a sample trigger.

CREATE TRIGGER Delete_Staff_tables
ON Staff
FOR DELETE
AS
BEGIN
   DECLARE @StaffID INT
   SELECT @StaffID = Staff_ID FROM Deleted
   DELETE FROM StaffStudy WHERE staff_ID = @StaffID
   DELETE FROM StaffPension WHERE staff_ID = @StaffID
   -- Etc
END

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?

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