简体   繁体   English

数据库设计结构

[英]Database design structure

I´m new to database design and never took class on it, i have problem with structuring my database and assigning primary keys. 我是数据库设计的新手,从不上课,我在构建数据库和分配主键时遇到了问题。

I have a list of cities, each city has 5 types of public transport. 我有一个城市名单,每个城市有5种类型的公共交通工具。 Each type of public transport has different ticket price, main station and CSV file with route coordinations etc. in every city. 每种类型的公共交通工具在每个城市都有不同的票价,主站和CSV文件以及路线协调等。 Then i need to daily calculate average cost of transportation in every city for each type of public transport based on route coordinations (distances), price, time it takes etc. 然后我需要每天根据路线协调(距离),价格,花费的时间等计算每种城市公共交通的平均运输成本等。

Table cities : cities

city (Primary key)

Table public transport : public transport

city, type of transport, ticket price, main station, file1, file2

Table results : results

city, type of transport, date, cost

How should i connect these tables (assuming their structure is right)? 我应该如何连接这些表(假设它们的结构是正确的)? In table public transport , i think city should be foreign key but type of transport will repeat for every city so i dont think it can be primary key of this table - the same for table results . 在表public transport ,我认为city应该是外键,但是每个城市都会重复type of transport所以我不认为它可以成为这个表的主键 - 对于表格results

The main idea is that you don't wish to repeat ya self. 主要的想法是你不想重复自我。 Not only is it an overhead but also it's quite error prone when you wish to change multiple entries that represent the same thing. 当您希望更改代表相同内容的多个条目时,它不仅是开销,而且非常容易出错。

There are guidelines on database normalization which help you to ensure that your data is on a form that's easy to maintain and work with. 有关数据库规范化的 指导原则可帮助您确保数据位于易于维护和使用的表单上。

You don't need to become an expert in understanding which form does what, but being able to identify what should be kept separated is a must when it comes to database designing. 在数据库设计方面,您无需成为了解哪种形式可以做什么,但能够确定应该保持分离的内容的专家。

You should list what you know: 你应该列出你所知道的:

  • Different cities. 不同的城市。
  • Different type of transport. 不同类型的运输。
  • Different ticket prices. 票价不同。
  • Different stations. 不同的站。

If you create a separate table for all of those then it'll be easy to link them together in rows in a table that then represents something on a larger scale. 如果为所有这些创建一个单独的表,那么很容易将它们在表中的行中链接在一起,然后表示更大规模的内容。 Every entry should have a separate id that will be your primary key, you need to be able to allow eg multiple cities with the same name, thus not being able to hold a unique value if they are to be the primary key. 每个条目都应该有一个单独的ID作为您的主键,您需要能够允许例如多个具有相同名称的城市,因此如果它们是主键,则无法保留唯一值。

Eg now it would be easy to identify routes for a city, there can be multiple routes in a city 例如,现在可以很容易地识别城市的路线,城市中可以有多条路线

route_id | city_id | route_name
 1          2         test1
 2          2         test2

You then could add another table that represent which kind of transport is tied with this specific kind of route. 然后,您可以添加另一个表,表示哪种传输与此特定路径绑定。

route_id | transport_id
 1          3  
 2          4

You're then able to create a new table that holds points of stations that are a part of your route and you can even identify whether it's a main route or not. 然后,您就可以创建一个新表,其中包含作为路线一部分的站点,您甚至可以确定它是否是主要路线。

route_connected_id | route_id | station_id | main_route
 1                    1          2            1        // a main route
 2                    1          3            0        // not a main route

And it goes on and on, separating the most simple entries allows you to create complex relationships where all you have to do is link ids. 它一直在继续,分离最简单的条目允许您创建复杂的关系,您需要做的就是链接ID。

This is the basic idea which should hopefully get you started, whether you find it helpful or not then I recommend that you take a look on the reading material that I suggested, ie database normalization. 这是一个基本的想法,应该有希望让你开始,无论你发现它是否有用,然后我建议你看看我建议的阅读材料,即数据库规范化。

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

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