2010-08-20 34 views
1
 
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不是數據庫密鑰。

+1

如果您發佈的實際DDL用於創建這些表會更容易些。 – codingbadger 2010-08-20 21:37:35

+0

考慮包括一個所有者表與關係到Bldg和關鍵 – Beth 2010-08-20 21:40:39

+0

我改變了我的模擬了表是一個實際的腳本另外我得到的錯誤是在FK_KeyToBuilding_Bldg它說Msg 1776,級別16,狀態0,行2 被引用表'dbo.Bldg'中沒有與外鍵'FK_KeyToBuilding_Bldg'中的引用列表匹配的主鍵或候選鍵。 – Peter 2010-08-21 00:30:36

回答

2

這是語法錯誤的來源;

在大廈表的主鍵是:

CONSTRAINT [PK_Bldg] PRIMARY KEY ([bldg], [owner]) 

而且在KeyToBuilding表,[FK_KeyToBuilding_Bldg]你正在嘗試引用([owner], [bldg])代替([bldg], [owner])

它幫助,如果你簡單地清理代碼位,下面執行正常:

CREATE TABLE [dbo].[Owners] 
    ( 
    [Owner] [varchar](10) NOT NULL 
    ,CONSTRAINT [PK_Owners] PRIMARY KEY CLUSTERED ([Owner]) 
) ; 

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], [key_id]) 
) ; 
ALTER TABLE [dbo].[Key] ADD CONSTRAINT [FK_Key_Owners] FOREIGN KEY([owner]) REFERENCES [dbo].[Owners] ([Owner]) ; 

CREATE TABLE [dbo].[Bldg] 
    ( 
    [bldg] [varchar](10) NOT NULL 
    ,[owner] [varchar](10) NOT NULL 
    ,CONSTRAINT [PK_Bldg] PRIMARY KEY CLUSTERED ([bldg], [owner]) 
) ; 
ALTER TABLE [dbo].[Bldg] ADD CONSTRAINT [FK_Bldg_Owners] FOREIGN KEY([owner]) REFERENCES [dbo].[Owners] ([Owner]); 

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], [bldg], [key_id]) 
); 

ALTER TABLE [dbo].[KeyToBuilding] ADD 
    CONSTRAINT [FK_KeyToBuilding_Key] FOREIGN KEY([owner], [key_id]) REFERENCES [dbo].[Key] ([owner], [key_id]) 
, CONSTRAINT [FK_KeyToBuilding_Bldg] FOREIGN KEY([bldg], [owner]) REFERENCES [dbo].[Bldg] ([bldg], [owner]) ; 

現在關於模型 - 我做不完全理解它 - 但如果我想跟蹤的人,鑰匙和建築物,這將做到:

alt text

上述模型不跟蹤建築物的業主,所以添加了BuildingOwner表。

  • 一個人可以擁有幾座建築物。
  • 建築可以由幾個人(合夥)擁有。

alt text

一鍵便可有一個所有者太多,不一定誰目前擁有鑰匙的人。

  • 鑰匙可以打開一個建築物,一個建築物可以用很多鑰匙打開。
  • 鑰匙擁有者。
  • 鑰匙由鑰匙持有人持有,鑰匙持有人可能也可能不是鑰匙的擁有者。

alt text

0

你應該可以創建它們,你用什麼方法來創建這是給你的錯誤的關係?

這可能是您正在向後創建它們。

同時檢查表中是否有衝突的數據。在您創建FK關係之前,KeyToBuilding中的所有行都必須位於Bldg和Key中。當所有3個表都是空行時最容易創建關係。

+0

我試圖在sql管理控制檯中創建它們。所有的表都是空的。我點擊KeyToBuilding上的設計,然後點擊關係,然後嘗試使用Bldg或Key設置外鍵作爲主表,並且它不起作用。 – Peter 2010-08-20 23:30:34

+0

嘗試另一種方式 - 點擊Design或Bldg設置FK和KeyToBuilding – Beth 2010-08-21 13:59:21