简体   繁体   English

在SQL Server 2008中存储数组

[英]Store array in SQL Server 2008

I am developing a contact manager application using SQL Server 2008 (service-based database) . 我正在使用SQL Server 2008(基于服务的数据库)开发一个联系人管理器应用程序。 Most contacts have several emails or several phone numbers. 大多数联系人都有几封电子邮件或多个电话号码。 So is there a way to store an array as a datatype in SQL Server? 那么有没有办法在SQL Server中将数组存储为数据类型? Or does anyone have an alternative to this way? 或者有没有人可以选择这种方式?

You'll want to create separate tables, with a row per contact number or email address. 您需要创建单独的表,每个联系人号码或电子邮件地址都有一行。

CREATE TABLE Contacts (contactId int, name varchar(128), etc, etc
CREATE TABLE ContactEmail (contactId int, emailAddress varchar(128), etc
CREATE TABLE ContactPhone (contactId int, phoneNumber varchar(128), etc

This will allow you to modify individual numbers/emails, remove them, add them, etc, without requiring an external program to unpack an array. 这将允许您修改单个数字/电子邮件,删除它们,添加它们等,而无需外部程序来解压缩数组。

But if you really want to store it denormalized, you could transform the array into a delimited string. 但是如果你真的想将它存储为非规范化的,你可以将数组转换为分隔的字符串。 . put a delimiter between each email address (with the appropriate magic to make sure an address doesn't already contain the delimiter) then split it on the way back out. 在每个电子邮件地址之间放置一个分隔符(使用适当的魔法来确保地址不包含分隔符)然后在退出的路上拆分它。

No there isn't an array type in SQL. 不,SQL中没有数组类型。

You should create a phonenumbers table and store one phone number per row use and use a foreign key (called, for example, person_id) to refer to the person table. 您应该创建一个phonenumbers表并在每行使用时存储一个电话号码,并使用外键(例如,person_id)来引用person表。

One other option you can use is to store the contact information as an XML document in a XML type field in your table. 您可以使用的另一个选项是将联系人信息作为XML文档存储在表中的XML类型字段中。 I would only do this if I am not trying to search for people based on some part of that contact information. 如果我不是根据联系信息的某些部分搜索人员,我只会这样做。

For example if you are think you might be interested in finding all your contacts who have phone numbers with a specific area code then you will want to make that a separate table and relate it instead of storing it this way. 例如,如果您认为您可能有兴趣找到所有具有特定区号的电话号码的联系人,那么您将希望将其作为一个单独的表并将其关联而不是以这种方式存储。

I know that you can actually query the XML in SQL Server 2008, but it really depends on how many records you are going to have in your contact list as parsing all that XML on a lot of data will be very slow compared to having separate tables. 我知道您实际上可以在SQL Server 2008中查询XML,但它实际上取决于您在联系人列表中将有多少条记录,因为解析大量数据上的所有XML与单独的表相比将非常慢。

You can use a Table Variable, to use this you should use a DDL Create Table, but you need to write first the at (@). 您可以使用表变量,要使用它,您应该使用DDL创建表,但您需要先写入at(@)。 With this you have an Array.... but you CANNOT store this on a table because a table is an array it selft. 有了这个你有一个数组....但你不能将它存储在一个表上,因为一个表是一个它自己的数组。 I you really need to have a data structure inside of your table, you should use an XML. 我真的需要在表内部有一个数据结构,你应该使用XML。 As I understand since SQL Server 2000 has several features to work with XML. 据我所知,因为SQL Server 2000有几个功能可以使用XML。

Perhaps if you can tell me more details of your reasons to do this... I can give you back more ideas! 也许如果你能告诉我你做这个理由的更多细节......我可以给你更多的想法!

I hope my answear can help you 我希望我的衣服可以帮到你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM