What if your data could organize itself and never get mixed up again?
Why Primary key declaration in MySQL? - Purpose & Use Cases
Imagine you have a big notebook where you write down your friends' phone numbers. You try to find a friend's number by flipping through every page one by one.
This takes a long time and you might accidentally write the same friend's number twice because you have no way to check if it is already there. It is easy to get confused and make mistakes.
Using a primary key is like giving each friend a unique ID number in your notebook. This ID helps you find their number quickly and prevents duplicates.
CREATE TABLE friends (name VARCHAR(50), phone VARCHAR(20));
CREATE TABLE friends (id INT PRIMARY KEY, name VARCHAR(50), phone VARCHAR(20));
It makes your data organized, easy to search, and safe from accidental duplicates.
When a library tracks books, each book has a unique ID number (primary key) so it can quickly find and manage each book without confusion.
Primary keys give each record a unique identity.
They speed up searching and keep data clean.
They prevent duplicate entries in your table.
Practice
PRIMARY KEY in a MySQL table?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 AQuick Check:
Primary key = unique row ID [OK]
- Thinking primary key allows NULL values
- Confusing primary key with indexing only
- Assuming primary key stores large text
CREATE TABLE statement?Solution
Step 1: Review inline primary key syntax
Inline primary key declaration is done by addingPRIMARY KEYright after the column type, likeid INT PRIMARY KEY.Step 2: Check each option
CREATE TABLE users (id INT PRIMARY KEY, name VARCHAR(50)); correctly placesPRIMARY KEYinline afterid INT. Others have syntax errors or misplaced keywords.Final Answer:
CREATE TABLE users (id INT PRIMARY KEY, name VARCHAR(50)); -> Option CQuick Check:
Inline primary key = column type + PRIMARY KEY [OK]
- Placing PRIMARY KEY after column list without parentheses
- Using PRIMARY KEY without column name
- Putting PRIMARY KEY after another column
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');
Solution
Step 1: Understand primary key uniqueness
The primary key columnidmust have unique values. Duplicate values cause errors.Step 2: Analyze the insert statements
The first insert addsid=1. The second insert tries to add the sameid=1, violating uniqueness.Final Answer:
Error due to duplicate primary key value -> Option BQuick Check:
Duplicate primary key = error [OK]
- Assuming duplicate inserts overwrite existing rows
- Thinking NULL can be inserted in primary key
- Believing both rows insert without error
CREATE TABLE orders ( order_id INT, customer_id INT, PRIMARY KEY order_id );
Solution
Step 1: Check syntax for separate primary key declaration
When declaring a primary key separately, the column name(s) must be inside parentheses, likePRIMARY KEY (order_id).Step 2: Identify the error in the statement
The statement usesPRIMARY KEY order_idwithout parentheses, causing a syntax error.Final Answer:
Missing parentheses around the primary key column -> Option DQuick Check:
Separate primary key needs parentheses [OK]
- Omitting parentheses in separate primary key
- Thinking primary key must be inline only
- Assuming INT type is invalid for primary key
employees with a composite primary key consisting of department_id and employee_number. Which is the correct way to declare this primary key?Solution
Step 1: Understand composite primary key syntax
A composite primary key is declared by listing multiple columns inside parentheses afterPRIMARY 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.Final Answer:
CREATE TABLE employees (department_id INT, employee_number INT, PRIMARY KEY (department_id, employee_number)); -> Option AQuick Check:
Composite key = PRIMARY KEY (col1, col2) [OK]
- Declaring multiple primary keys separately
- Missing parentheses for composite keys
- Assigning primary key to only one column
Ready to prove it?
Pass the test and this pattern moves unknown
