USE [Fk_Test2] GO /****** Object: Table [dbo].[Owners] Script Date: 08/20/2010 16:52:44 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[Owners]( [Owner] [varchar](10) NOT NULL, CONSTRAINT [PK_Owners] PRIMARY KEY CLUSTERED ( [Owner] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[Key] Script Date: 08/20/2010 16:49:57 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[Key]( [owner] [varchar](10) NOT NULL, [key_id] [varchar](10) NOT NULL, [description] [varchar](10) NOT NULL, CONSTRAINT [PK_Key] PRIMARY KEY CLUSTERED ( [owner] ASC, [key_id] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO ALTER TABLE [dbo].[Key] WITH CHECK ADD CONSTRAINT [FK_Key_Owners] FOREIGN KEY([owner]) REFERENCES [dbo].[Owners] ([Owner]) GO ALTER TABLE [dbo].[Key] CHECK CONSTRAINT [FK_Key_Owners] GO /****** Object: Table [dbo].[Bldg] Script Date: 08/20/2010 16:50:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[Bldg]( [bldg] [varchar](10) NOT NULL, [owner] [varchar](10) NOT NULL, CONSTRAINT [PK_Bldg] PRIMARY KEY CLUSTERED ( [bldg] ASC, [owner] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO ALTER TABLE [dbo].[Bldg] WITH CHECK ADD CONSTRAINT [FK_Bldg_Owners] FOREIGN KEY([owner]) REFERENCES [dbo].[Owners] ([Owner]) GO ALTER TABLE [dbo].[Bldg] CHECK CONSTRAINT [FK_Bldg_Owners] GO /****** Object: Table [dbo].[KeyToBuilding] Script Date: 08/20/2010 17:13:52 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[KeyToBuilding]( [owner] [varchar](10) NOT NULL, [bldg] [varchar](10) NOT NULL, [key_id] [varchar](10) NOT NULL, CONSTRAINT [PK_KeyToBuilding] PRIMARY KEY CLUSTERED ( [owner] ASC, [bldg] ASC, [key_id] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO ALTER TABLE [dbo].[KeyToBuilding] WITH CHECK ADD CONSTRAINT [FK_KeyToBuilding_Key] FOREIGN KEY([owner], [key_id]) REFERENCES [dbo].[Key] ([owner], [key_id]) GO ALTER TABLE [dbo].[KeyToBuilding] CHECK CONSTRAINT [FK_KeyToBuilding_Key] GO ALTER TABLE [dbo].[KeyToBuilding] WITH CHECK ADD CONSTRAINT [FK_KeyToBuilding_Bldg] FOREIGN KEY([owner], [bldg]) REFERENCES [dbo].[Bldg] ([owner], [bldg]) GO ALTER TABLE [dbo].[KeyToBuilding] CHECK CONSTRAINT [FK_KeyToBuilding_Bldg] GO
每當我嘗試設置此我得到一個錯誤 「列在表中‘關鍵’或‘大廈’不匹配現有的主鍵或唯一約束。具有與多個表的外鍵關係的組合主鍵。 SQL Server 2008米中的錯誤
上午我試圖以錯誤的方式實現這種關係?或者做一些愚蠢的事情?
該系統用於跟蹤人(物理)不同的建築物的密鑰,這就是爲什麼key_id和Key不是數據庫密鑰。
如果您發佈的實際DDL用於創建這些表會更容易些。 – codingbadger 2010-08-20 21:37:35
考慮包括一個所有者表與關係到Bldg和關鍵 – Beth 2010-08-20 21:40:39
我改變了我的模擬了表是一個實際的腳本另外我得到的錯誤是在FK_KeyToBuilding_Bldg它說Msg 1776,級別16,狀態0,行2 被引用表'dbo.Bldg'中沒有與外鍵'FK_KeyToBuilding_Bldg'中的引用列表匹配的主鍵或候選鍵。 – Peter 2010-08-21 00:30:36