简体   繁体   English

sql 工作台中的复合主键与自动递增主键

[英]Composite primary keys vs auto increment primary key in sql workbench

I need the advice of someone who has a greeter experience.我需要有迎宾经验的人的建议。

I have an associative entity in my database, like that:我的数据库中有一个关联实体,如下所示:

Table2-> CustomerID, ServiceID, DateSub

Since the same customer (with PK, for example 1111) can require the same service (with PK, for example 3) more than once but never in the same date , the composite PK of Table 2 can't be just (CustomerID, ServiceID).由于同一个客户(有 PK,例如 1111)可以多次要求相同的服务(有 PK,例如 3)但不会在同一个日期,所以表 2 的复合 PK 不能只是 (CustomerID, ServiceID )。

Now I have 2 options:现在我有两个选择:

1- Also "DateSub" will be a primary key , so the PK of table 2 will be (CustomerID, ServiceID, DateSub) 1- "DateSub" 也是主键,所以表 2 的 PK 将是 (CustomerID, ServiceID, DateSub)

2- Create a specific PK for the associative entity (for example, Table2ID, and so CustomerID and Service ID will be FK) 2- 为关联实体创建一个特定的 PK (例如,Table2ID,因此 CustomerID 和 Service ID 将是 FK)

Which of the 2 approach would you follow and why?你会遵循这两种方法中的哪一种,为什么? Thank you谢谢

First of all you need to decide whether is it your requirement to make combination of CustomerID , ServiceID amd DateI column as unique?首先,您需要确定是否需要将CustomerIDServiceIDDateI列的组合设为唯一? If so then you should go for firt option.如果是这样,那么您应该使用 go 作为首选。

Otherwise I would go for second option.否则我会选择 go 作为第二个选项。

With first option if DateI is of date data type you will not be able to insert same service for a customer twice.如果 DateI 是日期数据类型,则使用第一个选项,您将无法为客户插入相同的服务两次。 If it's datetime then it's doable though.如果是日期时间,那么它是可行的。

If you want to use this primary key (composite primary key) in any other table as foreign key then you need to use all three columns there too.如果你想在任何其他表中使用这个主键(复合主键)作为外键,那么你也需要在那里使用所有三列。

I tend to prefer the PK be "natural".我倾向于更喜欢PK是“自然的”。 You have 3 columns that, together, can uniquely define each row.您有 3 列,它们一起可以唯一地定义每一行。 I would consider using it.我会考虑使用它。

The next question is what order to put the 3 columns in. This depends on the common queries.下一个问题是放置 3 列的顺序。这取决于常见的查询。 Please provide them.请提供它们。

An index (including the PK) is used only leftmost first.索引(包括 PK)仅在最左边使用。 It may be desirable to have some secondary key(s), for efficient access to other columns.可能需要一些辅助键,以便有效地访问其他列。 Again, let's see the queries.再次,让我们看看查询。

If you have a lot of secondary indexes, it may be better to have a surrogate, AUTO_INCREMENT "id" as the PK.如果你有很多二级索引,最好有一个代理, AUTO_INCREMENT “id”作为PK。 Again, let's see the queries.再次,让我们看看查询。

If you ever use a date range, then it is probably best to have DateSub last in any index.如果您曾经使用过日期范围,那么最好DateSub放在任何索引的最后。 (There are rare exceptions.) (有极少数例外。)

How many rows in the table?表中有多少行?

The table is ENGINE=InnoDB , correct?该表是ENGINE=InnoDB ,对吗?

Reminder: The PRIMARY KEY is a Unique key, which is an INDEX .提醒: PRIMARY KEY是唯一键,它是一个INDEX

DateSub is of datatype DATE , correct? DateSub是数据类型DATE ,对吗?

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

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