by

Examples Of Computer-generated Surrogate Keys

Home › Archives › Primary Key and Foreign Key in MySQL Explained with Examples › Primary Key and Foreign Key in MySQL Explained with Examples.

  • Apr 20, 2006  The first problem is inherently caused by inserting meaningless data, and is always a problem, even with the built-in surrogate keys where the RDBMS provides a mechanism to retrieve the value. Sequences: a better surrogate key.
  • For example, your Customer table may have a CustomerPOID column used as a surrogate key as well as a CustomerNumber column and a SocialSecurityNumber column. You would likely need to support searches based on both the customer number and the social security number.

What are Keys?

A DBMS key is an attribute or set of an attribute which helps you to identify a row(tuple) in a relation(table). They allow you to find the relation between two tables. Keys help you uniquely identify a row in a table by a combination of one or more columns in that table.

Example:
Employee ID FirstName LastName
11 Andrew Johnson
22 Tom Wood
33 Alex Hale

In the above-given example, employee ID is a primary key because it uniquely identifies an employee record. In this table, no other employee can have the same employee ID.

In this tutorial, you will learn:

Why we need a Key?

Here, are reasons for using Keys in the DBMS system.

  • Keys help you to identify any row of data in a table. In a real-world application, a table could contain thousands of records. Moreover, the records could be duplicated. Keys ensure that you can uniquely identify a table record despite these challenges.
  • Allows you to establish a relationship between and identify the relation between tables
  • Help you to enforce identity and integrity in the relationship.

Various Keys in Database Management System

DBMS has folwing seven types of Keys each have their different functionality:

  • Super Key
  • Primary Key
  • Candidate Key
  • Alternate Key
  • Foreign Key
  • Compound Key
  • Composite Key
  • Surrogate Key

What is the Super key?

A superkey is a group of single or multiple keys which identifies rows in a table. A Super key may have additional attributes that are not needed for unique identification.

Example:
EmpSSNEmpNumEmpname
9812345098 AB05 Shown
9876512345 AB06 Roslyn
199937890 AB07 James

In the above-given example, EmpSSN and EmpNum name are superkeys.

What is a Primary Key?

PRIMARY KEY is a column or group of columns in a table that uniquely identify every row in that table. The Primary Key can't be a duplicate meaning the same value can't appear more than once in the table. A table cannot have more than one primary key.

Rules for defining Primary key:

  • Two rows can't have the same primary key value
  • It must for every row to have a primary key value.
  • The primary key field cannot be null.
  • The value in a primary key column can never be modified or updated if any foreign key refers to that primary key.

Example:

Examples of computer-generated surrogate keys freeIn the following example, <code>StudID</code> is a Primary Key.
StudIDRoll No First Name LastName Email
1 11 Tom Price This email address is being protected from spambots. You need JavaScript enabled to view it.
2 12 Nick Wright This email address is being protected from spambots. You need JavaScript enabled to view it.
3 13 Dana Natan This email address is being protected from spambots. You need JavaScript enabled to view it.

What is the Alternate key?

ALTERNATE KEYS is a column or group of columns in a table that uniquely identify every row in that table. A table can have multiple choices for a primary key but only one can be set as the primary key. All the keys which are not primary key are called an Alternate Key.

Example:

In this table, StudID, Roll No, Email are qualified to become a primary key. But since StudID is the primary key, Roll No, Email becomes the alternative key.
StudIDRoll No First Name LastName Email
1 11 Tom Price This email address is being protected from spambots. You need JavaScript enabled to view it.
2 12 Nick Wright This email address is being protected from spambots. You need JavaScript enabled to view it.
3 13 Dana Natan This email address is being protected from spambots. You need JavaScript enabled to view it.

What is a Candidate Key?

CANDIDATE KEY is a set of attributes that uniquely identify tuples in a table. Candidate Key is a super key with no repeated attributes. The Primary key should be selected from the candidate keys. Every table must have at least a single candidate key. A table can have multiple candidate keys but only a single primary key.

Properties of Candidate key:

  • It must contain unique values
  • Candidate key may have multiple attributes
  • Must not contain null values
  • It should contain minimum fields to ensure uniqueness
  • Uniquely identify each record in a table
Example: In the given table Stud ID, Roll No, and email are candidate keys which help us to uniquely identify the student record in the table.
StudIDRoll No First Name LastName Email
1 11 Tom Price This email address is being protected from spambots. You need JavaScript enabled to view it.
2 12 Nick Wright This email address is being protected from spambots. You need JavaScript enabled to view it.
3 13 Dana Natan This email address is being protected from spambots. You need JavaScript enabled to view it.

What is the Foreign key?

FOREIGN KEY is a column that creates a relationship between two tables. The purpose of Foreign keys is to maintain data integrity and allow navigation between two different instances of an entity. It acts as a cross-reference between two tables as it references the primary key of another table.

Example:
DeptCodeDeptName
001 Science
002 English
005 Computer
Teacher IDFnameLname
B002 David Warner
B017 Sara Joseph
B009 Mike Brunton

In this example, we have two table, teach and department in a school. However, there is no way to see which search work in which department.

In this table, adding the foreign key in Deptcode to the Teacher name, we can create a relationship between the two tables.
Teacher IDDeptCodeFnameLname
B002 002 David Warner
B017 002 Sara Joseph
B009 001 Mike Brunton

This concept is also known as Referential Integrity.

What is the Compound key?

COMPOUND KEY has two or more attributes that allow you to uniquely recognize a specific record. It is possible that each column may not be unique by itself within the database. However, when combined with the other column or columns the combination of composite keys become unique. The purpose of compound key is to uniquely identify each record in the table.

Example:
OrderNoPorductIDProduct NameQuantity
B005 JAP102459 Mouse 5
B005 DKT321573 USB 10
B005 OMG446789 LCD Monitor 20
B004 DKT321573 USB 15
B002 OMG446789 Laser Printer 3

In this example, OrderNo and ProductID can't be a primary key as it does not uniquely identify a record. However, a compound key of Order ID and Product ID could be used as it uniquely identified each record.

What is the Composite key?

COMPOSITE KEY is a combination of two or more columns that uniquely identify rows in a table. The combination of columns guarantees uniqueness, though individually uniqueness is not guaranteed. Hence, they are combined to uniquely identify records in a table.

The difference between compound and the composite key is that any part of the compound key can be a foreign key, but the composite key may or maybe not a part of the foreign key.

What is a Surrogate Key?

Examples Of Computer-generated Surrogate KeysAn artificial key which aims to uniquely identify each record is called a surrogate key. These kind of key are unique because they are created when you don't have any natural primary key. They do not lend any meaning to the data in the table. Surrogate key is usually an integer.
FnameLastnameStart TimeEnd Time
Anne Smith 09:00 18:00
Jack Francis 08:00 17:00
Anna McLean 11:00 20:00
Shown Willam 14:00 23:00

Above, given example, shown shift timings of the different employee. In this example, a surrogate key is needed to uniquely identify each employee.

Surrogate keys are allowed when

  • No property has the parameter of the primary key.
  • In the table when the primary key is too big or complicated.

Difference Between Primary key & Foreign key

Primary KeyForeign Key
Helps you to uniquely identify a record in the table. It is a field in the table that is the primary key of another table.
Primary Key never accept null values. A foreign key may accept multiple null values.
Primary key is a clustered index and data in the DBMS table are physically organized in the sequence of the clustered index. A foreign key cannot automatically create an index, clustered or non-clustered. However, you can manually create an index on the foreign key.
You can have the single Primary key in a table. You can have multiple foreign keys in a table.

Summary

  • A DBMS key is an attribute or set of an attribute which helps you to identify a row(tuple) in a relation(table)
  • DBMS keys allow you to establish a relationship between and identify the relation between tables
  • Seven Types of DBMS keys are Super, Primary, Candidate, Alternate, Foreign, Compound, Composite, and Surrogate Key.
  • A super key is a group of single or multiple keys which identifies rows in a table.
  • A column or group of columns in a table which helps us to uniquely identifies every row in that table is called a primary key
  • All the keys which are not primary key are called an alternate key
  • A super key with no repeated attribute is called candidate key
  • A compound key is a key which has many fields which allow you to uniquely recognize a specific record
  • A key which has multiple attributes to uniquely identify rows in a table is called a composite key
  • An artificial key which aims to uniquely identify each record is called a surrogate key
  • Primary Key never accept null values while a foreign key may accept multiple null values.

This article demonstrates how to “roll your own” surrogate keys and sequences in a platform-independent way, using standard SQL.

Surrogate keys

Relational theory talks about something called a “candidate key.” In SQL terms, a candidate key is any combination of columns that uniquely identifies a row (SQL and the relational model aren’t the same thing, but I’ll put that aside for this article). The data’s primary key is the minimal candidate key. Many people think a primary key is something the DBA defines, but that’s not true. The primary key is a property of the data, not the table that holds the data.

Unfortunately, the minimal candidate key is sometimes not a good primary key in the real world. For example, if the primary key is 6 columns wide and I need to refer to a row from another table, it’s impractical to make a 6-column wide foreign key. For this reason, database designers sometimes introduce a surrogate key, which uniquely identifies every row in the table and is “more minimal” than the inherently unique aspect of the data. The usual choice is a monotonically increasing integer, which is small and easy to use in foreign keys.

Every RDBMS of which I’m aware offers a feature to make surrogate keys easier by automatically generating the next larger value upon insert. In SQL Server, it’s called an IDENTITY column. In MySQL, it’s called AUTO_INCREMENT. It’s possible to generate the value in SQL, but it’s easier and generally safer to let the RDBMS do it instead. This does lead to some issues itself, such as the need to find out the value that was generated by the last insertion, but those are usually not hard to solve (LAST_INSERT_ID() and similar functions, for example).

It’s sometimes desirable not to use the provided feature. For instance, I might want to be sure I always use the next available number. In that case, I can’t use the built-in features, because they don’t generate the next available number under some circumstances. For example, SQL Server doesn’t decrement the internal counter when transactions are rolled back, leaving holes in the data (see my article on finding missing numbers in a sequence). Neither MySQL nor SQL Server decrements the counter when rows are deleted.

In these cases, it’s possible to generate the next value in the insert statement. Suppose my table looks like this:

The next value for c1 is simply the maximum value + 1. If there is no maximum value, it is 1, which is the same as 0 + 1.

Examples Of Computer-generated Surrogate Keys 2017

There are platform-dependent ways to write that statement as well, such as using SQL Server’s ISNULL function or MySQL’s IFNULL. This code can be combined into an INSERT statement, such as the following statement to insert 3 into the second column:

The code above is a single atomic statement and will prevent any two concurrent inserts from getting the same value for c1. It is not safe to find the next value in one statement and use it in another, unless both statements are in a transaction. I would consider that a bad idea, though. There’s no need for a transaction in the statement above.

Examples Of Computer-generated Surrogate Keys Free

Downsides to this approach are inability to find the value of c1 immediately after inserting, and inability to insert multiple rows at once. The first problem is inherently caused by inserting meaningless data, and is always a problem, even with the built-in surrogate keys where the RDBMS provides a mechanism to retrieve the value.

Sequences: a better surrogate key

Examples Of Computer-generated Surrogate Keys Florida

Surrogate keys are often considered very bad practice, for a variety of good reasons I won’t discuss here. Sometimes, though, there is just nothing for it but to artificially unique-ify the data. In these cases, a sequence number can often be a less evil approach. A sequence is just a surrogate key that restarts at 1 for each group of related records. For example, consider a table of log entries related to records in my t1 table:

/key-tasks-for-generativity-vs-stagnation.html. At this point I might want to enter some more records (0, 11) into t1:

Now suppose I want the following three log entries for the first row in t1:

Why Use Surrogate Key

There’s no good primary key in this data. I will have to add a surrogate key. It might seem I could add a date-time column instead, but that’s a dangerous design. It breaks as soon as two records are inserted within a timespan less than the maximum resolution of the data type. It also breaks if two records are inserted in a single transaction where the time is consistent from the first to the last statement. I’m much happier with a sequence column. The following statement will insert the log records as desired:

If I want to enter a log record on another record in t1, the sequence will start at 1 for it:

Examples Of Computer-generated Surrogate Keys For Sale

MySQL actually allows an AUTO_INCREMENT value to serve as a sequence for certain table types (MyISAM and BDB). To do tihs, just make the column the last column in a multi-column primary key. I’m not aware of any other RDBMS that does this.