What is the difference between Primary Key and Identity?

In SQL Server db, what is the difference between a primary key and an Identity column? A column can be a primary key without being indented. However, a column cannot be a person without being a primary key.

In addition to the differences, what does the PK column and Identity offer, that only the PK column does not work?

edit: Ouch. As indicated below and now verified by me, a column can be private without being PK. When I checked this, my column type was a string, and I could not set it to identity. Has the meaning.

+49
sql-server database-design
Nov 27 '10 at 19:20
source share
6 answers

A column can definitely be identical without being PK.

Identity is just a growth-increasing column.

A primary key is a unique column or columns that define a row.

The two are often used together, but there is no requirement that this be so.

+66
Nov 27 '10 at 19:24
source share

This answer relates more to the WHY identifier and the first key than WHAT they have been since Joe answered WHAT is correct above.

An identifier is a value that you control SQL. Authentication is a function of the string. It is sequential either increasing or decreasing in value, at least in SQL Server. It should never be changed, and spaces in the value should be ignored. Identity values ​​are very useful when linking table B to table A, since the value is never duplicated. Identity is not the best choice for a clustered index in each case. If the table contains audit data, a clustered index can be better created on the date it provides an answer to the question β€œwhat happened between today and four days ago” with less work, since the entries for the dates are sequential in the data pages.

The primary key makes a column or columns in a row unique. The Primay key is a column function. Only one priority key can be defined in any table, but several unique indexes can be created that mimic the primary key. Clustering a primary key is not always the right choice. Consider the phone book. If the phone book is grouped with the primay key (phone number), a request to return phone numbers to First Street will be very expensive.

The general rules that I use for identification and primary key are as follows:

  • Always use the identity column
  • Create a clustered index on the column or columns used in a range search
  • Keep the clustered index narrow as the clustered index is added at the end of every other index
  • Create a priority key and unique indexes to reject duplicate values
  • Narrow keys are better
  • Create an index for each column or columns used in joins

These are my general rules.

+8
Nov 28 '10 at 15:14
source share

A primary key (also known as a candidate key) is any set of attributes that have the properties of uniqueness and minimality. This means that the key column or columns are limited by uniqueness. In other words, the DBMS will not allow two rows to have the same set of values ​​for these attributes.

The IDENTITY property effectively creates an automatically incrementing default value for a column. This column does not have to be unique, so the IDENTITY column is not necessarily a key.

However, the IDENTITY column is usually intended to be used as a key, so it usually has a unique constraint to ensure that duplicates are not allowed.

+5
Nov 28 '10 at 11:20
source share

FEEDBACK EDITORS

The key is unique to the string. This is a way to define a string. Strings may not contain any, not one or several keys. These keys may consist of one or more columns.

Keys are indexes with a unique constraint. This distinguishes them from non-key indexes.

Any index with multiple columns is called a "composite index".

Traditionally, a primary key is seen as a master key that uniquely identifies a string. There can only be one of them.

Depending on the design of the table, the primary key may not be.

The primary key is simply the "primary key". This is the main one that defines the unique identifier of the string. Depending on the design of the table, this may be incorrect, and multiple keys express uniqueness.

In SQL Server, a primary key can be grouped. This means that the remaining columns are bound to this key at the index sheet level. In other words, once SQL Server found the key, it also found the string (to be clear, this is due to the clustered aspect).

An identity column is simply a method of creating a unique identifier for a row.

The two are often used together, but this is not a requirement.

+1
Nov 28 '10 at 7:16
source share

A primary key that emphasizes uniqueness and avoids the duplication value for all records in one column, while identification provides an increase in the number in a column without inserting data. Both functions can be on one column or on one.

0
Sep 10 '15 at 7:49
source share

You can use IDENTITY not only with integers, but also with any numeric data type that has a scale of 0

the primary key may have a scale, but it is not required.

IDENTIFICATION, combined with a PRIMARY KEY or UNIQUE constraint, allows you to provide a simple, unique string identifier

0
Sep 12 '17 at 6:15 on
source share



All Articles