What does the KEY keyword mean?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
In SQL, KEY usually refers to either a constraint or an index, depending on the exact phrase and the database system. The word matters most in definitions such as PRIMARY KEY, FOREIGN KEY, UNIQUE KEY, and in some systems plain KEY as a synonym for an index.
PRIMARY KEY
A primary key identifies each row in a table uniquely. It is the main row identity for that table and is normally used by other tables when they need to reference it.
In this example, id is the primary key. No two rows can share the same value, and the column cannot be NULL.
You can also define a composite primary key across multiple columns:
That means the pair of values must be unique even if each column alone is not.
FOREIGN KEY
A foreign key defines a relationship from one table to another. It says that a value in the child table must match a valid primary key or unique key in the parent table.
Here customer_id is not the primary key of orders. It is a foreign key that points to customers.id.
This is how relational databases enforce referential integrity. You cannot create an order for a customer that does not exist, unless the constraint is absent or temporarily disabled.
UNIQUE KEY
A unique key enforces uniqueness without making the column the table’s primary identifier.
This is useful when a table needs one primary key for identity but still must guarantee that other values, such as usernames or emails, never repeat.
Plain KEY Often Means Index
In MySQL and MySQL-compatible systems, plain KEY is commonly just another way to say INDEX.
That KEY does not enforce uniqueness by itself. It mainly improves lookup performance for queries that filter or join on category_id.
This is an important distinction:
- '
PRIMARY KEYidentifies rows' - '
FOREIGN KEYenforces relationships' - '
UNIQUE KEYenforces uniqueness' - plain
KEYoften means non-unique index
Database Differences Matter
The broad idea of keys is common across relational systems, but plain KEY syntax is not interpreted identically everywhere.
In standard SQL discussions, people usually mean PRIMARY KEY or FOREIGN KEY when they say “key.” In MySQL DDL, plain KEY is very often shorthand for an index definition. PostgreSQL users are more likely to write CREATE INDEX explicitly rather than use KEY in that loose MySQL sense.
So when someone asks what KEY means, the right answer depends on context:
- table constraint definition
- index definition
- relational data modeling discussion
A Full Example
This table uses several kinds of keys together:
Each clause serves a different purpose:
- '
PRIMARY KEYmakesidthe row identifier' - '
UNIQUE KEYguarantees invoice numbers do not repeat' - '
KEYspeeds up lookups bycustomer_id' - '
FOREIGN KEYguarantees the referenced customer exists'
Common Pitfalls
The biggest pitfall is assuming every KEY means uniqueness. Plain KEY in MySQL is often only an index, and duplicate values are allowed unless you declare UNIQUE.
Another common mistake is confusing primary keys with foreign keys. A foreign key references another table’s key, while a primary key identifies rows in its own table.
Developers also sometimes create indexes and expect them to enforce business rules. An ordinary index helps performance, but it does not stop duplicate rows or invalid references.
Finally, be careful when switching between database systems. Syntax that works in MySQL may need to be rewritten in PostgreSQL or SQL Server, especially for nonstandard shorthand forms.
Summary
- '
KEYin SQL usually refers to row identity, relationships, uniqueness, or indexing depending on context.' - '
PRIMARY KEYuniquely identifies rows.' - '
FOREIGN KEYenforces a relationship to another table.' - '
UNIQUE KEYenforces unique values.' - In MySQL, plain
KEYoften means a non-unique index.
Related reading
- What does unsigned in MySQL mean and when to use it?
- What effects does using a binary collation have?
- What exactly differs fuzzy search from Full Text Search?
- What exactly does transaction.state.log.min.isr mean?
- What happens if a TiDB leader goes down? How does TiDB use Raft to ensure data security and consistency?
- What happens to long running clickhouse updates if the client dies?
- What happens when all contact point in data-center goes down in cassandra
- What happens with constraints when a view is removed

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.