0

我在 SQL Server 2005 中有 2 个表,如下所示

表 A

  • ActionID(PK,int,不为空)
  • ProgressID(唯一标识符,不为空)
  • ReferID(唯一标识符,不为空)
  • 字段 XYZ(varchar(50),不为空)
  • 字段 MNO(tinyint,不为空)

表 B

  • TrackID(PK,int,不为空)
  • ProgressID(唯一标识符,不为空)
  • ReferID(唯一标识符,不为空)
  • 字段 ABC(varchar(20),不为空)
  • 字段 EFG(日期时间,不为空)

现在我有一个具体的问题:

两个表ProgressID中的 指的是同一个实体。我想建立一个完整的关系,这样ProgressID当表 B 中存在值时,表 A 中的删除是不可能的。如何做到这一点?

4

1 回答 1

1

我会为此推荐一个删除前触发器。就像是

create trigger tr_tableA_progressId
on TableA for Delete
as 
    if exists
        (select 'true'
        from dbo.TableB
        where TableB.progressID = (select progressID
                                  from deleted d))

        BEGIN
            RAISERROR 'Cannot delete progressId exists in TableB'
            ROLLBACK TRAN
        End

我不知道会强制执行您想要的约束。

于 2011-02-01T17:03:05.333 回答