Tables, Rows, Columns & Primary Keys
The anatomy of a table — columns, rows (records), cells — and why every table needs a primary key.
Every answer you get from SQL comes out of a table. Before writing real queries you must be able to look at a table and name each part correctly — column, row, cell, primary key — because these words appear in every chapter, every error message and every interview.
Definition — Table
A table is a database object that stores data about one kind of thing (an entity) in a grid of columns and rows. In relational theory a table is called a relation.
The players table stores data about IPL players — only players, nothing else. Teams have their own table, innings have their own table.
| id | name | team | country | runs |
|---|---|---|---|---|
| 1 | Virat Kohli | RCB | India | 750 |
| 2 | Rohit Sharma | MI | India | 500 |
| 3 | Jos Buttler | RR | England | 850 |
| 4 | MS Dhoni | CSK | India | 350 |
| 5 | David Warner | DC | Australia | 700 |
| 6 | Jasprit Bumrah | MI | India | 20 |
Definitions — Column, Row, Cell
A column (also called a field or attribute) is a vertical set of values that all describe the same property, and all have the same data type.
A row (also called a record or tuple) is one horizontal entry that describes one single item — here, one player.
A cell is the single value where one row and one column meet.
| Term | Other names | In the players table |
|---|---|---|
| Table | relation, entity | players |
| Column | field, attribute | name, team, country, runs |
| Row | record, tuple | 3, Jos Buttler, RR, England, 850 |
| Cell | value, field value | 'Jos Buttler' (row 3, column name) |
The table above has 5 columns and 6 rows. Columns are decided when the table is designed (structure); rows are added and removed all the time (data).
Definition — Primary Key
A primary key (PK) is a column (or a set of columns) whose value uniquely identifies every row in a table. It must be unique and can never be NULL (empty).
In players, the id column is the primary key: 1, 2, 3 … each number belongs to exactly one player.
Why not use name as the key? Because names can repeat (there could be two "Rahul Sharma"s), names can change spelling, and text keys are slower to compare. A small, meaningless number such as id never needs to change — that is called a surrogate key.
Rules of a primary key:
- Unique — no two rows can share a key value.
- Not NULL — every row must have a key value.
- One per table — a table can have only one primary key (but it can span several columns — a composite key).
- Stable — the value should never change once assigned.
Example — find one row by its primary key
sql
SELECT *
FROM players
WHERE id = 3;| id | name | team | country | runs |
|---|---|---|---|---|
| 3 | Jos Buttler | RR | England | 850 |
How it works: because id is unique, asking for id = 3 is guaranteed to return at most one row. That is exactly why apps use primary keys to fetch "this one player".
What happens if a duplicate key is inserted?
sql
INSERT INTO players (id, name, team, country, runs)
VALUES (1, 'Shubman Gill', 'GT', 'India', 890);SQL Server rejects it with an error like "Violation of PRIMARY KEY constraint … Cannot insert duplicate key in object 'dbo.players'. The duplicate key value is (1)." The DBMS protects your data automatically.
Foreign key (preview)
The team column in players holds values like RCB that also exist as the primary key of the teams table. A column that points to another table's primary key is called a foreign key — it is the "link" that makes a database relational. You will use it with JOINs in Chapter 6.
Common mistakes
- ❌ Calling a row a "column" or vice versa. ✅ Columns go down (properties), rows go across (items).
- ❌ Using
nameoremailas a primary key "because it's unique today". ✅ Prefer a stable numericid. - ❌ Thinking rows are stored in a fixed order. ✅ A table has no guaranteed row order; use
ORDER BYwhen order matters.
Interview tip
Q: What is the difference between a primary key and a unique key? A: Both enforce uniqueness. A primary key cannot contain NULL and there can be only one per table. A unique key (UNIQUE constraint) allows one NULL in SQL Server, and a table can have many of them.
Key points to remember
- A table stores data about one kind of thing in columns and rows (a 'relation' in theory).
- A column (field/attribute) is one property of the item and has one data type for all its values.
- A row (record/tuple) is one item — e.g. one player; a cell is one value where a row and column meet.
- A primary key uniquely identifies each row: it must be unique, NOT NULL, and a table has only one.
- Prefer a stable numeric id over names as a primary key (surrogate key).
- A foreign key is a column that refers to another table's primary key — it links tables together.
- Tables have no guaranteed row order — use ORDER BY when order matters.
Try it yourself
Fetch one player by primary key
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
Duplicate primary key is rejected (SQL Server)
-- id 1 already belongs to Virat Kohli
INSERT INTO players (id, name, team, country, runs)
VALUES (1, 'Shubman Gill', 'GT', 'India', 890);
-- Msg 2627: Violation of PRIMARY KEY constraint 'PK__players__...'.
-- Cannot insert duplicate key in object 'dbo.players'.
-- The duplicate key value is (1).SQL Server-specific — run this in SQL Server (SSMS / Azure Data Studio).
Try it: add a row with a NEW key
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
Practice: count rows and columns
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
Revision Flashcards
Tap a card to flip it and see the answer.
