Custom Search
 


Primary Key Constraint and Unique Constraint


Primary Key Constraint is defined by creating a primary key on a table. The value in primary key must uniquely identify each row in the table. Primary Key Constraint enforces row integrity.

Consider the following facts when defining a primary key:

  • When you create a primary key on a table, a unique index (and hence unique constraint) is automatically and implicitly created which enforces uniqueness of data in this column.

  • Primary key column cannot contain NULL values. Database engine implicitly creates a NOT NULL constraint on the primary key column.

  • A table can only have one primary key defined on it.

  • A primary key can be formed by a single column or a composition of multiple columns (i.e. composite primary key).

  • If you insert a duplicated record in primary key, you'll get an error.

What is artificial primary key?

There are two types of primary key: artificial primary key and natural primary key.

Artificial primary key is an integer number that is system auto-generated, auto-incremented, and maintained by MySQL database engine. It is the preferred way of defining and creating primary key. Artificial primary key is also known as surrogate primary key.

In MySQL, when we define a primary key as auto_increment, it will automatically increment by 1 every time when we add a new record into the table. This number is just an integer number and have no meanings whatsoever - you can call it stupid key.

Defining primary key:
CategoryID as artificial primary key

Data in primary key column CategoryID:
CategoryID data as artificial primary key

What is natural primary key?

As its name suggests, natural primary key takes the natural format of the data and often has business meanings associated with it. Natural primary key is also known as intelligent primary key.

For example, primary key CustomerID in customers table is a natural primary key. It uses a string of 5 characters which is abbreviated from the customer's company name.

Natural primary key:
CustomerID data as natural primary key

Why we should use an artificial primary key

From the example showing above for CustomerID, the drawback of using natural primary key is obvious. First, the primary key is created by shortening the company name. It's rather cumbersome to come up with a unique short name for a customer. Second, it's not flexible and is subject to business requirement changes.

  • Integer values are independent of the business requirements which often change from time to time.

    This is actually the disadvantage of using a natural primary key. When an organization goes thru functional changes, so do the primary key values. Artificial primary key does not suffer from this problem because integer values don't have business meanings and are not associated business changes.

  • Using integer values as primary key can often improve query performance.

To optimize the Customers table, we can create a new integer type CustomerID column in customers table as artificial primary key and remove the previous varchar type CustomerID. We then renamed the old varchar type CustomerID column to CustomerAbbr (if we still want to keep this column) which is short for customer abbreviation. We also added unique index to CustomerAbbr column.

Artificial primary key:
Add artificial primary key

Dis-advantages of using artificial primary key:

The only disadvantage I can think of is when we retrieve data from tables by using joins, we'll have to join more tables than we need. The extra joins come from the lack of natural key in the foreign key tables.

The following two sets of screenshots illustrate how extra joins can occur.

Table 1: Use extra join to get CustomerID

CustomerID below is integer data type and so it's artificial primary key. To retrieve order information and CustomerAbbr data, we need to join the two tables together.

select b.OrderID, a.CustomerAbbr, b.OrderDate, b.RequiredDate
from customers as a
inner join orders as b on a.CustomerID=b.CustomerID

Customers table:
Disadvantage artificial primary key
Orders table:
Add artificial primary key

Table 2: No join is used to get CustomerAbbr

Here natural key Customer abbreviation is used as CustomerID and because it's used as foreign key in orders table, when we retrieve order information and Customer abbreviation data, we don't need to join the two tables together.

select OrderID, CustomerID as CustomerAbbr, OrderDate, RequiredDate
from orders

Disadvantage artificial primary key
Add artificial primary key

The queries in Table 1 and Table 2 return exactly the same result but Table 1 used JOIN but Table 2 didn't.

Unique Constraint

Unique Constraint defines that values in a column must be unique. No duplicate values are allowed in the column. Unique Constraint enforces row integrity.

Unique Constraint is created on a column when you want to guarantee that data in this column must be unique. For example, a unique constraint is created on CategoryName column in categories table. This is achieved by creating a unique index on CategoryName column.

Create unique constraint

Considerations when creating unique constraint:

  • Unique constraint is automatically created when you define a column as primary key.

  • Unique constraint can be created on one or more columns.

  • When on multiple columns, the combination of data on these columns must be unique. For example, we created a unique index on OrderID and ProductID column in order_details table.

    Create composite unique constraint

  • Unique constraint is created implicitly by defining a unique index on the column(s).

  • NULL value is allowed by unique constraint.

  • One table can have more than one unique constraint.

On next page, we will look into details of Creating foreign key relationships and considerations.





Other tutorials in this category

1. How to Design Relational Database

2. Enforce Data Integrity by Database Constraints

3. How to Enforce Data Type Constraint

4. How to Enforce Default Constraint and Nullability Constraint

5. Foreign Key Relationships and Considerations

Back to Tutorial Index Page


Copyright © 2024 GeeksEngine.com. All Rights Reserved.

This website is hosted by HostGator.

No portion may be reproduced without my written permission. Software and hardware names mentioned on this site are registered trademarks of their respective companies. Should any right be infringed, it is totally unintentional. Drop me an email and I will promptly and gladly rectify it.

 
Home | Feedback | Terms of Use | Privacy Policy