简单的MySQL数据库问题

发布于 2024-07-17 13:11:40 字数 174 浏览 6 评论 0原文

我对数据库很陌生,我有一个简单的问题。

如果我有这些字段,我将如何设计我的 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 技术交流群。

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

发布评论

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

评论(4

憧憬巴黎街头的黎明 2024-07-24 13:11:40

您的第一个表可能称为“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).

日暮斜阳 2024-07-24 13:11:40

从简洁的描述中很难看出您要做什么,但第三范式规定任何列都应该依赖于:

  • 键。
  • 整个钥匙。
  • 除了钥匙什么也没有。

为此,我想说我的初步分析将生成:

Location
    LocId primary key
    Lat
    Long
Events
    LocId foreign key Location(LocId)
    Date
    Time

这是基于我的(可能有缺陷的)分析,您希望存储可能发生零个或多个事件的位置。

将事件放在单独的表中是一种很好的做法,因为替代方案是使用列数组,这绝不是一个好主意。

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:

  • the key.
  • the whole key.
  • nothing but the key.

To that end, I'd say my initial analysis would generate:

Location
    LocId primary key
    Lat
    Long
Events
    LocId foreign key Location(LocId)
    Date
    Time

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.

千里故人稀 2024-07-24 13:11:40

据我猜测,约会时情侣总是在一起出现。 在这种情况下,我会建议两张桌子、地点和时间。

CREATE TABLE location (
  id INT  NOT NULL,
  lat FLOAT  NOT NULL,
  long FLOAT  NOT NULL,
  PRIMARY KEY (id)
)


CREATE TABLE time (
  id INT  NOT NULL,
  locationid INT  NOT NULL,
  date DATE  NOT NULL,
  time DATE  NOT NULL
)

您可以选择添加外键约束

ALTER TABLE time ADD CONSTRAINT location_fk_constraint FOREIGN KEY location_fk_constraint (locationid)
    REFERENCES location (id)
    ON DELETE CASCADE
    ON UPDATE CASCADE;

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.

CREATE TABLE location (
  id INT  NOT NULL,
  lat FLOAT  NOT NULL,
  long FLOAT  NOT NULL,
  PRIMARY KEY (id)
)


CREATE TABLE time (
  id INT  NOT NULL,
  locationid INT  NOT NULL,
  date DATE  NOT NULL,
  time DATE  NOT NULL
)

Optionally you can add a foreign key constraint

ALTER TABLE time ADD CONSTRAINT location_fk_constraint FOREIGN KEY location_fk_constraint (locationid)
    REFERENCES location (id)
    ON DELETE CASCADE
    ON UPDATE CASCADE;
嘦怹 2024-07-24 13:11:40

好吧,为了便于讨论,我们假设您正在谈论经度和纬度。 这些是某种经度和纬度列表(也许是航海日志?啊,我的朋友们!)中的条目。 并且这些长/纬度对中的每一个都可能在列表中出现多次。

也许您想建立一个数据库来计算每个长/纬度对出现的次数以及每次出现的时间?

那么这是怎么回事:首先我们有一个长/纬度对的表,我们将为每一个提供一个 ID。

ID  long   lat
--  -----  -----
1   11111  22222
2   33333  44444
3   55555  66666

接下来,我们将有另一个表,它将为长/纬度对的每次出现分配一个日期/时间:

ID  date     time
--  ----     -----
1   1/1/1900 12:30
1   2/2/1900 12:31
1   3/2/1900 12:30
2   1/1/1930 08:21

假设您将第一个表称为“longlat”,将第二个表称为“<代码>外观”。

您可以通过执行以下操作找到单个长/纬度对的所有出现:

SELECT date,time FROM appearances 
   LEFT JOIN longlat ON appearances.ID=longlat.ID 
   WHERE longlat.long = 11111 AND longlat.lat = 22222

您可以通过执行以下操作来计算在经度 11111 发生某事的次数:

SELECT count(ID) FROM appearances 
   LEFT JOIN longlat ON appearances.ID=longlat.ID 
   WHERE longlat.long = 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.

ID  long   lat
--  -----  -----
1   11111  22222
2   33333  44444
3   55555  66666

Next, we'll have another table, which will assign each appearance of the long/lat pairs a date/time:

ID  date     time
--  ----     -----
1   1/1/1900 12:30
1   2/2/1900 12:31
1   3/2/1900 12:30
2   1/1/1930 08:21

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:

SELECT date,time FROM appearances 
   LEFT JOIN longlat ON appearances.ID=longlat.ID 
   WHERE longlat.long = 11111 AND longlat.lat = 22222

You could count how many times something happened at a longitude of 11111, by doing:

SELECT count(ID) FROM appearances 
   LEFT JOIN longlat ON appearances.ID=longlat.ID 
   WHERE longlat.long = 11111

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!

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