📖 Scenario: You are setting up 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: Create a table named Books with columns for BookID, Title, and Author. Ensure that BookID is the PRIMARY KEY to uniquely identify each book.
📋 What You'll Learn
Create a table called Books
Add a column BookID of type INT
Add columns Title and Author of type VARCHAR(100)
Set BookID as the PRIMARY KEY of the table
💡 Why This Matters
🌍 Real World
Databases use PRIMARY KEY constraints to uniquely identify each record, like a library catalog number for books.
💼 Career
Understanding PRIMARY KEY constraints is essential for database design and data integrity in software development and data management jobs.
Progress0 / 4 steps
1
Create the Books table with columns
Write a CREATE TABLE statement to create a table named Books with columns BookID as INT, Title as VARCHAR(100), and Author as VARCHAR(100).
SQL
Hint
Use CREATE TABLE with column names and data types separated by commas.
2
Add PRIMARY KEY constraint to BookID
Modify the CREATE TABLE statement to add a PRIMARY KEY constraint on the BookID column.
SQL
Hint
Add PRIMARY KEY right after the data type of BookID.
3
Insert sample data into Books table
Write an INSERT INTO statement to add a book with BookID 1, Title 'The Great Gatsby', and Author 'F. Scott Fitzgerald' into the Books table.
SQL
Hint
Use INSERT INTO with column names and VALUES with matching data.
4
Try inserting a duplicate BookID to see PRIMARY KEY in action
Write an INSERT INTO statement to add another book with the same BookID 1, Title '1984', and Author 'George Orwell'. This should fail because BookID is a PRIMARY KEY.
SQL
Hint
Try inserting a row with the same BookID to see the PRIMARY KEY constraint prevent duplicates.
Practice
(1/5)
1. What is the main purpose of a PRIMARY KEY in a database table?
easy
A. To speed up database backups
B. To uniquely identify each row and prevent null values
C. To store large text data
D. To allow duplicate values in a column
Solution
Step 1: Understand the role of PRIMARY KEY
A PRIMARY KEY uniquely identifies each row and does not allow nulls, ensuring data integrity.
Step 2: Compare options with PRIMARY KEY purpose
Options A, B, and C do not describe uniqueness or null restriction, which are key features of PRIMARY KEY.
Final Answer:
To uniquely identify each row and prevent null values -> Option B
Quick Check:
PRIMARY KEY = unique + not null [OK]
Hint: PRIMARY KEY means unique and no nulls [OK]
Common Mistakes:
Thinking PRIMARY KEY allows duplicates
Confusing PRIMARY KEY with foreign key
Assuming PRIMARY KEY stores large text
2. Which of the following is the correct syntax to define a PRIMARY KEY on the id column during table creation?
easy
A. CREATE TABLE users (id INT PRIMARY KEY, name VARCHAR(50));
B. CREATE TABLE users (id INT, name VARCHAR(50) PRIMARY KEY);
C. CREATE TABLE users (id INT, PRIMARY KEY name);
D. CREATE TABLE users (id INT, name VARCHAR(50), PRIMARY KEY id);
Solution
Step 1: Review correct PRIMARY KEY syntax inline
PRIMARY KEY is declared after the column type, like id INT PRIMARY KEY.
Step 2: Check each option's syntax
CREATE TABLE users (id INT PRIMARY KEY, name VARCHAR(50)); correctly places PRIMARY KEY after id INT. Options B, C, and D have syntax errors or misplaced keywords.
Final Answer:
CREATE TABLE users (id INT PRIMARY KEY, name VARCHAR(50)); -> Option A
Quick Check:
PRIMARY KEY follows column type inline [OK]
Hint: PRIMARY KEY goes right after column type [OK]
Common Mistakes:
Placing PRIMARY KEY after wrong column
Using PRIMARY KEY without parentheses for multiple columns
5. You want to create a table students where each student is uniquely identified by a combination of student_id and campus_id. Which of the following statements correctly enforces this using a PRIMARY KEY?
hard
A. CREATE TABLE students (student_id INT PRIMARY KEY, campus_id INT PRIMARY KEY);
B. CREATE TABLE students (student_id INT, campus_id INT, PRIMARY KEY student_id, campus_id);
C. CREATE TABLE students (student_id INT, campus_id INT, PRIMARY KEY (student_id, campus_id));
D. CREATE TABLE students (student_id INT, campus_id INT, UNIQUE (student_id, campus_id));
To uniquely identify rows by two columns, a composite PRIMARY KEY on both columns is needed.
Step 2: Evaluate each option's correctness
CREATE TABLE students (student_id INT, campus_id INT, PRIMARY KEY (student_id, campus_id)); correctly defines a composite PRIMARY KEY. CREATE TABLE students (student_id INT PRIMARY KEY, campus_id INT PRIMARY KEY); tries to assign two PRIMARY KEYs, which is invalid. CREATE TABLE students (student_id INT, campus_id INT, PRIMARY KEY student_id, campus_id); has syntax errors. CREATE TABLE students (student_id INT, campus_id INT, UNIQUE (student_id, campus_id)); uses UNIQUE but not PRIMARY KEY.
Final Answer:
CREATE TABLE students (student_id INT, campus_id INT, PRIMARY KEY (student_id, campus_id)); -> Option C
Quick Check:
Composite PRIMARY KEY uses parentheses around columns [OK]
Hint: Use parentheses for composite PRIMARY KEY columns [OK]
Common Mistakes:
Trying multiple PRIMARY KEYs on separate columns
Missing parentheses in PRIMARY KEY declaration
Using UNIQUE instead of PRIMARY KEY for uniqueness
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.