Udemy logo

mysqluniquekeyIn the popular open-source relational database management system (RDBMS) known as MySQL, a key, also known as an index, is a single or combination of multiple fields in a table. A key can retrieve requested data rows from a table and can create relationships among different tables. The key we are discussing today is the unique key. The unique key is used to uniquely identify one or more fields (rows or columns) in a table. The unique key is used when you want a field to have distinct values.

The unique key is similar to and works in conjunction with the primary key. Sometimes they are used interchangeably, but there are in fact subtle differences between the two and should not be confused with each other. Both the unique and primary keys are similar in that the two consist of a single column or a set of columns, but they differ in constraint and depth. Essentially, a unique key is more basic than a primary key.

Each unique key is composed of one or more data attributes of a data entity found in an entity relationship diagram of a data model. The set of unique keys assigned to a data entity is referred to as candidate keys for that data entity. Then, a single unique key is selected from the candidate keys which becomes the primary key of that data entity.

Differences Between Primary Keys and Unique Keys

As mentioned before, the primary key and unique key actions within the MySQL framework seem almost identical on the surface and can be easily confused, but the differences are subtle and they render slightly different outcomes.

Other Keys Used in MySQL

MySQL utilizes a few different keys, or indexes, in its language and are interconnected in several ways. These keys are used in order to more easily find information requested by the user and to create connections among pieces of information from disparate sources. They also help prevent duplicate row data. Conversely, the index system of MySQL have negative effects, including taking up disc space and slowing down the speed of writing queries. Here’s a brief description of a few of the other MySQL keys.

  1. Normal or Non-Unique Key – This is the most basic unit of the index system of MySQL and unlike the other keys, it contains no restraints and can be likened to a library’s card catalog. The benefit of having an index that is not designated as unique or primary is to separate themselves from more complex fields for faster search results. Being the least complex keys, they are the easiest to create.
  2. Full-Text Key – This specialized index varies greatly from the others and its behavior differs between database systems. Full-text indexes are only useful with full text searches done with the MATCH()/AGAINST() clause, which are implemented internally using b-trees (allowing for selecting, sorting or ranges starting from the left-most column) or hash tables.
  3. Foreign Key – This is a field in a relational table that matches the primary key column of another table. This index is helpful in cross-referencing tables. Foreign keys use the values of attributes in the referenced relation to restrict the domain of one or more attributes in the referencing relation.
  4. Candidate Key – Mentioned above, the candidate key is a minimal superkey for a specific relation. The superkey does not have two distinct tuples (rows or records in common database language) with the same values for the attributes that make up the key.

Uses and Examples of the Unique Key

When written out, the unique key looks similar to the primary key, but as explained earlier, it renders a different outcome. Here is a basic example of using the unique key in creating a table with MySQL.

The data that needs to be put into the table is as follows:

id  pid aid
1   2   3
2   3   2
3   3   4   

The user would then enter the following code into the system, using the unique key:

CREATE TABLE ab (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
pid INT UNSIGNED NOT NULL DEFAULT 0,
aid INT UNSIGNED NOT NULL DEFAULT 0,
PRIMARY KEY (id),
UNIQUE KEY (pid, aid) <-----------
);

In this example, the unique key restraint is on the “pid” and “aid” columns. The unique restraint can be dropped from this table as well. By inputing the following query, you are doing away with that particular restraint.

ALTER TABLE ab
DROP INDEX (pid, aid)

The unique key found in the MySQL database management system, when used in conjunction with the other indexes found in that system, can streamline, facilitate and interconnect the various pieces of data being retrieved from a data table, making a search easier for the user. While similar to the primary key, the unique key’s subtle differences further whittle down potentially large amounts of information into more manageable amounts, making the keys of MySQL very useful tools when utilized effectively. If you enjoyed this tutorial, why not learn how to code secure PHP?

Page Last Updated: January 2014

MySQL students also learn

Empower your team. Lead the industry.

Get a subscription to a library of online courses and digital learning tools for your organization with Udemy Business.

Request a demo