0
0
MySQLquery~20 mins

REPLACE INTO behavior in MySQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
REPLACE INTO Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
What is the output after REPLACE INTO with existing primary key?

Consider a table users with columns id (primary key) and name. Initially, it has one row: (1, 'Alice').

What will be the content of the table after executing this query?

REPLACE INTO users (id, name) VALUES (1, 'Bob');
MySQL
CREATE TABLE users (id INT PRIMARY KEY, name VARCHAR(50));
INSERT INTO users VALUES (1, 'Alice');
REPLACE INTO users (id, name) VALUES (1, 'Bob');
SELECT * FROM users ORDER BY id;
A(1, 'Bob')
B(1, 'Alice')
CEmpty table
DError: Duplicate entry for primary key
Attempts:
2 left
💡 Hint

REPLACE INTO deletes the existing row with the same primary key and inserts the new one.

🧠 Conceptual
intermediate
2:00remaining
How does REPLACE INTO handle unique key conflicts?

Given a table products with columns id (primary key), sku (unique key), and name, what happens when you run:

REPLACE INTO products (id, sku, name) VALUES (2, 'SKU123', 'New Product');

Assuming a row exists with id=3 and sku='SKU123'.

AThe existing row with <code>id=3</code> is updated to <code>id=2</code> and <code>name='New Product'</code>.
BThe new row is inserted alongside the existing row, causing duplicate <code>sku</code> error.
CThe row with <code>id=3</code> is deleted and the new row inserted with <code>id=2</code> and <code>sku='SKU123'</code>.
DThe query fails with a syntax error.
Attempts:
2 left
💡 Hint

REPLACE INTO deletes rows that conflict on any unique key, not just primary key.

📝 Syntax
advanced
2:00remaining
Which REPLACE INTO syntax is valid for inserting multiple rows?

Which of the following REPLACE INTO statements correctly inserts multiple rows into a table orders with columns order_id and amount?

AREPLACE INTO orders (order_id, amount) VALUES (1, 100), (2, 200);
BREPLACE INTO orders VALUES (1, 100), (2, 200);
CREPLACE INTO orders (order_id, amount) SET (1, 100), (2, 200);
DREPLACE INTO orders (order_id, amount) VALUES ((1, 100), (2, 200));
Attempts:
2 left
💡 Hint

Multiple rows can be inserted by separating value tuples with commas.

optimization
advanced
2:00remaining
Why might REPLACE INTO be less efficient than INSERT ... ON DUPLICATE KEY UPDATE?

Consider a table with many indexes. Which reason explains why REPLACE INTO can be slower than INSERT ... ON DUPLICATE KEY UPDATE?

AREPLACE INTO does not check for duplicate keys, causing errors.
BREPLACE INTO locks the entire table, blocking all other queries.
CREPLACE INTO cannot update existing rows, so it duplicates data.
DREPLACE INTO deletes the existing row before inserting, causing more index updates.
Attempts:
2 left
💡 Hint

Deleting and inserting rows affects indexes more than updating.

🔧 Debug
expert
2:00remaining
What error occurs when REPLACE INTO violates a foreign key constraint?

Given a table child with a foreign key referencing parent(id), what happens if you run:

REPLACE INTO child (id, parent_id) VALUES (1, 999);

Assuming no parent row with id=999 exists.

ANo error, the row is replaced successfully
BError: Cannot add or update a child row: a foreign key constraint fails
CThe parent row with <code>id=999</code> is automatically created
DThe row is inserted with <code>parent_id=999</code> ignoring the constraint
Attempts:
2 left
💡 Hint

Foreign key constraints prevent invalid references.