question

Steve Jones - Editor avatar image
Steve Jones - Editor asked

What is a primary key?

What is a primary key? Is it a type of index? How do I create one in SQL Server?

database-designprimary-key
1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Matt Whitfield avatar image Matt Whitfield ♦♦ commented ·
I bet £50 that David P has something to say :)
0 Likes 0 ·
JustABitOfCode avatar image
JustABitOfCode answered

A PrimaryKey of a table uniquely identifies each record.

In SQLServer, when you create a Primary Key, it's implemented as a unique clustered index on the column or columns (if there's not already a clustered index on the table and you don't specify a unique nonclustered index).

Once inplace, it means that inserts or updates that would create non-unique records are rejected. In SQLServer, primary key column's cannot allow NULL values.

In the table designer, you can create one by highlighting the column or columns and clicking on the key symbol in the toolbar.

Or you can use TSQL's CREATE TABLE statement which is described here

1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Matt Whitfield avatar image Matt Whitfield ♦♦ commented ·
No - Firstly, you don't create primary keys by using create index, you use ALTER TABLE ... ADD CONSTRAINT [name] PRIMARY KEY Secondly, while the UI in SSMS defaults a primary key to a clustered one, there is no reason that a PK must be clustered. Thirdly, a clustered primary key it is not a unique clustered index. It's a clustered primary key constraint. The two, while being operationally similar, are not the same thing.
2 Likes 2 ·
Kev Riley avatar image
Kev Riley answered

Strictly speaking a Primary Key is a CONSTRAINT, not an index.

However when you specify one, SQL Server implements the uniqueness by creating a UNIQUE INDEX for the columns that you have specified for the primary key. This differs from a unique constraint in the fact that the columns in a PK are not allowed to be nullable [whereas a unique constraint will allow one and only one null]

As already noted, by default SQL will attempt to make the PK a clustered index, but you can specify it to be non-clustered.

Having a PK allows a FOREIGN KEY to reference the table (although UNIQUE INDEX or UNIQUE CONSTRAINT also allow this)

You can only have one PK per table.

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

David 1 avatar image
David 1 answered
  1. A primary key is any candidate key.

  2. "PRIMARY KEY" is also the name of a constraint which may optionally be used to enforce uniqueness of some columns in a SQL table. This need not be the same thing as 1.

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Pranjali13 avatar image
Pranjali13 answered

Primary Key is a unique value that is used to identify a row in a table. The primary key can be anything in the table which is unique for a row. Here we can take the phone no also a Primary key because it is unique for every person.

read more about primary key and difference between foreign key and primary key on https://www.scaler.com/topics/difference-between-primary-key-and-foreign-key/#what-is-primary-key-

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.