Domain integrity
- SQL course
- Intermediate
- Lesson 97/115
- 4 min
Learn domain integrity with column rules.
Domain integrity
Domain integrity restricts the range of values a column can take. Enforced with CHECK and NOT NULL.
CREATE TABLE products (
price DECIMAL(10,2) CHECK (price > 0),
name VARCHAR(100) NOT NULL
);Price must be positive, name must be filled.
The table products is created.
Domain integrity — details
A CHECK condition and NOT NULL constraint together enforce domain integrity. Invalid values are rejected.
For example, price DECIMAL(10,2) NOT NULL CHECK (price > 0) prevents the price from being empty, zero or negative.
Practice Domain integrity
Add a price > 0 CHECK constraint to products.
Starter code
-- domain integrity
ALTER TABLE productsSample solution
ALTER TABLE products
ADD CHECK (price > 0)Expected output
Tablo güncellendi
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 What enforces domain integrity?
- JOIN + GROUP BY
- INDEX + VIEW
- TRIGGER + PROCEDURE
- CHECK + NOT NULL Correct answer
Explanation: CHECK and NOT NULL enforce domain integrity.
2 Add CHECK
price DECIMAL(10,2) ____ (price > 0)
price DECIMAL(10,2) CHECK (price > 0)
Explanation: CHECK (price > 0) prevents a zero or negative price.
3 Domain integrity: match the terms
| Term | Meaning |
|---|---|
domain |
domain |
CHECK |
condition |
NOT NULL |
required |
invalid |
reject |