【问题标题】:How to create connection between two tables in SQL Server 2008?如何在 SQL Server 2008 中创建两个表之间的连接?
【发布时间】:2018-03-27 18:30:34
【问题描述】:

我有一个数据库,其中有学生表,其中包含系统中每个学生的信息。该表中的每条记录都有唯一标识符,该标识符是在 SQL 2008 中使用NEWID() 创建的。然后我还有其他三个使用相同 ID 的表来链接学生和记录。我想知道是否需要设置任何类型的属性来链接这些表/记录。这是我的学生表的示例:

st_id -> Auto increment id 
st_studentGUID -> Primary key
st_firstName
st_lastName
st_dob
st_gender
st_uid
st_udt
st_utime

这是我的其他三个表的示例:

Table 1                   Table 2                   Table 3
tb1_id -> auto increment  tb2_id -> auto increment  tb3_id -> auto increment
tb1_studentGUID           tb2_studentGUID           tb3_studentGUID

我尝试在 studentGUID 上为每个表 1,2 和 3 创建外键,但出现错误:

The ALTER TABLE statement conflicted with the FOREIGN KEY constraint "FK_Table1_Student". The conflict occurred in database "testDB", table "Student", column 'st_studentGUID'.

在我做一些研究之后,似乎发生了这个错误,因为表 1 没有全部 st_studentGUID。这是有道理的,因为学生记录可能存在于表 3 中,但不存在于表 1 中。我想知道是否可以在表之间设置任何其他类型的关系?我应该在studentGUID 字段上使用Indexes 吗?这就是我问这个的原因,在我的一个更新查询中,我在 where 子句中使用了自动递增的 id,由于某种原因,我收到了一个如下所示的错误:

ErrorCode   1205
Message [Macromedia][SQLServer JDBC Driver][SQLServer]Transaction (Process ID 111) was deadlocked on lock | communication buffer resources with another process and has been chosen as the deadlock victim. Rerun the transaction.
SQLState    40001

错误指向更新查询的结尾行,如下所示:

<cfquery name="updateTable1" datasource="testDB">
    UPDATE Table1
    SET
        tb1_testdt = <cfqueryparam value="#FORM.frm_testdt#" cfsqltype="cf_sql_date" maxlength="10" null="#yesNoFormat(!len(FORM.frmhs_testdtgr))#" />
    FROM Table1 AS Table
        LEFT OUTER JOIN studentLocked AS Locked
            ON Locked.lk_studentGUID = Table.tb1_studentGUID
    WHERE tb1_id = <cfqueryparam value="#FORM.frm_id#" cfsqltype="cf_sql_integer" />
        AND lk_active = '1'
        AND lk_staffID = <cfqueryparam value="#trim(appStaff)#" cfsqltype="cf_sql_char" maxlength="10" />
</cfquery>

上面的查询是检查锁定表中是否存在studentGUID,我这样做的原因是因为我们不希望两个不同的用户同时更新同一条记录。查询更新单个记录,应该很快。我对deadlock victim 上面的错误消息感到困惑。我的数据库设计中是否存在关闭或我的更新查询导致此错误的内容?我在网上找不到任何可以帮助我解决此问题的内容。如果有人遇到过此类问题,请告诉我。

【问题讨论】:

  • 您是否有一个包含所有 StudentGUID 的表?如果没有,那么您就无法真正在数据库中建立任何关系。不过,如果你不这样做,我不得不质疑你的设计。
  • @TabAlleman 所有 studentGUID 都在 Student 表中。但它们并不存在于所有三个表中。因为我们可能在 Table1 和 Table3 中记录了 studentGUID,但在 Table2 中没有。这有意义吗?
  • 我感觉您使用 newid() 在每个表中填充了 guid?这意味着对于给定的学生,每个表中的 guid 值不同。我看到了很多值得怀疑的事情。就像在每个表中都有一个身份和一个向导。这些列都有前缀来指示它们来自哪个表。当数据元素根据其所在的表进行更改时,这是我在数据管理中最大的烦恼之一。
  • 如果所有 studentGUID 都在 student 中,那么您应该不会收到错误消息。您发布错误但不发布命令。您希望我们如何帮助您?将 PK 放在 st_studentGUID 上并不是最佳选择。
  • 您是否从 Student 表中删除了任何记录,在 TableX 中留下了孤立的记录?尝试设置外键时,孤儿将导致失败。

标签: sql-server sql-server-2008 sql-update deadlock


【解决方案1】:

如果所有 StudentGUID 都存在于 Student 表中,那么正确的设计是让 Table1、Table2 和 Table3 中的 StudentGUID 成为 Student 表的外键。

【讨论】:

    【解决方案2】:

    既然您已经解决了 Student 缺少记录的外键问题,请尝试在所有表上设置 FK,然后重新尝试更新查询。

    如果死锁问题仍然存在并且可以重现,请为此提交一个新问题,包括您正在更新的表的架构以及您尝试传递的值。

    【讨论】:

      【解决方案3】:

      下面的脚本演示了如果学生表中存在所有键,您可以创建所描述的关系:

      CREATE TABLE students (st_id AS NEWID(), 
      st_studentGUID UNIQUEIDENTIFIER PRIMARY KEY)
      
      INSERT INTO students (st_studentGUID) VALUES ('3B9F59DD-BF0A-4A09-BF4E-A396E2978B24')
      INSERT INTO students (st_studentGUID) VALUES ('7CC5FF67-DAB8-426A-B9F7-E9F041718B6B')
      INSERT INTO students (st_studentGUID) VALUES ('84B80D3E-44C4-4291-857D-B6CA6552369D')
      
      CREATE TABLE t1 (tb1_id AS NEWID(), tb1_studentGUID UNIQUEIDENTIFIER)
      
      ALTER TABLE t1
      ADD CONSTRAINT FK_t1_tbl_studentGUID FOREIGN KEY (tb1_studentGUID) REFERENCES students(st_studentGUID)
      
      INSERT INTO t1 (tb1_studentGUID) VALUES ('3B9F59DD-BF0A-4A09-BF4E-A396E2978B24')
      
      CREATE TABLE t2 (tb2_id AS NEWID(), tb2_studentGUID UNIQUEIDENTIFIER)
      
      ALTER TABLE t2
      ADD CONSTRAINT FK_t2_tbl_studentGUID FOREIGN KEY (tb2_studentGUID) REFERENCES students(st_studentGUID)
      
      INSERT INTO t2 (tb2_studentGUID) VALUES ('7CC5FF67-DAB8-426A-B9F7-E9F041718B6B')
      
      CREATE TABLE t3 (tb3_id AS NEWID(), tb3_studentGUID UNIQUEIDENTIFIER)
      
      ALTER TABLE t3
      ADD CONSTRAINT FK_t3_tbl_studentGUID FOREIGN KEY (tb3_studentGUID) REFERENCES students(st_studentGUID)
      
      INSERT INTO t3 (tb3_studentGUID) VALUES ('84B80D3E-44C4-4291-857D-B6CA6552369D')
      

      现在创建第 4 个表,添加一个不在 students 表中的 UUID,并尝试创建外键约束:

      CREATE TABLE t4 (tb4_id AS NEWID(), tb4_studentGUID UNIQUEIDENTIFIER)
      
      INSERT INTO t4 (tb4_studentGUID) VALUES ('897925F1-BE92-44EB-82C3-88E1C33C7792')
      
      ALTER TABLE t4
      ADD CONSTRAINT FK_t4_tbl_studentGUID FOREIGN KEY (tb4_studentGUID) REFERENCES students(st_studentGUID)
      

      重现您的错误消息:

      The ALTER TABLE statement conflicted with the FOREIGN KEY constraint "FK_t4_tbl_studentGUID". The conflict occurred in database "IconERP", table "dbo.students", column 'st_studentGUID'.
      

      【讨论】:

        猜你喜欢
        • 2014-05-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-06-15
        • 2015-11-14
        • 1970-01-01
        • 2018-12-01
        • 1970-01-01
        相关资源
        最近更新 更多