如何在SQL Server 2008中的两个表之间创建连接



我有数据库,该数据库的学生表包含有关系统中每个学生的信息。该表中的每个记录都有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吗?这就是为什么我要问这个问题的原因,在我的一个更新查询中,我使用自动递增的ID在wery子句中,由于某种原因,我收到了一个看起来像这样的错误:

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上方的错误消息。我的数据库设计中是否有一些关闭的内容或我的更新查询导致此错误?我在网上找不到任何可以帮助我解决此问题的东西。如果有人有这种问题的经验,请告诉我。

如果学生表中存在所有学生,则正确的设计是让table1,table2和table3中的学生为学生表的外键。

现在,您已经在学生丢失的记录中整理了外国密钥问题,请尝试在所有桌子上设置FKS,然后重新尝试更新查询。

如果僵持问题持续存在并且可再现,请为此提交一个新问题,包括您要更新的表格的架构以及您要通过的值。

这是一个脚本,证明您可以创建所描述的关系,前提是所有密钥都存在于学生表中:

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')

现在创建一个第四表,添加一个不在学生表中的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'.

最新更新