What Is A Primary Key?

Primary keys are used to uniquely identify records in databases. They are used to make sure that every record has a unique identity.

This article should help you understand a little more about primary keys!

What Is A Primary Key

What Does Primary Key Mean?

A primary key is a unique identifier for every row in a table.

Primary keys help you identify individual rows in a table.

Natural or normal primary keys are those that come directly from observable data about your business entity.

Surrogate keys are arbitrary identifiers assigned by developers for convenience.

There can be only one primary key per table. Keys can be made up of any number of fields.

In some cases, an auto-incrementing integer field is automatically added to the end of the list of fields making up the key.

Primary keys are often used as identifiers for records. While most people know what an ID number is, few realize how many types of IDs there are.

A social security number is a personal identifier that is unique to each person. In addition, a street address or a driver’s license number is also a unique identifier.

Primary keys should be unique. They should be used to uniquely identify each row in a table.

This means that if you add another column to your primary key, then you must delete all existing rows before you can insert new ones.

When querying data, using the primary key uniqueness feature ensures one result.

Surrogate Keys

A surrogate key is a type of artificial key that does not represent anything real in the world.

They are typically generated by adding sequential integers to the end of other columns. For example, instead of having a customer ID, we could use a sequence number: 1, 2, 3, 4, 5…

Surrogate keys should be avoided when possible because they make queries much slower. There is no single correct choice for primary keys, but the most common choices are:

  1. An auto-incrementing integer (the rowid)
  2. A timestamp
  3. A UUID
  4. A random number generated by the database server
  5. A hash of data stored elsewhere
  6. A combination of any of these.

Alternate Key

A primary key must be unique. Typically, one candidate key is selected as the primary key.

All other candidate keys become alternate keys. Each alternate key must be unique. Only one candidate key may be used to join two different tables together.

What Are The Properties And Rules Of An SQL Primary Key?

Each key must be unique.

Keys are used to identifying rows. This is how you know which row belongs to which record.

You need to make sure your keys are unique. Keys should never contain nulls, always be longer than 900 characters, should never exceed the size limit, and should be made up of more than one column.

Good Practice For Primary Keys

Good Practice For Primary Keys

Numeric primary keys should be as small and as fast as possible. Don’t use passport numbers, social security numbers, or employee contracts numbers as your primary keys.

These numbers can change for real-world scenarios.

The data type of field name should be CHAR(6) if there is no need to store any other information about an agent.

The size of DECIMAL should be set to 2 because we want to store values ranging from 0.00 to 100.00. DECIMAL places should be set to 2 since we want to store values up to 15 digits.

Primary keys should be immutable – that’s why they should be deleted along with the associated records. Primary Keys should be an anonymous integer or numerical identifiers.

However, neither the relational model nor the SQL standards mandate this behavior.

Database and application designers need to exercise due diligence when deciding on the immutation of primary keys.

Primary Keys In SQL Server

You can create a unique index by clicking the Add Unique Constraint button. This button appears when you right-click the table and choose Design.

Selecting a row selector for the database field you want to use as the primary key causes the Create Index dialog box to appear. Choose an appropriate name for the index, then click OK.

Create Primary Key Using SSMS

Right-click on the column where you want to add a unique constraint. Click on Design, then choose Add Columns from the context menu.

In the dialog box, select Unique Checkbox and enter the name of your new column.

You can also specify whether this column should be nullable or non-nullable. Finally, check the Create Constraint option to create a unique constraint.

Finally, save the table by clicking on the save icon in the top toolbar.

You will see the primary key symbols before the columns for which you set as the primary key, such as ID, name, email, etc., as shown below.

Indexes And Constraints

Indexes are automatically created when primary keys and unique constraints are defined on table column names. Non-clustered indexes are automatically created if a unique constraint is defined on a table.

Clustered indexes are automatically generated if a primary key constraint is defined on a database table. A primary key must be unique, but a primary key does not need to be indexed.

Query Optimizer

Well-designed indexes can reduce I/O operations and consumption of system resources, thus improving query performance.

An index can help you retrieve information quickly by reducing the number of times you must read from the actual table.

Frequently Asked Questions

What Are The Uses Of Surrogate Primary Keys?

Since primary keys exist primarily as a convenience to the programmer, surrogate primary keys are often used, in many cases exclusively, in database application design.

What Are The ORM Rules?

In the ORM active record pattern, these additional restrictions are placed on primary keys: Primary keys should be immutable, that is, never changed or re-used; they should be deleted along with the associated record.

What Is The Difference Between A Primary And Alternate Key?

Alternate keys may be used as the primary key when doing a single-table select or when filtering in a where clause, but are not typically used to join multiple tables.

Can A Primary Key Have NULL Values?

No. A primary key can only contain UNIQUE values, and can’t have NULL values.


Primary keys are a useful thing to learn about in SQL. Hopefully, this article has helped you understand them better!

Albert Niall
Latest posts by Albert Niall (see all)