简单的MySQL数据库问题
我对数据库很陌生,我有一个简单的问题。
如果我有这些字段,我将如何设计我的 MySQL 数据库:
ID、 纬度, 长的, 日期 - 多个日期, 时间 - 多次
我知道我应该把它放入两个表中,对吗? 那两张桌子看起来怎么样?
谢谢!
I'm very new to databases and I have a quick question.
How would I design my MySQL database if I have these fields:
ID,
lat,
long,
date - multiple dates,
time - multiple times
I know I should put it into two tables, right? And how would those two tables look?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您的第一个表可能称为“location”,它将有一个“id”列作为其主键,以及两个名为“latitude”和“longditude”的列(可以是 varchar 或数字类型,具体取决于您的应用程序的需求)。 您的第二个表可能称为“location_event”,它可能有一个“id”列作为其主键,以及一个名为“location_id”的外键列,该外键列是对“location”表主键的引用。 该“location_event”表还将具有“日期”列和“时间”列(分别为日期和时间类型)。
Your first table might be called "location" and it would have an "id" column as its primary key, along with two columns called "latitude" and "longditude" (which could be varchar or a numeric type, depending what your application requires). Your second table might be called "location_event" and it could have an "id" column as its primary key, along with a foreign key column called "location_id" that is a reference to the primary key of the "location" table. This "location_event" table would also have a "date" column and a "time" column (of types date and time respectively).
从简洁的描述中很难看出您要做什么,但第三范式规定任何列都应该依赖于:
为此,我想说我的初步分析将生成:
这是基于我的(可能有缺陷的)分析,您希望存储可能发生零个或多个事件的位置。
将事件放在单独的表中是一种很好的做法,因为替代方案是使用列数组,这绝不是一个好主意。
It's hard to tell what you're trying to do from the terse description but third normal form dictates that any column should be dependent on:
To that end, I'd say my initial analysis would generate:
This is based on my (possibly flawed) analysis that you want to store a location at which zero or more events can happen.
It's good practice to put the events in a separate table since the alternative is to have arrays of columns which is never a good idea.
据我猜测,约会时情侣总是在一起出现。 在这种情况下,我会建议两张桌子、地点和时间。
您可以选择添加外键约束
As far as I can guess the date en time are couple always appearing together. In that case I would suggest two tables, location and time.
Optionally you can add a foreign key constraint
好吧,为了便于讨论,我们假设您正在谈论经度和纬度。 这些是某种经度和纬度列表(也许是航海日志?啊,我的朋友们!)中的条目。 并且这些长/纬度对中的每一个都可能在列表中出现多次。
也许您想建立一个数据库来计算每个长/纬度对出现的次数以及每次出现的时间?
那么这是怎么回事:首先我们有一个长/纬度对的表,我们将为每一个提供一个 ID。
接下来,我们将有另一个表,它将为长/纬度对的每次出现分配一个日期/时间:
假设您将第一个表称为“
longlat
”,将第二个表称为“<代码>外观”。您可以通过执行以下操作找到单个长/纬度对的所有出现:
您可以通过执行以下操作来计算在经度 11111 发生某事的次数:
希望有帮助! 我得承认,尝试猜测人们的意思确实很烦人……以后试着让自己更清楚,你会发现你得到的帮助会更有用、更简洁、更有针对性。你需要什么。
祝你好运!
OK, let's say, for the sake of argument, that you are talking about longitude and latitude. That these are entries in some kind of list (perhaps a sea log? Arrgh, me maties!) of longitude and latitude. And that each of these long/lat pairs may appear more than once in the list.
Perhaps you want to build a database that figures out how many appearances each long/lat pair has, and when each appearance happened?
So how's this: First we have a table of the long/lat pairs, and we'll give each of those an ID.
Next, we'll have another table, which will assign each appearance of the long/lat pairs a date/time:
Let's say you'll call the first table "
longlat
" and the second one "appearances
".You could find all the appearances of a single long/lat pair by doing something like:
You could count how many times something happened at a longitude of 11111, by doing:
Hope that helps! I gotta admit, it's really quite annoying to try and guess what people mean... Try making yourself more clear in the future, and you'll see that the help you'll get will be that much more useful, concise and targeted at what you need.
Good luck!