我不確定你在說什麼!可能如果你發佈了一個示例模式,我可以理解你的意思。
你可以在一個表多個空FK列:
建表和FKS:
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 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)
顯示的數據(見多少個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)
如果這不是你在說什麼,你需要提供一些示例表和數據。
刪除這些測試表:
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
'CREATE TABLE A(X INT NULL UNIQUE); CREATE TABLE B(X INT空引用A(X)); INSERT INTO B值(NULL),(NULL )'工作正常,所以大概你在談論限制表A中的多個空值? –