mysql - 对于公交车时刻表,我应该为所有公交车准备一个数据表并将时间表存储在一列中,还是为每辆公交车创建单独的表?

标签 mysql database database-design database-schema

我只需要能够显示接下来几辆公交车的到达时间,不打算做任何旅行计划。

在单个条目中包含每条路线的所有时间对于数据库来说可能会更快,对吗?尽管这意味着任何正在拖延的时间表都需要得到全部,即使他们只需要一些。

哪种方式效率更高?

最佳答案

在最简单的形式中,您需要三张表:一张用于公交车,一张用于停靠站,一张用于时间表。

create table bus (
    bus_id int -- primary key
,   route_name varchar(64)
)

create table stop (
    stop_id int -- primary key
,   bus_id int references bus(bus_id)
,   address_or_intersection varchar(128)
)

create table timetable (
    time_entry_id int -- primary key
,   stop_id int references stop(stop_id)
,   run_id int
,   stop_time time
)

前两个表是不言自明的。下面是第三个表格的示例,该路线包含三个停靠点,早上跑一趟,晚上跑一趟:

bus:
bus_id    route_name
------    ----------
   1      red arrow

stop:
stop_id   bus_id   address
-------   ------   ---------
   1         1     Market St
   2         1     North Point
   3         1     Stockton

timetable:
time_entry_id   stop_id   run_id  stop_time
-------------   -------   ------  ---------
    1              1         1     06:15
    2              2         1     06:39
    3              3         1     07:05
    4              1         2     19:03
    5              2         2     19:30
    6              3         2     19:51

关于mysql - 对于公交车时刻表,我应该为所有公交车准备一个数据表并将时间表存储在一列中,还是为每辆公交车创建单独的表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9270852/

相关文章:

mysql - 如何在 MySQL 查询中获取给定月份的第 N 个工作日的日期?

mysql - 戈朗 : Mysql Prepare Insert statements do not add rows into db table

asp.net - 允许空值与默认值

mysql - Webapp : Mysql: Row level security. 优点/缺点?更好的方法吗?

python - 使用Boto3与RDS上的亚马逊极光交互

php - 如何在一个网页上连接多个 MySQL 数据库?

java - 插入数据, "MobileServiceTable must have a single id property defined"

php - 如何在 CodeIgniter 中一次运行多个迁移文件?

sql-server - 多态关联 - 总是不好吗?

mysql - 是否建议在数据库的所有表中使用文本字段作为用户 ID?