Bird
Raised Fist0
DBMS Theoryknowledge~10 mins

Integrity constraints in DBMS Theory - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Integrity constraints
Start: Insert/Update/Delete
↓
Check NOT NULL
↓
Check UNIQUE
↓
Check PRIMARY KEY
↓
Check FOREIGN KEY
↓
Check CHECK constraints
↓
If all pass
→Operation Allowed
↓
If any fail
→Operation Rejected
When data is changed, the system checks each integrity rule step-by-step. If all rules pass, the change is allowed; if any fail, it is rejected.
Execution Sample
DBMS Theory
Insert into Students (ID, Name, Age) values (101, 'Amy', 20);
-- ID is PRIMARY KEY
-- Name is NOT NULL
-- Age must be >= 18 (CHECK)
This inserts a student record while checking key integrity rules like primary key uniqueness, not null, and age condition.
Analysis Table
StepConstraint CheckedConditionResultAction
1NOT NULL on NameName='Amy' is not nullPassContinue
2PRIMARY KEY on IDID=101 unique?PassContinue
3CHECK on AgeAge=20 >= 18?PassContinue
4All constraints passed--Insert allowed
💡 All constraints passed, so the insert operation is allowed.
State Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
Namenull'Amy''Amy''Amy''Amy'
IDnullnull101101101
Agenullnullnull2020
Key Insights - 3 Insights
Why does the operation stop if a NOT NULL constraint fails?
Because NOT NULL ensures a column must have a value. If it is null, the operation is rejected immediately as shown in step 1 of the execution_table.
What happens if the PRIMARY KEY value already exists?
The operation is rejected because PRIMARY KEY must be unique. This would be caught in step 2 of the execution_table where uniqueness is checked.
How does a CHECK constraint affect data insertion?
CHECK verifies a condition on data (like Age >= 18). If false, the operation is rejected as shown in step 3 of the execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the result of the PRIMARY KEY check at step 2?
APass
BFail
CNot checked
DSkipped
💡 Hint
Check the 'Result' column in row 2 of the execution_table.
At which step does the system verify that Age is at least 18?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at the 'Constraint Checked' column for the CHECK constraint in the execution_table.
If the Name was null, what would happen at step 1?
ASkip NOT NULL check
BPass and continue
CFail and reject operation
DCheck other constraints first
💡 Hint
Refer to the key_moments section about NOT NULL failure and step 1 in the execution_table.
Concept Snapshot
Integrity constraints ensure data correctness.
Common types: NOT NULL, UNIQUE, PRIMARY KEY, FOREIGN KEY, CHECK.
Each data change is checked against these rules.
If any fail, the operation is rejected.
They keep the database reliable and consistent.
Full Transcript
Integrity constraints are rules that a database uses to keep data correct and reliable. When you add or change data, the database checks these rules one by one. For example, NOT NULL means a column cannot be empty. PRIMARY KEY means each value must be unique. CHECK means a value must meet a condition like age being at least 18. If all checks pass, the data is saved. If any fail, the change is stopped. This step-by-step checking helps keep the database clean and trustworthy.

Practice

(1/5)
1. Which of the following best describes the purpose of integrity constraints in a database?
easy
A. To ensure data accuracy and consistency
B. To speed up database queries
C. To store large amounts of data
D. To create user interfaces

Solution

  1. Step 1: Understand the role of integrity constraints

    Integrity constraints are rules that keep data accurate and consistent in a database.
  2. Step 2: Match the purpose with the options

    Only To ensure data accuracy and consistency mentions accuracy and consistency, which matches the purpose of integrity constraints.
  3. Final Answer:

    To ensure data accuracy and consistency -> Option A
  4. Quick Check:

    Integrity constraints = data accuracy and consistency [OK]
Hint: Integrity constraints keep data correct and reliable [OK]
Common Mistakes:
  • Confusing constraints with performance features
  • Thinking constraints create user interfaces
  • Assuming constraints store data
2. Which of the following is the correct syntax to declare a NOT NULL constraint on a column named email in SQL?
easy
A. email VARCHAR(50) NOT NULL
B. email VARCHAR(50) NULL
C. email VARCHAR(50) UNIQUE
D. email VARCHAR(50) PRIMARY KEY

Solution

  1. Step 1: Recall the syntax for NOT NULL constraint

    The NOT NULL constraint is added after the data type to ensure the column cannot have empty values.
  2. Step 2: Identify the correct option

    email VARCHAR(50) NOT NULL correctly uses email VARCHAR(50) NOT NULL. Other options either allow NULL or use different constraints.
  3. Final Answer:

    email VARCHAR(50) NOT NULL -> Option A
  4. Quick Check:

    NOT NULL syntax = column datatype NOT NULL [OK]
Hint: NOT NULL means column must have a value, no blanks allowed [OK]
Common Mistakes:
  • Using NULL instead of NOT NULL
  • Confusing UNIQUE with NOT NULL
  • Using PRIMARY KEY when only NOT NULL is needed
3. Consider this SQL table definition:
<pre>CREATE TABLE Students ( ID INT PRIMARY KEY, Name VARCHAR(100) NOT NULL, Age INT CHECK (Age >= 18) );
What will happen if you try to insert (1, 'Alice', 16) into this table?
medium
A. Insertion succeeds without error
B. Insertion fails due to CHECK constraint violation
C. Insertion fails due to PRIMARY KEY violation
D. Insertion fails due to NOT NULL violation

Solution

  1. Step 1: Analyze the CHECK constraint on Age

    The CHECK constraint requires Age to be 18 or older.
  2. Step 2: Compare the inserted Age value

    The inserted Age is 16, which violates the CHECK constraint.
  3. Final Answer:

    Insertion fails due to CHECK constraint violation -> Option B
  4. Quick Check:

    Age 16 violates CHECK (Age >= 18) [OK]
Hint: CHECK constraints validate data ranges or conditions [OK]
Common Mistakes:
  • Ignoring CHECK constraints and assuming insert works
  • Confusing PRIMARY KEY violation with CHECK violation
  • Thinking NOT NULL is violated when it is not
4. You have a table with a UNIQUE constraint on the username column. Assume the table already contains a row with username = 'john_doe'. Which of the following SQL statements will cause an error?
medium
A. INSERT INTO Users (username) VALUES ('new_user');
B. INSERT INTO Users (username) VALUES ('jane_smith');
C. INSERT INTO Users (username) VALUES ('john_doe');
D. UPDATE Users SET username = 'jane_smith' WHERE id = 1;

Solution

  1. Step 1: Understand UNIQUE constraint behavior

    The UNIQUE constraint prevents duplicate values in the username column.
  2. Step 2: Identify duplicate insertion

    INSERT INTO Users (username) VALUES ('john_doe'); tries to insert 'john_doe' which already exists, causing a UNIQUE violation.
  3. Final Answer:

    INSERT INTO Users (username) VALUES ('john_doe'); -> Option C
  4. Quick Check:

    Duplicate username violates UNIQUE constraint [OK]
Hint: UNIQUE means no duplicates allowed in that column [OK]
Common Mistakes:
  • Assuming UPDATE causes error without duplicate
  • Thinking inserts of new usernames cause error
  • Confusing UNIQUE with PRIMARY KEY
5. A database table has two columns: OrderID and CustomerID. You want to ensure that each order belongs to an existing customer and that no two orders have the same OrderID. Which combination of integrity constraints should you use?
hard
A. NOT NULL on both columns only
B. UNIQUE on CustomerID and NOT NULL on OrderID
C. CHECK constraint on OrderID and UNIQUE on CustomerID
D. PRIMARY KEY on OrderID and FOREIGN KEY on CustomerID referencing Customers table

Solution

  1. Step 1: Ensure unique OrderID

    Use PRIMARY KEY on OrderID to guarantee uniqueness and not null.
  2. Step 2: Ensure Order belongs to existing Customer

    Use FOREIGN KEY on CustomerID referencing the Customers table to enforce valid customer links.
  3. Final Answer:

    PRIMARY KEY on OrderID and FOREIGN KEY on CustomerID referencing Customers table -> Option D
  4. Quick Check:

    Unique orders + valid customers = PRIMARY + FOREIGN KEY [OK]
Hint: Use PRIMARY KEY for unique ID, FOREIGN KEY for valid references [OK]
Common Mistakes:
  • Using UNIQUE instead of PRIMARY KEY for OrderID
  • Missing FOREIGN KEY for CustomerID
  • Assuming NOT NULL alone enforces relationships
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard