简体   繁体   English

基于数据库ID的引用

[英]database id based referencing

I have a table called branch which will store the details of a branch office like its manager, location etc. The fields are: 我有一个名为branch的表,该表将存储branch的详细信息,例如其经理,位置等。这些字段是:

branch_id
branch_manager
branch_location

Data in this table would look like: 该表中的数据如下所示:

1    Mr. John Doe      Manhattan
2    Miss. Jane Doe    Utah
3    Mr. Random Guy    Somewhere

There is another table called services which stores all service details. 还有一个称为services表,用于存储所有服务详细信息。 The fields are: 字段是:

service_id
service_name

Data would be like: 数据如下:

1   New Sales
2   After-Sales Support
3   Replacement
4   Exchange Offer

Now I want to store the services available in each branch. 现在,我想在每个分支中存储可用的服务。 Normally I guess it is done by creating a new table called branch_services which will contain branch_id and service_id . 通常,我猜这是通过创建一个名为branch_services的新表来完成的,该表将包含branch_idservice_id So if I have following record in branch_services 因此,如果我在branch_services有以下记录

branch-id  service-id
1          2
1          3
2          1

This means that the Manhattan branch does After-Sales Support and Replacement, while the Utah branch does only New Sales. 这意味着曼哈顿分公司进行售后支持和更换,而犹他州分公司仅进行新销售。

Now my question is - instead of having this branch_services table, what if I stored a comma separated list of services in the branch table itself? 现在我的问题是-如果不将这个branch_services表存储在分支表本身中,该怎么办呢?

1    Mr. John Doe      Manhattan    2,3
2    Miss. Jane Doe    Utah         1
3    Mr. Random Guy    Somewhere

What are the main arguments against this? 对此有哪些主要论点? Is it that queries would be more difficult/slower to execute (eg if I wanted to see which all branches did a particular service)? 是不是查询会更困难/执行起来更慢(例如,如果我想查看哪个分支执行了特定服务)? Is there any advantage by structuring it comma separated? 通过将其用逗号分隔是否有任何优势?

You can store your data in the way that you prefer (ie you can have your text fields in Cyrillic alphabet) but you have also to consider what you want to do with your data (ie you want to store English words and you and your colleagues don't know Cyrillic alphabet). 您可以按照自己喜欢的方式存储数据(即,您可以使用西里尔字母来表示文本字段),但也必须考虑要对数据进行什么处理(即,您要存储英文单词,然后您和您的同事不知道西里尔字母)。

If you are working with a relational database, I suspect you are going to use SQL. 如果您正在使用关系数据库,我怀疑您将使用SQL。 Now I can write a query like: 现在,我可以编写一个查询,例如:

select b.branch_location,
       s.service_name
  from branch b
inner join branch_services b_s 
        on b.branch_id = b_s.branch_id
inner join services s
        on b_s.service_id = s.service_id

To obtain: 获得:

branch_location service_name 
Manhattan       After-Sales Support 
Manhattan       Replacement 
Utah            New Sales

Because SQL is designed to query a RDMS and in our case the relationship is between the content of the columns branch_id in branch and branch_services (or service_id in branch_services and services ). 由于SQL是设计来查询关系数据库管理系统,并在我们的例子中关系是列的内容之间branch_idbranchbranch_services (或service_id为branch_servicesservices )。

Of course, we can understand that 2,3 mean the same than 2 and 3 in two different rows, but SQL is not able to understand that. 当然,我们可以理解在两个不同的行中2,3含义与23相同,但是SQL无法理解。 [Actually what we do is to split the 2,3 string in two values and associate each of them to a row in the services tables. [实际上,我们要做的是将2,3字符串分成两个值,并将每个值关联到services表中的一行。 Not that simple to do it in pure SQL.] 在纯SQL中做到这一点并不那么简单。]

Your question is about performance, before to get there the real question is "Can I query such kind of structure with SQL?" 您的问题是关于性能的,真正的问题是“我可以用SQL查询这种结构吗?” Yes and no. 是的,没有。

If to get a list of branches and services, like my query above, can be quite complicated, to get the branch that does a specific service is still doable, after all if need to check who does New Sales you need only to see if in your services list there is a 1, but exclude all 10, 11, 12 and so on. 如果要获得分支机构和服务的列表(例如上面的查询)可能非常复杂,那么获得特定服务的分支机构仍然可行,毕竟,如果需要检查谁进行了New Sales ,则只需查看是否在您的服务列表中有1,但不包括所有10、11、12,依此类推。 Uhm... not that straight forward. 嗯...不是那么直接。

What I'm trying to show you is that the three tables structure is more flexible, if you need to use it with SQL and not with a human brain. 我想向您展示的是,如果您需要将其用于SQL而不是人脑,则三表结构将更加灵活。 Unfortunately computers are not yet that smart. 不幸的是,计算机还不那么聪明。

So: 所以:

Is it possible to store data comma-separated in a column? 是否可以在列中存储逗号分隔的数据? Yes . 是的

Is a good idea? 是个好主意吗? Usually No if you want to use it in a relationship with another table. 如果要在与另一个表的关系中使用它,通常为No。

Usually? 通常? If they need just to expose the long comma-separated string, then yes. 如果他们只需要公开用逗号分隔的长字符串,则可以。

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

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