简体   繁体   English

SQL:插入和查询组织数据列表的最佳方法是什么?

[英]SQL: What is the best way to insert and query a list of organized data?

This is a rather simple use case that I think there has got to be a "standard" solution for, but I can't find any examples. 这是一个相当简单的用例,我认为必须一个“标准”解决方案,但我找不到任何示例。

I am fairly new to SQL (just started learning), so maybe it is blindingly obvious. 我对SQL还是很陌生(刚刚开始学习),所以也许显而易见。

Say I have three arrays of data: 假设我有三个数据数组:

seconds: [0,  1,  2,  3,  4,  5, 6]
height:  [0, 10, 20, 25, 20, 10, 0]
speed:   [10,10,  5,  0, -5,-10, 0]  

I am going to be getting a whole bunch more height and speed arrays, but they all have the same second/timestamp. 我将获得更多的高度和速度数组,但它们都具有相同的秒/时间戳。

Questions: 问题:

  • what is the best way to create a table/tables to store this data? 创建一个或多个表来存储此数据的最佳方法是什么?
    • My current understanding is that I have to create a table who's columns are named "0", "1", "2", "3", etc -- is this correct??? 我目前的理解是,我必须创建一个表,其名为“ 0”,“ 1”,“ 2”,“ 3”等,这是正确的吗?
  • what is the best way to actually store this data using SQL commands? 使用SQL命令实际存储此数据的最佳方法是什么?
  • what is the best way to retrieve this data so that everything is in the same order I put it as? 检索此数据的最佳方法是什么,以使一切都与我输入的顺序相同?

Again, I think this question should be so easy it's stupid, but if something like this is NOT easy to do in SQL, I'm wondering if it is the data base I should be using. 再次,我认为这个问题应该很简单,这很愚蠢,但是如果在SQL中这样的事情不容易实现,我想知道它是否是我应该使用的数据库。

Thanks 谢谢

Thanks to binderbound for the answer 感谢黏合剂的答案

I was thinking of things from the viewpoint of something like Excel, where you would not want to ever store data downwards in column format. 我从Excel之类的角度考虑问题,您永远不想以列格式向下存储数据。 But it makes so much sense now because you have the power of sql to generate your queries to however you want them to be. 但这现在意义非凡,因为您具有sql的功能来生成查询,无论您希望查询是什么。 You can put your data like his answer suggests and it is easy to query it how you want it. 您可以按照他的答案所建议的那样放置数据,并且可以轻松查询所需的数据。

Thanks a ton! 万分感谢!

You don't have to create table's with columns 0, 1, 2, 3, etc. I would not recommend having six columns though. 您不必创建具有0、1、2、3等列的表。不过,我不建议有6列。 It's just bad schema design, even though it may work in your situation. 尽管这可能会在您遇到的情况下起作用,但这只是不良的架构设计。 Rather than store a whole trial in a row, each row should really only store an individual second. 而不是将整个试用版存储在一行中,每一行实际上应该只存储一个秒。 That way, adding more seconds in future is a fairly simple process 这样,将来增加更多的秒数是一个相当简单的过程

Following this principle, here is a rough schema 遵循此原理,这是一个粗略的方案

HeightSpeedEntry
_______________
TrialId   PK  int
Seconds   PK  int
Height    PK  int
Speed     PK  int

To create this table: 要创建此表:

CREATE TABLE HeightSpeedEntry(
TrialId int,
Seconds int,
Height int,
Speed int,
CONSTRAINT pk_trial_seconds PRIMARY KEY (TrialId, Seconds));

To insert data row by row 逐行插入数据

INSERT INTO HeightSpeedEntry(TrialId, Seconds, Height, Speed)
VALUES (1, 0, 0, 10)

INSERT INTO HeightSpeedEntry(TrialId, Seconds, Height, Speed)
VALUES (1, 1, 10, 10)

To retrieve a set of records 检索一组记录

SELECT * FROM HeightSpeedEntry
WHERE TrialId = 1
ORDER BY Seconds

How to actually pass the array into the insert statement in a non-manual fashion depends on where your array is coming from. 如何以非手动方式将数组实际传递给insert语句取决于数组的来源。

Just to re-clarify, what we're doing here is converting your records from this: 只是为了澄清一下,我们在这里所做的就是从中转换您的记录:

seconds: [0,  1,  2,  3,  4,  5, 6]
height:  [0, 10, 20, 25, 20, 10, 0]
speed:   [10,10,  5,  0, -5,-10, 0] 

to this: 对此:

HeightSpeedEntry: [1, 0, 0,  10 ]
HeightSpeedEntry: [1, 1, 10, 10 ]
HeightSpeedEntry: [1, 2, 20, 5  ]
HeightSpeedEntry: [1, 3, 25, 0  ]
HeightSpeedEntry: [1, 4, 20, -5 ]
HeightSpeedEntry: [1, 5, 10, -10]
HeightSpeedEntry: [1, 6, 0,  0  ]

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

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