| Lesson 13 | Concatenated primary keys |
| Objective | Define concatenated primary keys and explain when to use them. |
A concatenated primary key, also called a composite primary key, is a primary key made up of two or more columns in the same table. Instead of relying on a single column to uniquely identify each row, the database uses the combination of values across several columns.
Recall that a primary key must uniquely identify every row and must never contain NULL values. In many tables, a single column such as
OrderNo or CustomerID is sufficient. However, there are important cases where no single column can guarantee uniqueness.
In those situations, a concatenated primary key is an appropriate design choice.
Consider a Line Item table that records the items on customer orders:
| Line Item | ||||||
| OrderNo | CDNo | Quantity | SellingPrice | Discount | LineCost | Shipped |
In this table, OrderNo identifies a customer order, and CDNo identifies the CD (or product) being sold.
Neither column by itself can uniquely identify a row:
OrderNo value appears on several rows.CDNo value appears on several rows.
Because both columns contain duplicates, neither column alone can serve as the primary key for the table.
We need a key that treats OrderNo and CDNo as a pair.
A concatenated primary key uses the existing columns in the table to form a unique identifier. In the Line Item table, each row is uniquely identified by the combination of:
OrderNoCDNo
As long as the same CD does not appear more than once on the same order, the pair (OrderNo, CDNo) is unique for every row.
Together, those two columns can safely serve as the primary key.
Here is a simplified version of the LineItem table definition using a concatenated primary key:
CREATE TABLE LineItem (
OrderNo INTEGER NOT NULL,
CDNo INTEGER NOT NULL,
Quantity INTEGER NOT NULL,
SellingPrice NUMERIC(8,2) NOT NULL,
Discount NUMERIC(5,2),
LineCost NUMERIC(8,2),
Shipped CHAR(1) NOT NULL,
CONSTRAINT pk_lineitem PRIMARY KEY (OrderNo, CDNo)
);
The PRIMARY KEY (OrderNo, CDNo) constraint tells the database to enforce two important rules:
(OrderNo, CDNo) must be unique in the table.OrderNo nor CDNo may contain NULL values.When you decide to use a concatenated primary key, keep these design rules in mind:
OrderNo and CDNo are sufficient. Adding Quantity to the key would
still produce unique rows, but it is unnecessary and would complicate foreign key relationships.
NULL.
In many modern designs, you will see a surrogate key (such as an auto-incrementing integer or GUID) used as the primary key.
Even in those designs, concatenated keys still appear frequently in intersection tables that resolve many-to-many relationships
(for example, StudentCourse(StudentID, CourseID)).
StudentCourse table:
STUDENT_ID has duplicates (student 1 appears in multiple rows).COURSE_ID has duplicates (course 101 appears in multiple rows).(STUDENT_ID, COURSE_ID) appears only once, so the combination uniquely identifies every row.STUDENT_ID and COURSE_ID belong to the primary key.So far we have focused on practical design rules. Behind those rules is the formal language of relational theory. In that theory, a table is called a relation[2]., a column is called an attribute, and a row is called a tuple.
The term tuple reflects the idea that a row is an ordered collection of attribute values. A relation with two attributes contains pairs of values (2-tuples), a relation with three attributes contains triples of values (3-tuples), and so on. Mathematicians use the general term n-tuple for these ordered sets.
In pure relational theory, relations do not impose an inherent ordering on either rows or columns. The order in which rows are stored or retrieved and the order in which columns are defined are both considered irrelevant. In practice, however, most database applications:
ORDER BY clause.The main takeaway for this lesson is that a concatenated primary key is still a primary key on a relation: it uniquely identifies tuples in that relation, just as a single-column primary key does.
Use a concatenated primary key when:
OrderLine or StudentCourse).As you design your schema, always check that the key—single-column or concatenated—captures the business rule “each row represents one and only one thing” in a way the database can enforce.
Before moving on to the next lesson, click the Quiz link below to check your understanding of key columns and primary keys.