简体   繁体   English

主键的自动编号与文本字符串?

[英]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.CODEID的形式使用唯一 ID 是一种更安全的方法。 Avoiding the use of name allows you to:避免使用名称允许您:

  • Abstract the name from the identifier从标识符中提取名称
  • Store the name in a single location将名称存储在一个位置
  • Change the name, if required, in a single location如果需要,在单个位置更改名称
  • Use less disk-space and memory.使用更少的磁盘空间和内存。
  • Perform faster indexing, inserts, deletes, joins, sorts and group-bys.执行更快的索引、插入、删除、连接、排序和分组。

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:它是:

  • Numeric, so it's stored efficiently数字,所以它被有效地存储
  • Numeric, so it's fast to work with数字,因此使用起来很快
  • Guaranteed to be unique保证独一无二
  • New entries always insert at the end of a table and require minimal effort for page movements or index changes.新条目总是插入到表的末尾,并且需要最少的页面移动或索引更改工作。

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.

相关问题 MS Access-显示自定义主键自动编号 - MS Access - Display Custom Primary Key autonumber access:autonumber字段但不与主键并行 - access: autonumber field but not parallel with primary key jdbc插入到MS Access表中,该表的主键是自动编号 - jdbc INSERT into MS Access table whose primary key is an autonumber 通过ADOX添加带有自动编号主键的Access表 - Adding an Access table with an AutoNumber Primary Key via ADOX MS Access主键自动编号变得混乱 - MS Access Primary Key AutoNumber has become confused 如何在MS Access中执行查询时保留主键的自动编号? - How to retain the AutoNumber of a Primary Key when executing a query in MS Access? 使用自动编号作为后端ms Access数据库中的主键的表是否存在问题? - Are there issues with tables using an autonumber as a primary key in a back-end ms access db? 使用自动编号主键绑定到 MS Access 表的 DataGridView 导致并发错误 - DataGridView bound to MS Access table with Autonumber Primary key causes Concurrency errors 我有现有的表,并希望使自动编号成为一个表中的主键,而使另一个表中的外键。 有什么好办法? - I have existing tables and want to make Autonumber the Primary key in one table and Foreign key in the other table. What is a good way? 当长文本/备注字符串是主键字段或“已索引:是(允许重复)”时,将长文本/备注字符串截断为255个字符 - Truncates Long Text/Memo string to 255 characters when it is a primary key field or “Indexed: Yes (no-duplicates) allowed”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM