什么是主键 [英] What is a PRIMARY KEY

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

问题描述

在数据库代码中,我看到了对 PRIMARY KEY 的引用.什么是主键?以及如果我的一个表中的某些列形成了PRIMARY KEY ...或没有形成PRIMARY KEY,会有什么区别.

In database code, I see references to a PRIMARY KEY. What is a PRIMARY KEY? and what difference does it make if some of the columns in one of my tables form a PRIMARY KEY... or don't form a PRIMARY KEY.

推荐答案

第一范式要求您具有唯一的键才能建立关系.如果不满足此要求(即,如果您没有唯一键),那么您的表将只是一个堆,而不是一个关系.

1st Normal Form requires that you have a unique key for you to have a relation. If this requirement is not met (i.e. if you have no unique keys), then your table will be just a heap, not a relation.

主键或多或少(即大致来说)是一个特别选择的唯一键.设计模式的人选择它.PK和唯一键之间的主要区别在于,唯一键可以包含NULL值,而PK不能.此外,给定表中可以有多个唯一键,但最多只能有一个PK.

The primary key is more or less (i.e. roughly speaking) one specially selected unique key. The one who designs the schema chooses it. The main difference between a PK and a unique key is that unique keys can contain NULL values while PKs cannot. Also, you can have more than one unique keys in a given table but at most one PK.

通过将唯一键之一作为主键,可以允许其他表通过其外键(FK)轻松指向该表.从技术上讲,(子表的)FK也可以指向任何唯一键(来自父表),但是通常人们为此目的而使用主键(PK),该主键(如前所述)基本上只是唯一键之一.这意味着FK通常指向PK.

By making one of the unique keys a primary key, you allow other tables to easily point to this table via their foreign keys (FKs). Technically the FKs (of the child tables) can also point to any unique key (from the parent table) but usually people use for that purpose the primary key (PK) which (as said) is basically just one of the unique keys. That means FKs usually point to PKs.

有关更多详细信息,请参见:

For more details, see also:

b/w主键有什么区别和唯一键

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

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