Bird
0
0

Given the table products(id INT PRIMARY KEY, price DECIMAL CHECK (price > 0)), what happens if you run this query?

medium📝 query result Q13 of 15
SQL - Table Constraints
Given the table products(id INT PRIMARY KEY, price DECIMAL CHECK (price > 0)), what happens if you run this query?
INSERT INTO products (id, price) VALUES (1, -10);
AThe row is inserted with price -10.
BThe query fails due to the CHECK constraint violation.
CThe price is automatically changed to 0.
DThe query succeeds but price is set to NULL.
Step-by-Step Solution
Solution:
  1. Step 1: Understand the CHECK constraint on price

    The CHECK constraint requires price to be greater than 0, so negative values are not allowed.
  2. Step 2: Analyze the INSERT statement

    Inserting price = -10 violates the CHECK constraint, so the database rejects the insert and throws an error.
  3. Final Answer:

    The query fails due to the CHECK constraint violation. -> Option B
  4. Quick Check:

    CHECK constraint rejects invalid data [OK]
Quick Trick: CHECK constraints block invalid values on insert [OK]
Common Mistakes:
MISTAKES
  • Assuming invalid data is inserted anyway
  • Thinking the database auto-corrects invalid values
  • Believing NULL is set automatically on violation

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes