📖 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: Create a MySQL table called books with columns for book_id, title, and author. Define book_id as the primary key to ensure each book has a unique ID.
📋 What You'll Learn
Create a table named books
Add columns book_id (integer), title (varchar 100), and author (varchar 100)
Declare book_id as the primary key
💡 Why This Matters
🌍 Real World
Primary keys are essential in databases to uniquely identify each record, like a library book's unique ID.
💼 Career
Database developers and administrators use primary keys to organize and maintain data integrity in real-world applications.
Progress0 / 4 steps
1
Create the books table with columns
Write a MySQL CREATE TABLE statement to create a table called books with columns book_id as INT, title as VARCHAR(100), and author as VARCHAR(100).
MySQL
Hint
Use CREATE TABLE books (column definitions) to start. Define each column with its name and data type.
2
Add primary key declaration
Add a primary key constraint to the book_id column inside the CREATE TABLE books statement by adding PRIMARY KEY (book_id).
MySQL
Hint
Place PRIMARY KEY (book_id) after the column definitions, separated by commas.
3
Add NOT NULL to primary key column
Modify the book_id column to include NOT NULL so it cannot have empty values, by changing book_id INT to book_id INT NOT NULL.
MySQL
Hint
Add NOT NULL right after the data type for book_id.
4
Add AUTO_INCREMENT to primary key
Add AUTO_INCREMENT to the book_id column so MySQL automatically assigns a unique number to each new book. Change book_id INT NOT NULL to book_id INT NOT NULL AUTO_INCREMENT.
MySQL
Hint
Add AUTO_INCREMENT after NOT NULL to make the ID auto-generate.
Practice
(1/5)
1. What is the main purpose of a PRIMARY KEY in a MySQL table?
easy
A. To uniquely identify each row and prevent duplicates
B. To allow NULL values in a column
C. To speed up SELECT queries only
D. To store large text data
Solution
Step 1: Understand the role of PRIMARY KEY
A PRIMARY KEY uniquely identifies each row in a table and ensures no duplicate or NULL values exist in that column.
Step 2: Compare options with PRIMARY KEY purpose
Only To uniquely identify each row and prevent duplicates correctly states the unique identification and prevention of duplicates, which is the main purpose.
Final Answer:
To uniquely identify each row and prevent duplicates -> Option A
Quick Check:
Primary key = unique row ID [OK]
Hint: Primary key means unique and no duplicates [OK]
Common Mistakes:
Thinking primary key allows NULL values
Confusing primary key with indexing only
Assuming primary key stores large text
2. Which of the following is the correct syntax to declare a primary key inline in a MySQL CREATE TABLE statement?
easy
A. CREATE TABLE users (id INT, name VARCHAR(50 PRIMARY KEY));
B. CREATE TABLE users (id INT, name VARCHAR(50), PRIMARY KEY id);
C. CREATE TABLE users (id INT PRIMARY KEY, name VARCHAR(50));
D. CREATE TABLE users (id INT, PRIMARY KEY, name VARCHAR(50));
Solution
Step 1: Review inline primary key syntax
Inline primary key declaration is done by adding PRIMARY KEY right after the column type, like id INT PRIMARY KEY.
Step 2: Check each option
CREATE TABLE users (id INT PRIMARY KEY, name VARCHAR(50)); correctly places PRIMARY KEY inline after id INT. Others have syntax errors or misplaced keywords.
Final Answer:
CREATE TABLE users (id INT PRIMARY KEY, name VARCHAR(50)); -> Option C
Quick Check:
Inline primary key = column type + PRIMARY KEY [OK]
Hint: Inline primary key goes right after column type [OK]
Common Mistakes:
Placing PRIMARY KEY after column list without parentheses
Using PRIMARY KEY without column name
Putting PRIMARY KEY after another column
3. Given the table creation below, what will be the result of inserting two rows with the same id?
CREATE TABLE products (
id INT PRIMARY KEY,
name VARCHAR(100)
);
INSERT INTO products (id, name) VALUES (1, 'Pen');
INSERT INTO products (id, name) VALUES (1, 'Pencil');
medium
A. Both rows inserted successfully
B. Error due to duplicate primary key value
C. Second row overwrites the first row
D. NULL inserted for the second row's id
Solution
Step 1: Understand primary key uniqueness
The primary key column id must have unique values. Duplicate values cause errors.
Step 2: Analyze the insert statements
The first insert adds id=1. The second insert tries to add the same id=1, violating uniqueness.
Final Answer:
Error due to duplicate primary key value -> Option B
Quick Check:
Duplicate primary key = error [OK]
Hint: Primary key duplicates cause insert errors [OK]
D. Missing parentheses around the primary key column
Solution
Step 1: Check syntax for separate primary key declaration
When declaring a primary key separately, the column name(s) must be inside parentheses, like PRIMARY KEY (order_id).
Step 2: Identify the error in the statement
The statement uses PRIMARY KEY order_id without parentheses, causing a syntax error.
Final Answer:
Missing parentheses around the primary key column -> Option D
Quick Check:
Separate primary key needs parentheses [OK]
Hint: Separate primary key needs ( ) around columns [OK]
Common Mistakes:
Omitting parentheses in separate primary key
Thinking primary key must be inline only
Assuming INT type is invalid for primary key
5. You want to create a table employees with a composite primary key consisting of department_id and employee_number. Which is the correct way to declare this primary key?
A composite primary key is declared by listing multiple columns inside parentheses after PRIMARY KEY.
Step 2: Evaluate each option
CREATE TABLE employees (department_id INT, employee_number INT, PRIMARY KEY (department_id, employee_number)); correctly declares both columns together as a composite primary key. CREATE TABLE employees (department_id INT PRIMARY KEY, employee_number INT PRIMARY KEY); tries to assign two primary keys separately, which is invalid. CREATE TABLE employees (department_id INT, employee_number INT PRIMARY KEY); declares only one column as primary key. CREATE TABLE employees (department_id INT, employee_number INT, PRIMARY KEY department_id, employee_number); has syntax errors missing parentheses.