我有3个表:T_A(PK AId),T_B(PK计算列BId),T_C(PK CId)。在T_A之间,T_B是FK约束ON T_A.AId = T_B.AId。在T_B中,PK BId作为具有表达式的计算列:(ISNULL('CAT _'+ [AId] +'_'+ CONVERT(varchar,[Id],0),0))PERSISTED NOT NULL。 有一段代码:
-- Table dbo.T_A
CREATE TABLE [dbo].[T_A]
(
[AId] Char(9) NOT NULL
)
go
-- Add keys for table dbo.T_Season
ALTER TABLE [dbo].[T_A] ADD CONSTRAINT [PK_AId] PRIMARY KEY ([AId])
go
-- Table dbo.T_B
CREATE TABLE [dbo].[T_B]
(
[BId] AS (ISNULL('CAT_'+[AId]+'_' + CONVERT([varchar](5),[Id],0),0)) PERSISTED NOT NULL,
[Id] Int IDENTITY(1,1) NOT NULL,
[AId] Char(9) NULL
)
go
-- Add keys for table dbo.T_B
ALTER TABLE [dbo].[T_B] ADD CONSTRAINT [PK_T_B] PRIMARY KEY ([BId])
go
ALTER TABLE [dbo].[T_B] ADD CONSTRAINT [AK_Id] UNIQUE ([Id])
go
-- Table dbo.T_C
CREATE TABLE [dbo].[T_C]
(
[CId] Int IDENTITY(1,1) NOT NULL,
[BId] AS ???????,
)
go
-- Add keys for table dbo.T_C
ALTER TABLE [dbo].[T_C] ADD CONSTRAINT [PK_CId] PRIMARY KEY ([CId])
go
-- Create relationships section -------------------------------------------------
ALTER TABLE [dbo].[T_B] ADD CONSTRAINT [Relationship8] FOREIGN KEY ([AId]) REFERENCES [dbo].[T_A] ([AId])
go
ALTER TABLE [dbo].[T_C] ADD CONSTRAINT [Relationship21] FOREIGN KEY ([BId]) REFERENCES [dbo].[T_B] ([BId])
go
我正在使用MS SQL Server 2008 R2。如何创建FK关系从T_C.BId到T_B.BId(代码???????),如果可能的话? 这个好设计吗?还是完全荒谬的建议?很多人都不喜欢。