向sql添加多个值

发布于 2024-10-01 05:33:50 字数 97 浏览 3 评论 0原文

假设我有一列定义为地址。另外,我有一张唱片,我们就叫它鲁迪的吧。现在 Rudy's 有多个地址,所以我需要包含多个地址,以便它们都可以搜索。使用 SQL 解决方案的最佳方法是什么?

say i have a column defined as Address. also, I have a record, let's call it Rudy's. now Rudy's has multiple addresses, so I need to include multiple address so that they are all searchable. what is the best way to approach a solution in SQL?

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

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

发布评论

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

评论(2

寂寞笑我太脆弱 2024-10-08 05:33:50

您应该添加一个带有地址列的子表。您将拥有一对多关系,其中地址存储在子表中。您可以根据需要为每个用户添加任意​​数量的地址。您还可以添加额外的信息,例如地址类型(家庭、工作或主要、次要等)。

我不会选择一列地址。如果这是邮政地址,最好有更多列,例如街道、城镇、门牌号等。然后,您可以享受在列上使用索引的优势。

You should add a child table with an Address column. You will have one to many relation where address is stored in a child table. You can add as many addresses per user as you want. Also you can add extra info like address type (home, work or primary, secondary ect.)

I wouldn't go for one column for address. If this is a postal address it is better to have more columns such as street, town, house number ect. Then you can have an advantage of using indexes on your columns.

能怎样 2024-10-08 05:33:50

您可以尝试这样的操作:

CREATE TABLE [dbo].[Person](
[PersonId] [int] IDENTITY(1,1) NOT NULL,
[FullName] [varchar](50) NULL,
CONSTRAINT [PK_Person] PRIMARY KEY CLUSTERED ([PersonId] ASC))
GO

CREATE TABLE [dbo].[Addresses](
[AddressId] [int] IDENTITY(1,1) NOT NULL,
[PersonId] [int] NOT NULL,
[AddressLine1] [varchar](50) NULL,
[AddressLine2] [varchar](50) NULL,
[City] [varchar](50) NULL,
[State] [varchar](4) NULL,
[Country] [varchar](50) NULL,
CONSTRAINT [PK_Addresses] PRIMARY KEY CLUSTERED ([AddressId] ASC))
GO

ALTER TABLE [dbo].[Addresses]  
WITH CHECK ADD  CONSTRAINT [FK_Addresses_Person] FOREIGN KEY([PersonId]) 
REFERENCES [dbo].[Person] ([PersonId])
GO

ALTER TABLE [dbo].[Addresses] CHECK CONSTRAINT [FK_Addresses_Person]
GO

当然,您可以将其设置为您想要的复杂程度,并遵循之前存储地址类型等的建议。

http://sqlserversamples.codeplex.com/ 并遵循他们的最佳实践。

You could try something like this:

CREATE TABLE [dbo].[Person](
[PersonId] [int] IDENTITY(1,1) NOT NULL,
[FullName] [varchar](50) NULL,
CONSTRAINT [PK_Person] PRIMARY KEY CLUSTERED ([PersonId] ASC))
GO

CREATE TABLE [dbo].[Addresses](
[AddressId] [int] IDENTITY(1,1) NOT NULL,
[PersonId] [int] NOT NULL,
[AddressLine1] [varchar](50) NULL,
[AddressLine2] [varchar](50) NULL,
[City] [varchar](50) NULL,
[State] [varchar](4) NULL,
[Country] [varchar](50) NULL,
CONSTRAINT [PK_Addresses] PRIMARY KEY CLUSTERED ([AddressId] ASC))
GO

ALTER TABLE [dbo].[Addresses]  
WITH CHECK ADD  CONSTRAINT [FK_Addresses_Person] FOREIGN KEY([PersonId]) 
REFERENCES [dbo].[Person] ([PersonId])
GO

ALTER TABLE [dbo].[Addresses] CHECK CONSTRAINT [FK_Addresses_Person]
GO

Of course you can have it as complex as you want and follow the previous advice for storing address types etc.

It might help to download MS examples from http://sqlserversamples.codeplex.com/ and follow their best practices.

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