Re: Foreign key column Just additional info... Foreign Keys must reference either a PK or a column with a UNIQUE index... that not-with-standing, you still can't reference a computed column as a Foreign Key...
CREATE TABLE Foo
(
FooPk VARCHAR(255) NOT NULL,
FumFum VARCHAR(512),
PRIMARY KEY (FooPk)
)
GO
CREATE TABLE Fum
(
FumC1 VARCHAR(255) NOT NULL,
FumC2 VARCHAR(255) NOT NULL,
FumC3 AS CAST(( FumC1 + '/' + FumC2) AS VARCHAR(512))
)
GO
CREATE UNIQUE INDEX IX_Fum_FumC3 ON Fum (FumC3)
GO
ALTER TABLE dbo.Foo
ADD CONSTRAINT FK_Foo_Fum
FOREIGN KEY (FumFum) REFERENCES dbo.Fum (FumC3)
GO
Server: Msg 3701, Level 11, State 5, Line 1
Cannot drop the table 'Foo', because it does not exist in the system catalog.
Server: Msg 1784, Level 16, State 1, Line 1
Cannot create the foreign key 'FK_Foo_Fum' because the referenced column 'Fum.FumC3' is a computed column.
Server: Msg 1750, Level 16, State 1, Line 1
Could not create constraint. See previous errors. |