[英]Autonumber vs. Text String for primary key?
I'm trying to find the best solution to an issue I've just come across.我正在尝试为我刚刚遇到的问题找到最佳解决方案。 I hate doing things without understanding so I'm hoping someone can help.
我讨厌在不理解的情况下做事,所以我希望有人可以提供帮助。
I have an Access database with a table that stores Hotel Information - and then another table that stores Itineraries.我有一个 Access 数据库,其中包含一个存储酒店信息的表 - 然后是另一个存储行程的表。 The Itineraries table will select from the list of Hotels on the Hotels table.
行程表将从酒店表上的酒店列表中进行选择。
I want to make a proper relationship, but using an Autonumber primary key on the Hotels table that connects to the Hotels field on the Itineraries table won't work.我想建立适当的关系,但是在连接到 Itineraries 表上的 Hotels 字段的 Hotels 表上使用自动编号主键将不起作用。 (because the Autonumber ID doesn't match the hotel names.)
(因为自动编号 ID 与酒店名称不匹配。)
Is it better to:是否更好:
A. Use the Hotel name as the primary key on the hotels table, even though the string length may get pretty long? A. 使用酒店名称作为酒店表上的主键,即使字符串长度可能会很长?
B. Change the display control on the Hotels field on the Itineraries table to a combobox that lists the Hotels table autonumber primary key - but hides it. B. 将 Itineraries 表上 Hotels 字段的显示控件更改为列出 Hotels 表自动编号主键的组合框 - 但将其隐藏。 Instead it shows the column with the hotel names.
相反,它显示带有酒店名称的列。 I found that solution here: http://www.trigonblue.com/accesslookup.htm
我在这里找到了解决方案: http : //www.trigonblue.com/accesslookup.htm
Neither solution seems perfect as I think solution A may slow the indexing down with long text strings, and solution B gets messed up if new fields are inserted in the table.两种解决方案似乎都不完美,因为我认为解决方案 A 可能会用长文本字符串减慢索引速度,如果在表中插入新字段,解决方案 B 就会混乱。
I'd hate to pick the wrong answer here and have problems down the road.我不想在这里选择错误的答案,并在路上遇到问题。
Can anyone help me out here?有人可以帮我吗? Please let me know if I need to clarify any part of my question.
如果我需要澄清我的问题的任何部分,请告诉我。
Thanks!谢谢!
You should almost never use a name as a Primary Key.您几乎不应该使用名称作为主键。 Using a Unique ID in the form of a
CODE
or ID
is a much safer approach.以
CODE
或ID
的形式使用唯一 ID 是一种更安全的方法。 Avoiding the use of name allows you to:避免使用名称允许您:
Sometimes you'll have a code or ID already, or you're constrained by an internal/external rule, but most of the time an AutoNumbered Primary Key is very useful.有时您已经有了代码或 ID,或者您受到内部/外部规则的约束,但大多数时候自动编号的主键非常有用。 It is:
它是:
Auto-number is the most efficient way to set up a Primary Key, it is the least work for a DBMS to search through to find what it's looking for.自动编号是设置主键的最有效方法,它是 DBMS 搜索以找到所需内容的最少工作。 This is especially true if you are going to have Primary/Foreign key relationships in your tables.
如果您要在表中具有主键/外键关系,则尤其如此。
Not to mention, there are advantages to doing it this way for storage purposes and indexing purposes (not a big deal on Access, but on others it would be).更不用说,这样做有利于存储目的和索引目的(对 Access 来说没什么大不了的,但对其他人来说却是)。
With the current fast computers and For a few tens or may be hundreds of records its of no apperant difference to use a text or numeric PK, but certainly for many thousands of records AND ABOVE then the issue will be different, numeric is the friend of the CPU , because it is the easiest data type to be worked with by the processor.使用当前快速的计算机和对于几十或可能是数百条记录使用文本或数字PK没有明显区别,但对于数千条记录和以上肯定会有不同的问题,数字是朋友CPU ,因为它是处理器最容易使用的数据类型。 If I suppose that a table will have many thousands of records then I shull use Neumeric and preferably of a long type.
如果我假设一个表将有数千条记录,那么我将使用 Neumeric 并且最好使用 long 类型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.