📖 Scenario: You are designing a simple database for a small library. To keep the data accurate and reliable, you need to apply rules called integrity constraints.These rules help ensure that the data entered into the database follows certain conditions, like no duplicate book IDs or valid publication years.
🎯 Goal: Build a basic understanding of integrity constraints by creating a table with specific rules that keep the library data correct and consistent.
📋 What You'll Learn
Create a table named Books with columns BookID, Title, Author, and YearPublished
Add a primary key constraint on BookID to prevent duplicate entries
Add a check constraint on YearPublished to allow only years from 1500 to the current year
Add a not null constraint on Title and Author to ensure these fields are always filled
💡 Why This Matters
🌍 Real World
Libraries, schools, and businesses use integrity constraints to keep their data trustworthy and consistent.
💼 Career
Database administrators and developers use integrity constraints to design robust databases that prevent errors and maintain data quality.
Progress0 / 4 steps
1
Create the Books table with columns
Write a SQL statement to create a table called Books with columns BookID as integer, Title as text, Author as text, and YearPublished as integer.
DBMS Theory
Hint
Use CREATE TABLE followed by the table name and define each column with its data type inside parentheses.
2
Add primary key and not null constraints
Modify the Books table creation to add a primary key constraint on BookID and add NOT NULL constraints on Title and Author columns.
DBMS Theory
Hint
Add PRIMARY KEY after BookID INTEGER and NOT NULL after Title TEXT and Author TEXT.
3
Add a check constraint on YearPublished
Add a CHECK constraint to the YearPublished column to allow only years between 1500 and 2024 inclusive.
DBMS Theory
Hint
Use CHECK (YearPublished >= 1500 AND YearPublished <= 2024) after the YearPublished INTEGER column definition.
4
Complete the Books table with all constraints
Ensure the Books table creation includes the primary key on BookID, NOT NULL on Title and Author, and the CHECK constraint on YearPublished from 1500 to 2024.
DBMS Theory
Hint
Review all constraints together to make sure the table enforces data integrity.
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
Step 1: Understand the role of integrity constraints
Integrity constraints are rules that keep data accurate and consistent in a database.
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.
Final Answer:
To ensure data accuracy and consistency -> Option A
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
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.
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.
Final Answer:
email VARCHAR(50) NOT NULL -> Option A
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
Step 1: Analyze the CHECK constraint on Age
The CHECK constraint requires Age to be 18 or older.
Step 2: Compare the inserted Age value
The inserted Age is 16, which violates the CHECK constraint.
Final Answer:
Insertion fails due to CHECK constraint violation -> Option B
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
Step 1: Understand UNIQUE constraint behavior
The UNIQUE constraint prevents duplicate values in the username column.
Step 2: Identify duplicate insertion
INSERT INTO Users (username) VALUES ('john_doe'); tries to insert 'john_doe' which already exists, causing a UNIQUE violation.
Final Answer:
INSERT INTO Users (username) VALUES ('john_doe'); -> Option C
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
Step 1: Ensure unique OrderID
Use PRIMARY KEY on OrderID to guarantee uniqueness and not null.
Step 2: Ensure Order belongs to existing Customer
Use FOREIGN KEY on CustomerID referencing the Customers table to enforce valid customer links.
Final Answer:
PRIMARY KEY on OrderID and FOREIGN KEY on CustomerID referencing Customers table -> Option D