A PRIMARY KEY is a column or a set of columns that uniquely identifies each row in a table. It ensures no two rows have the same key value and that the key is never NULL.
Click to reveal answer
beginner
Can a PRIMARY KEY column contain NULL values?
No, a PRIMARY KEY column cannot contain NULL values. Every row must have a unique and non-null value for the PRIMARY KEY.
Click to reveal answer
beginner
How many PRIMARY KEYs can a table have?
A table can have only one PRIMARY KEY. This key can be made of one or multiple columns (called a composite key).
Click to reveal answer
beginner
What happens if you try to insert a duplicate value into a PRIMARY KEY column?
The database will reject the insert and return an error because PRIMARY KEY values must be unique.
Click to reveal answer
beginner
How do you define a PRIMARY KEY when creating a table in SQL?
You can define a PRIMARY KEY using the syntax: CREATE TABLE table_name (id INT PRIMARY KEY, ...); or by adding a constraint: CREATE TABLE table_name (id INT, ..., PRIMARY KEY (id));
Click to reveal answer
What is the main purpose of a PRIMARY KEY in a table?
ATo uniquely identify each row
BTo allow duplicate values
CTo store large text data
DTo allow NULL values
✗ Incorrect
A PRIMARY KEY uniquely identifies each row and does not allow duplicates or NULLs.
How many PRIMARY KEYs can a table have?
AOne
BTwo
CAs many as columns
DNone
✗ Incorrect
A table can have only one PRIMARY KEY, which can be a single or composite key.
Which of these is true about PRIMARY KEY columns?
AThey can contain NULL values
BThey can have duplicate values
CThey must be unique and not NULL
DThey are optional
✗ Incorrect
PRIMARY KEY columns must have unique values and cannot be NULL.
What happens if you insert a row with a duplicate PRIMARY KEY value?
AThe old row is deleted
BThe row is inserted successfully
CThe duplicate is ignored
DThe database throws an error
✗ Incorrect
Inserting a duplicate PRIMARY KEY value causes an error because uniqueness is enforced.
Which SQL keyword is used to define a PRIMARY KEY?
AUNIQUE
BPRIMARY KEY
CFOREIGN KEY
DINDEX
✗ Incorrect
The PRIMARY KEY keyword defines the primary key constraint on a column or columns.
Explain what a PRIMARY KEY is and why it is important in a database table.
Think about how you find a unique item in a list.
You got /4 concepts.
Describe how to define a PRIMARY KEY when creating a new table in SQL.
Use CREATE TABLE statement with PRIMARY KEY.
You got /4 concepts.
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.