主键 [英] Primary Keys

查看:68
本文介绍了主键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大多数参考书都推荐使用autonum主键,但是

Access帮助说任何唯一键都能正常工作。


有什么权衡?


我有几个具有唯一字段的表。我可以将它们用作

主键还是应该定义一个autonum主键?


一张表中包含有关美国50个州的信息。表格

如下所示:


州议会大厦日期承认

Alabama Montgomery 1819年12月14日

阿拉斯加朱诺1959年1月3日

亚利桑那凤凰城1912年2月14日


由于州名是独一无二的,有没有理由不这样做

字段是否为主键?

另一个表与小学学校乘法表有关。这个

就是这样一个简单的表格:


因素答案

1x1 1

2x1 2

2x2 4

3x1 3

3x2 6

3x3 9

。 ..

12x1 12

12x2 24

...

12x12 144

同样,因子字段(文本字段)也是唯一的。是否有任何

的理由不使用它作为主键?


在这两种情况下,表都很小,所以添加另一个4字节的字段是

没什么大不了的,但是我想保持桌子尽可能简单

除非有一些缺点。


谢谢


-

在Win2000上运行MS Office 2000 Pro

Most of the reference books recommend autonum primary keys, but the
Access help says that any unique keys will work.

What are the tradeoffs?

I have several tables that have unique fields. Can I use them as
primary keys or should I define an autonum primary key?

One table has information about the 50 states in the US. The table
looks like this:

State Capitol Date Admitted
Alabama Montgomery December 14, 1819
Alaska Juneau January 3, 1959
Arizona Phoenix February 14, 1912

Since the state names are unique, is there any reason not to make that
field the primary key?

Another table has to do with grade school multiplication tables. This
is a simple table something like this:

Factors Answer
1x1 1
2x1 2
2x2 4
3x1 3
3x2 6
3x3 9
...
12x1 12
12x2 24
...
12x12 144

Here again, the Factors field (a text field) is unique. Is there any
reason not to use it as the primary key?

In both cases, the tables are small, so adding another 4-byte field is
no big deal, but I''d like to keep the tables as simple as possible
unless there is some downside.

Thanks

--
Running MS Office 2000 Pro on Win2000

推荐答案

在我看来,如果您向用户显示

主键或者您可能想要编辑该信息,那么您会遇到麻烦。由于

主键通常是你建立关系的方式,所以你不希望
希望它们在创建记录后永远改变。即使你认为关键价值不会发生变化,也可能会发生错别字。

此外,数字字段在数据库和主数据库中占用的空间更少钥匙,作为关系的来源,通常在许多

表中反复重复。


因此,我总是使用自动编号。其他意见各不相同。


HTH;


Amy


" LurfysMa" < in ***** @ invalid.invalidwrote in message

news:1f ************************* ******* @ 4ax.com ...
In my opinion, you''re asking for trouble if you ever show the user the
primary key or if you might ever want to edit that information. Since
primary keys are normally the way you establish relationships, you don''t
want them to ever change once a record has been created. Even though you
may think the key value won''t change, typos have been known to happen.
Also, number fields take up less space in the database and primary keys, as
the source of the relationship, are typically repeated over and over in many
tables.

Therefore, I always use autonumbers. Other opinions vary.

HTH;

Amy

"LurfysMa" <in*****@invalid.invalidwrote in message
news:1f********************************@4ax.com...

大多数参考书推荐autonum主键,但

访问帮助说任何唯一的密钥都可以工作。


有什么权衡?


我有几个具有唯一字段的表。我可以将它们用作

主键还是应该定义一个autonum主键?


一张表中包含有关美国50个州的信息。表格

如下所示:


州议会大厦日期承认

Alabama Montgomery 1819年12月14日

阿拉斯加朱诺1959年1月3日

亚利桑那凤凰城1912年2月14日


由于州名是独一无二的,有没有理由不这样做

字段是否为主键?

另一个表与小学学校乘法表有关。这个

就是这样一个简单的表格:


因素答案

1x1 1

2x1 2

2x2 4

3x1 3

3x2 6

3x3 9

。 ..

12x1 12

12x2 24

...

12x12 144

同样,因子字段(文本字段)也是唯一的。是否有任何

的理由不使用它作为主键?


在这两种情况下,表都很小,所以添加另一个4字节的字段是

没什么大不了的,但是我想保持桌子尽可能简单

除非有一些缺点。


谢谢


-

在Win2000上运行MS Office 2000 Pro
Most of the reference books recommend autonum primary keys, but the
Access help says that any unique keys will work.

What are the tradeoffs?

I have several tables that have unique fields. Can I use them as
primary keys or should I define an autonum primary key?

One table has information about the 50 states in the US. The table
looks like this:

State Capitol Date Admitted
Alabama Montgomery December 14, 1819
Alaska Juneau January 3, 1959
Arizona Phoenix February 14, 1912

Since the state names are unique, is there any reason not to make that
field the primary key?

Another table has to do with grade school multiplication tables. This
is a simple table something like this:

Factors Answer
1x1 1
2x1 2
2x2 4
3x1 3
3x2 6
3x3 9
...
12x1 12
12x2 24
...
12x12 144

Here again, the Factors field (a text field) is unique. Is there any
reason not to use it as the primary key?

In both cases, the tables are small, so adding another 4-byte field is
no big deal, but I''d like to keep the tables as simple as possible
unless there is some downside.

Thanks

--
Running MS Office 2000 Pro on Win2000



2006年7月12日星期三11:48:27 -0500,Amy Blankenship

< Am ******** @ magnoliamultimedia.comwrote:
On Wed, 12 Jul 2006 11:48:27 -0500, "Amy Blankenship"
<Am********@magnoliamultimedia.comwrote:

>在我看来,如果你向用户显示
主键
>In my opinion, you''re asking for trouble if you ever show the user the
primary key

$ b,你会遇到麻烦
$ b为什么只向用户显示主键有问题?


-

在Win2000上运行MS Office 2000 Pro

Why is merely showing the user the primary key a problem?

--
Running MS Office 2000 Pro on Win2000


大多数参考书推荐autonum主键,但
Most of the reference books recommend autonum primary keys, but the

访问帮助说任何唯一的密钥都可以工作。


有什么权衡?


我有几个具有唯一字段的表。我可以将它们用作

主键还是应该定义一个autonum主键?


一张表中包含有关美国50个州的信息。表格

如下所示:


州议会大厦日期承认

Alabama Montgomery 1819年12月14日

Alaska Juneau 1959年1月3日

亚利桑那凤凰城1912年2月14日


由于州名是独一无二的,有没有理由不做$>
表示主键?


另一个表与小学学校乘法表有关。这个

就是这样一个简单的表格:


因素答案

1x1 1

2x1 2

2x2 4

3x1 3

3x2 6

3x3 9

。 ..

12x1 12

12x2 24

...

12x12 144

同样,因子字段(文本字段)也是唯一的。是否有任何

的理由不使用它作为主键?


在这两种情况下,表都很小,所以添加另一个4字节字段

没什么大不了的,但是我想保持桌子尽可能简单

除非有一些缺点。


谢谢
Access help says that any unique keys will work.

What are the tradeoffs?

I have several tables that have unique fields. Can I use them as
primary keys or should I define an autonum primary key?

One table has information about the 50 states in the US. The table
looks like this:

State Capitol Date Admitted
Alabama Montgomery December 14, 1819
Alaska Juneau January 3, 1959
Arizona Phoenix February 14, 1912

Since the state names are unique, is there any reason not to make
that field the primary key?

Another table has to do with grade school multiplication tables. This
is a simple table something like this:

Factors Answer
1x1 1
2x1 2
2x2 4
3x1 3
3x2 6
3x3 9
...
12x1 12
12x2 24
...
12x12 144

Here again, the Factors field (a text field) is unique. Is there any
reason not to use it as the primary key?

In both cases, the tables are small, so adding another 4-byte field
is no big deal, but I''d like to keep the tables as simple as possible
unless there is some downside.

Thanks



我建议你花时间使用自己喜欢的搜索引擎

来表示像自然这样的术语vs代理主键。这样的搜索



可能列出一些优点和缺点,除了数小时的乐趣

;-)


基本上,有些人喜欢使用代理键(自动编号),其他人则需要使用自然键,这代表事物。有生意

意思,也可以是字段的组合。一些(包括

me)将根据要求使用两者。对于州,我可能

使用两个字母代码。


请注意 - 对于某些人来说这不仅仅是一个偏好问题,这是一种接近狂热的程度的宗教信仰。


如果你决定使用代理密钥(自动编号),那么

请记住,这不能确保数据的完整性!只有确保每条记录都有一个唯一的号码。

。在表格中说

你有一个唯一的字段,但是你决定为

主键添加一个自动编号字段,你还需要在它上面添加一个唯一的索引。 自然

键字段除了Autonumber

字段的主键索引外,

否则你将冒险欺骗。


- -

Roy-Vidar

I''d recommend you to take the time to use your favourite search engine
for the terms like "natural vs surrogate primary key". Such search
will
probably list some of the pros and cons, in addition to hours of fun
;-)

Basically, some favours usage of surrogate keys (Autonumber), others
favours natural keys, which represents "things" having a business
meaning, and which can also be a combination of fields. Some (including
me) will use both, based upon the requirements. For state, I''d probably
use the two letter code.

Just be aware - for some this isn''t just a matter of preference, it''s
religion to a degree thats close to fanatism.

Just be sure that if you decide upon surrogate key (Autonumber), then
remember that this will not ensure the integrity of your data! It will
only ensure that each record has a unique number. Say in a table where
you have a unique field, but you decide to add an Autonumber field for
primary key, you will need to also add a unique index on the "natural
key" field in addition to the primary key index on the Autonumber
field,
else you''ll risk dupes.

--
Roy-Vidar


这篇关于主键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆