数据库设计 - 防止“房间”重复桌子

发布于 2024-10-14 17:38:25 字数 1820 浏览 0 评论 0原文

大家好,我正在尝试为我的一位私人朋友创建一个数据库,由于我在开发数据库方面缺乏经验,我很难尝试建立我目前正在处理的数据库。本质上,我的问题是我的“房间”表与另一个名为“位置”的表有关联;位置就是您所期望的一切(建筑物 ID、街道地址等),并且 Room 有一个包含建筑物 ID 的外键。我希望我的“房间”表具有基于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

任何帮助将不胜感激。

谢谢。

Hey everyone, I'm trying to create a database for a personal friend of mine and given my inexperience with developing databases I'm having difficulty trying to establish one I'm currently dealing with. Essentially, my is issue is with my "rooms" table which has an association with another table called "location"; The location is the is everything you would expect (buildingID, streetAddress,etc.), and Room has a foreign key containing the buildingId. I want my "rooms" table to have unique values for room numbers based on the buildingId.

To give you a clearer idea, I'll just c&p the script I'm using to create those tables.

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

Any help would greatly be appreciated.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清浅ˋ旧时光 2024-10-21 17:38:25

表级唯一约束?

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

这也可以是允许 INCLUDE 列的唯一索引

A table level unique constraint?

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

This can be a unique index too which would allow INCLUDE columns

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文