Entity integrity
- SQL course
- Intermediate
- Lesson 95/115
- 4 min
Learn entity integrity with PRIMARY KEY.
Entity integrity
Entity integrity requires each row to have a unique, non-NULL primary key.
CREATE TABLE products (
id INT PRIMARY KEY,
name VARCHAR(100)
);Id primary key → entity integrity.
The table products is created.
Entity integrity — details
A primary key cannot be NULL or duplicated. This ensures each row is uniquely identified.
For example, if you try to insert two products with the same id, the PRIMARY KEY rejects the second insert.
Practice Entity integrity
Add entity integrity to products with id INT PRIMARY KEY.
Starter code
-- entity integrity
CREATE TABLE productsSample solution
CREATE TABLE products (
id INT PRIMARY KEY,
name VARCHAR(100)
)Expected output
Tablo oluşturuldu
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 PRIMARY KEY
id INT ____
id INT PRIMARY KEY
Explanation: PRIMARY KEY is the right answer.
2 Can a primary key be NULL?
- Yes, it can
- No, it cannot Correct answer
- Sometimes
- Only for INT
Explanation: A primary key cannot be NULL.
3 Entity integrity: match the terms
| Term | Meaning |
|---|---|
entity |
entity |
PRIMARY KEY |
unique |
NULL |
forbidden |
no duplicate |
one |