在SQL Server中存储DateTime值数组的最简单方法 [英] Simplest way to store an array of DateTime values in SQL Server

查看:153
本文介绍了在SQL Server中存储DateTime值数组的最简单方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在SQL Server中存储DateTime值数组的最简单方法是什么?

数组的长度当然可以有所不同。



我想到的一个选项是使用xml列...其他建议?

What is the simplest way to store an array of DateTime values in SQL Server?
The length of the array can vary of course.

One option I thought of is using an xml column... Other suggestions?

推荐答案

它是一个数组。所以使用两个表。



在第一个表中标记日期时间值的id。

在第二个表中,重复id和将数组项逐个存储。

Its an array. So use two tables.

In the first table mark the id of the datetime values.
In the second table, repeat the id and store the array items as rows one by one.
Table 1
ColA ColB DateId
..   ..   1
..   ..   2
...

Table 2
DateId DateValue
1      Date1
1      Date2
2      Date3
2      Date4
2      Date5
....


a中的多值列关系数据库永远不是一个好主意。

推荐的方法是在原始表中为记录创建一个单独的表,其中包含日期时间字段和引用字段。

Multi-valued columns in a relational database is never a good idea.
The recommended way is to have a separate table with a datetime field and a reference field to the record in the original table it belongs to.
CREATE TABLE ORIGINAL (
   OriginalId int NOT NULL IDENTITY PRIMARY KEY
  ,--
)

CREATE TABLE YOURDATETIMES (
   YourDateTimeId int NOT NULL IDENTITY PRIMARY KEY
  ,OriginalId int NOT NULL REFERENCES Original.OriginalId
  ,Value datetime2 NOT NULL
)


这篇关于在SQL Server中存储DateTime值数组的最简单方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆