Example 1
Allow only non-negative inventory quantities
Both inserted rows satisfy quantity >= 0, so they are stored. A later insert such as quantity = -1 would violate the CHECK constraint and be rejected before bad data entered the table.
Validated query Shared across supported engines.
SQL
CREATE TABLE inventory (
id INT,
item VARCHAR(50),
quantity INT,
CONSTRAINT chk_inventory_quantity CHECK (quantity >= 0)
);
INSERT INTO
inventory (id, item, quantity)
VALUES
(1, 'Widget', 12),
(2, 'Gadget', 0);
SELECT
id,
item,
quantity
FROM
inventory
ORDER BY
id;Expected result Returned rows for the shared example.
| id | item | quantity |
|---|---|---|
| 1 | Widget | 12 |
| 2 | Gadget | 0 |
The accepted rows and returned result are identical across supported engines.