简体   繁体   English

可空外键约束

[英]Nullable Foreign Key Constraints

We are trying increase the scope of database compatibility for our web application. 我们正在尝试扩大Web应用程序的数据库兼容性范围。 Our application is Java EE with JSPs, Servlets, and EJBs. 我们的应用程序是带有JSP,Servlet和EJB的Java EE。 The database we are trying to make our application compatible with is SQL Server 2008. 我们试图使我们的应用程序兼容的数据库是SQL Server 2008。

The problem we are running into is that our application uses nullable foreign keys in many cases in many files. 我们遇到的问题是,在许多情况下,我们的应用程序在许多文件中使用可为空的外键。 These nullable foreign keys work in other databases but we have not found a way to get them to work in SQL Server 2008 because it will only allow a single foreign key to be 'null' at a given time. 这些可为空的外键可在其他数据库中工作,但我们尚未找到使它们在SQL Server 2008中工作的方法,因为它在给定时间仅允许单个外键为“空”。 We understand that, in general, it is best to avoid such nullable foreign keys. 我们了解,通常最好避免使用这种可为空的外键。 However, this web application is large and it would be quite difficult to change files one-by-one. 但是,此Web应用程序很大,很难一一更改文件。

Thus far, we have tried the following: 到目前为止,我们已经尝试了以下方法:
[1] initializing dummy elements in referenced tables so that the foreign keys will point to something. [1]初始化引用表中的伪元素,以便外键指向某物。
[2] using 'EXEC sp_msforeachtable "ALTER TABLE ? NOCHECK CONSTRAINT all"' in order to remove the foreign key constraint [2]使用'EXEC sp_msforeachtable“ ALTER TABLE?NOCHECK CONSTRAINT all”“来删除外键约束

Unfortunately, initializing dummy elements in [1] above broke many components in the web application. 不幸的是,上面[1]中初始化虚拟元素破坏了Web应用程序中的许多组件。

Unfortunately, trying to remove the constraint according to the statement in [2] above did not work. 不幸的是,根据上面[2]中的陈述尝试删除约束是行不通的。 We suspect this because subsequent attempts to drop each table result in Foreign Key constraint errors. 我们怀疑这是因为随后尝试删除每个表会导致外键约束错误。

Currently, clear answers to the following questions would help us make some progress: 目前,对以下问题的明确答案将有助于我们取得一些进展:
[1] Is there a quick fix to allow SQL Server 2008 to allow for multiple 'null'-valued foreign keys? [1]是否有一个快速修复程序允许SQL Server 2008允许多个“空”值的外键?
[2] Is there another workaround we could try that would not involve extensive changes to our web application? [2]是否可以尝试另一种不对Web应用程序进行大量更改的变通办法?

I'm not sure what you are talking about! 我不确定你在说什么! Possibly if you posted a sample schema, I could understand what you mean. 如果您发布了示例架构,我可能理解您的意思。

You can have multiple null FK columns in a table : 一个表中可以有多个空FK列

build the tables and FKs: 建立表格和FK:

CREATE TABLE dbo.AAAA
    (
    A_ID int NOT NULL identity(1,1) primary key,
    B_ID int NULL,
    C_ID int NULL
    )  ON [PRIMARY]

CREATE TABLE dbo.BBBB
    (
    B_ID int NOT NULL identity(1,1) primary key,
    A_ID int NULL,
    C_ID int NULL
    )  ON [PRIMARY]

CREATE TABLE dbo.CCCC
    (
    C_ID int NOT NULL identity(1,1) primary key,
    A_ID int NULL,
    B_ID int NULL
    )  ON [PRIMARY]


ALTER TABLE dbo.CCCC ADD CONSTRAINT FK_CCCC_AAAA FOREIGN KEY ( A_ID ) REFERENCES dbo.AAAA ( A_ID ) ON UPDATE  NO ACTION  ON DELETE  NO ACTION 
ALTER TABLE dbo.BBBB ADD CONSTRAINT FK_BBBB_AAAA FOREIGN KEY ( A_ID ) REFERENCES dbo.AAAA ( A_ID ) ON UPDATE  NO ACTION  ON DELETE  NO ACTION 
ALTER TABLE dbo.CCCC ADD CONSTRAINT FK_CCCC_BBBB FOREIGN KEY ( B_ID ) REFERENCES dbo.BBBB ( B_ID ) ON UPDATE  NO ACTION  ON DELETE  NO ACTION 
ALTER TABLE dbo.AAAA ADD CONSTRAINT FK_AAAA_BBBB FOREIGN KEY ( B_ID ) REFERENCES dbo.BBBB ( B_ID ) ON UPDATE  NO ACTION  ON DELETE  NO ACTION 
ALTER TABLE dbo.AAAA ADD CONSTRAINT FK_AAAA_CCCC FOREIGN KEY ( C_ID ) REFERENCES dbo.CCCC ( C_ID ) ON UPDATE  NO ACTION  ON DELETE  NO ACTION 
ALTER TABLE dbo.BBBB ADD CONSTRAINT FK_BBBB_CCCC FOREIGN KEY ( C_ID ) REFERENCES dbo.CCCC ( C_ID ) ON UPDATE  NO ACTION  ON DELETE  NO ACTION 

insert sample data: 插入样本数据:

INSERT INTO AAAA VALUES (NULL,NULL)
INSERT INTO AAAA VALUES (NULL,NULL)
INSERT INTO AAAA VALUES (NULL,NULL)

INSERT INTO BBBB VALUES (1,NULL)
INSERT INTO BBBB VALUES (2,NULL)
INSERT INTO BBBB VALUES (NULL,NULL)
INSERT INTO BBBB VALUES (NULL,NULL)
INSERT INTO BBBB VALUES (1,NULL)

Show the data (see how many FK columns are null): 显示数据(查看有多少FK列为空):

select * from AAAA
select * from BBBB
select * from CCCC

OUTPUT: 输出:

A_ID        B_ID        C_ID
----------- ----------- -----------
1           NULL        NULL
2           NULL        NULL
3           NULL        NULL

(3 row(s) affected)

B_ID        A_ID        C_ID
----------- ----------- -----------
1           1           NULL
2           2           NULL
3           NULL        NULL
4           NULL        NULL
5           1           NULL

(5 row(s) affected)

C_ID        A_ID        B_ID
----------- ----------- -----------

(0 row(s) affected)

If this isn't what you are talking about, you need to provide some sample tables and data. 如果这不是您要谈论的内容,则需要提供一些示例表和数据。

remove these test tables: 删除这些测试表:

ALTER TABLE dbo.CCCC drop CONSTRAINT FK_CCCC_AAAA
ALTER TABLE dbo.BBBB drop CONSTRAINT FK_BBBB_AAAA
ALTER TABLE dbo.CCCC drop CONSTRAINT FK_CCCC_BBBB
ALTER TABLE dbo.AAAA drop CONSTRAINT FK_AAAA_BBBB
ALTER TABLE dbo.AAAA drop CONSTRAINT FK_AAAA_CCCC
ALTER TABLE dbo.BBBB drop CONSTRAINT FK_BBBB_CCCC
drop table AAAA
drop table BBBB
drop table CCCC

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM