0

嘿大家,我想創造我個人的朋友一個數據庫,並給我的經驗不足與我有困難,試圖建立一個我目前正在開發的數據庫處理。基本上,我的問題是與我的「房間」表有關聯的另一個表「位置」表;位置是你期望的所有東西(buildingID,streetAddress等),而房間有一個包含buildingId的外鍵。我希望我的「房間」表根據buildingId具有唯一的房間號碼值。數據庫設計 - 防止複製的「房間」表

爲了給你一個清晰的概念,我將不僅僅是C & P中的腳本,我使用的是創建這些表。

CREATE TABLE [dbo].[Location](
[buildingId] [int] IDENTITY(1,1) NOT NULL, 
[streetAddress] [varchar](50) NOT NULL, 
[postalCode] [varchar](7) NOT NULL, 
[province] [varchar](30) NOT NULL, 
[city] [varchar](30) NOT NULL, 
CONSTRAINT [PK_Location] PRIMARY KEY CLUSTERED 
(
[buildingId] ASC 
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY], 
CONSTRAINT [UN_postalCode] UNIQUE NONCLUSTERED 
(
[postalCode] ASC 
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY], 
CONSTRAINT [UN_streetAddress] UNIQUE NONCLUSTERED 
(
[streetAddress] ASC 
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] 
) ON [PRIMARY] 



CREATE TABLE [dbo].[Room](
[rmId] [int] IDENTITY(1,1) NOT NULL, 
[roomNum] [varchar](10) NOT NULL, 
[floor] [int] NOT NULL, 
[capacity] [int] NOT NULL, 
[permission] [bit] NOT NULL, 
[buildingId] [int] NOT NULL, 
CONSTRAINT [PK_Room_1] PRIMARY KEY CLUSTERED 
(
[rmId] 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].[Room] WITH CHECK ADD CONSTRAINT [FK_Room_Location] FOREIGN KEY([buildingId]) 
REFERENCES [dbo].[Location] ([buildingId]) 
GO 

ALTER TABLE [dbo].[Room] CHECK CONSTRAINT [FK_Room_Location] 
GO 

任何幫助將不勝感激。

謝謝。

回答

2

表級別唯一約束?

ALTER TABLE dbo.Room WITH CHECK ADD 
    CONSTRAINT UQ_Room_RoomBuildingLocation UNIQUE (roomNum, buildingId) 

這可能是唯一索引也將允許INCLUDE列

+0

哦!男人感謝助攻的好先生。 – Steve 2011-01-28 09:44:28