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');
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;
REPLACE INTO deletes the existing row with the same primary key and inserts the new one.
REPLACE INTO first deletes the row with id=1 and then inserts the new row (1, 'Bob'). So the table ends up with the new row.
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'.
REPLACE INTO deletes rows that conflict on any unique key, not just primary key.
REPLACE INTO deletes any existing row that conflicts on primary or unique keys before inserting the new row. Here, the row with sku='SKU123' is deleted, then the new row inserted.
Which of the following REPLACE INTO statements correctly inserts multiple rows into a table orders with columns order_id and amount?
Multiple rows can be inserted by separating value tuples with commas.
Option A uses the correct syntax for inserting multiple rows with REPLACE INTO. Option A is missing column list but may work if table columns match. Option A uses invalid SET syntax. Option A nests tuples incorrectly.
Consider a table with many indexes. Which reason explains why REPLACE INTO can be slower than INSERT ... ON DUPLICATE KEY UPDATE?
Deleting and inserting rows affects indexes more than updating.
REPLACE INTO deletes the conflicting row and inserts a new one, which causes more index maintenance than updating the existing row.
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.
Foreign key constraints prevent invalid references.
REPLACE INTO still enforces foreign key constraints. If the referenced parent row does not exist, it raises an error.