Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
858 views
in Technique[技术] by (71.8m points)

database - What is a 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

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.

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:

What is the difference b/w Primary Key and Unique Key


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...