Sub topic 5
Identifying Rows with PRIMARY KEY
Assume you know basic SELECT, INSERT, UPDATE, and DELETE statements, and that you understand tables, rows, and columns. This chapter focuses on how Oracle uses a primary key to identify each row.
A primary key is a constraint that requires every row to have a unique, non-null identifier. A table can have only one primary key, but that key can contain one column or several columns.
Why a primary key matters
A primary key gives each row a stable identity. Other tables can refer to that identity through a foreign key. Applications can also use it to find, update, or delete exactly one row.
For example, a department name might change, but its numeric department ID should remain stable.
CREATE TABLE departments (
department_id NUMBER GENERATED ALWAYS AS IDENTITY,
department_name VARCHAR2(100) NOT NULL,
CONSTRAINT departments_pk
PRIMARY KEY (department_id)
);
INSERT INTO departments (department_name)
VALUES ('Sales');
SELECT department_id, department_name
FROM departments;The departments_pk constraint applies two rules to department_id:
- No two rows can have the same value.
- No row can have a null value.
The identity clause generates the value automatically. The primary key constraint still performs the uniqueness and non-null checks. Identity generation and primary key enforcement are separate features.
Inline and out-of-line definitions
You can define a primary key beside its column, called an inline constraint:
CREATE TABLE locations (
location_id NUMBER CONSTRAINT locations_pk PRIMARY KEY,
location_name VARCHAR2(100) NOT NULL
);You can also define it after the column definitions, called an out-of-line constraint:
CREATE TABLE offices (
office_id NUMBER,
office_name VARCHAR2(100) NOT NULL,
CONSTRAINT offices_pk PRIMARY KEY (office_id)
);Out-of-line syntax is useful for composite keys and often makes table definitions easier to scan. Naming the constraint explicitly gives Oracle errors and maintenance statements a predictable name.
Composite primary keys
A composite primary key contains multiple columns. The combination must be unique, even though each individual column may repeat.
CREATE TABLE project_assignments (
project_id NUMBER NOT NULL,
employee_id NUMBER NOT NULL,
assigned_on DATE DEFAULT SYSDATE NOT NULL,
CONSTRAINT project_assignments_pk
PRIMARY KEY (project_id, employee_id)
);This table can contain (10, 25) and (10, 31), because the combinations differ. It cannot contain (10, 25) twice.
A composite key is useful when the row represents a relationship, such as one employee assigned to one project. A single-column surrogate key may be simpler when many other tables must reference the row.
How Oracle uses the constraint
When you create a primary key, Oracle creates or uses a unique index to enforce uniqueness. Inserts and updates that violate the key fail. A primary key can also be referenced by a foreign key in another table.
Exercise
Can a table have two separate PRIMARY KEY constraints, and what must be true of a composite primary key?
Show answer
No. A table can have only one primary key constraint. In a composite key, the combined column values must be unique and every key column must be non-null.