📖 Scenario: You are creating a simple database table to store information about books in a library. Each book must have a unique identifier so that it can be easily found and managed.
🎯 Goal: Build a table called Books with a primary key and a unique constraint to ensure data integrity.
📋 What You'll Learn
Create a table named Books with columns BookID, Title, and ISBN.
Set BookID as the primary key.
Add a unique constraint on the ISBN column to prevent duplicate ISBN numbers.
💡 Why This Matters
🌍 Real World
Libraries, bookstores, and inventory systems use primary keys and unique constraints to keep track of items without confusion.
💼 Career
Database administrators and developers must design tables with keys and constraints to ensure data accuracy and efficient data retrieval.
Progress0 / 4 steps
1
Create the Books table with columns
Write a SQL statement to create a table called Books with three columns: BookID as an integer, Title as text, and ISBN as text.
SQL
Hint
Use CREATE TABLE followed by the table name and define each column with its data type inside parentheses.
2
Add a primary key to BookID
Modify the Books table creation statement to set BookID as the primary key.
SQL
Hint
Add PRIMARY KEY right after the BookID INT declaration.
3
Add a unique constraint on ISBN
Update the Books table creation statement to add a unique constraint on the ISBN column to prevent duplicate ISBN values.
SQL
Hint
Add UNIQUE right after the ISBN TEXT declaration.
4
Complete the Books table with primary key and unique constraint
Ensure the final CREATE TABLE Books statement includes BookID as the primary key and ISBN as unique.
SQL
Hint
Double-check that both PRIMARY KEY and UNIQUE constraints are present in the table definition.
Practice
(1/5)
1. Which of the following is TRUE about a PRIMARY KEY in a SQL table?
easy
A. It allows duplicate values but no NULLs.
B. It uniquely identifies each row and cannot contain NULL values.
C. It can contain multiple NULL values.
D. It is used only for indexing and does not enforce uniqueness.
Solution
Step 1: Understand the role of PRIMARY KEY
A PRIMARY KEY uniquely identifies each row in a table and does not allow NULL values to ensure every row is distinct.
Step 2: Compare options with PRIMARY KEY properties
It uniquely identifies each row and cannot contain NULL values. correctly states uniqueness and no NULLs. Other options contradict these rules.
Final Answer:
It uniquely identifies each row and cannot contain NULL values. -> Option B
Quick Check:
PRIMARY KEY = unique + no NULL [OK]
Hint: Primary key = unique + no NULL values [OK]
Common Mistakes:
Thinking primary keys allow NULLs
Confusing primary key with unique constraint
Assuming primary key allows duplicates
2. Which SQL statement correctly defines a column id as a primary key in a table?
easy
A. CREATE TABLE users (id INT, name VARCHAR(50) PRIMARY KEY);
B. CREATE TABLE users (id INT UNIQUE, name VARCHAR(50));
C. CREATE TABLE users (id INT PRIMARY KEY, name VARCHAR(50));
D. CREATE TABLE users (id INT NOT NULL, name VARCHAR(50));
Solution
Step 1: Identify correct PRIMARY KEY syntax
The correct syntax to define a primary key inline is column_name data_type PRIMARY KEY.
Step 2: Check each option
CREATE TABLE users (id INT PRIMARY KEY, name VARCHAR(50)); uses correct syntax. CREATE TABLE users (id INT UNIQUE, name VARCHAR(50)); uses UNIQUE, not PRIMARY KEY. CREATE TABLE users (id INT NOT NULL, name VARCHAR(50)); only uses NOT NULL. CREATE TABLE users (id INT, name VARCHAR(50) PRIMARY KEY); incorrectly assigns PRIMARY KEY to the wrong column.
Final Answer:
CREATE TABLE users (id INT PRIMARY KEY, name VARCHAR(50)); -> Option C
PRIMARY KEY enforces unique values. The first insert adds id=1 successfully.
Step 2: Analyze second insert with duplicate id
The second insert tries to add id=1 again, violating uniqueness, causing an error.
Final Answer:
The second insert fails due to duplicate primary key. -> Option A
Quick Check:
Duplicate PRIMARY KEY value causes insert failure [OK]
Hint: Duplicate primary keys cause insert errors [OK]
Common Mistakes:
Assuming duplicates are allowed in primary key
Thinking second insert overwrites first
Believing primary keys cannot be integers
4. You have this table definition:
CREATE TABLE products (
product_id INT UNIQUE,
name VARCHAR(100)
);
Which problem will occur if you try to insert two rows with product_id as NULL?
medium
A. Both rows insert successfully because UNIQUE allows multiple NULLs.
B. The second insert fails because UNIQUE does not allow duplicate NULLs.
C. Syntax error because NULL is not allowed in UNIQUE columns.
D. Both inserts fail because product_id is not a PRIMARY KEY.
Solution
Step 1: Understand UNIQUE constraint with NULLs
UNIQUE allows only one NULL in some databases, but standard SQL allows multiple NULLs because NULL means unknown.
Step 2: Apply to given inserts
In most SQL systems, inserting multiple rows with NULL in a UNIQUE column succeeds because NULLs are not considered equal.
Final Answer:
Both rows insert successfully because UNIQUE allows multiple NULLs. -> Option A
Quick Check:
UNIQUE allows multiple NULLs in most SQL [OK]
Hint: UNIQUE allows multiple NULLs, PRIMARY KEY does not [OK]
Common Mistakes:
Assuming UNIQUE disallows multiple NULLs
Confusing UNIQUE with PRIMARY KEY NULL rules
Expecting syntax error on NULL in UNIQUE
5. You want to create a table orders where each order has a unique order_id and a unique tracking_number that can be NULL. Which SQL definition is correct?
hard
A. CREATE TABLE orders (order_id INT UNIQUE, tracking_number VARCHAR(20) PRIMARY KEY);