What is the purpose of the UPDATE statement in MySQL?
A
To delete data from a table
D
To modify existing records
Analysis & Theory
The UPDATE statement is used to modify existing data in a table.
Which of the following is a correct syntax for an UPDATE statement?
A
MODIFY table SET column = value;
B
UPDATE table_name SET column1 = value1 WHERE condition;
C
CHANGE table SET value TO column;
D
ALTER table SET column = value;
Analysis & Theory
Correct syntax: UPDATE table_name SET column1 = value1 WHERE condition;
What will happen if you run an UPDATE statement without a WHERE clause?
A
Only the first row will be updated
B
Only NULL rows will be updated
C
All rows in the table will be updated
D
No rows will be updated
Analysis & Theory
Without a WHERE clause, the UPDATE statement will affect all rows in the table.
Which keyword is used in the UPDATE statement to specify the row(s) to change?
Analysis & Theory
The WHERE clause is used to target specific rows for updating.
What does the following query do?
`UPDATE employees SET salary = salary + 1000 WHERE department = 'HR';`
A
Inserts new salary for all employees
B
Increases salary by 1000 for HR employees
C
Sets salary to 1000 for all employees
D
Decreases salary by 1000 in HR
Analysis & Theory
This query increases the salary of employees in the HR department by 1000.
Can you update multiple columns in a single UPDATE statement?
A
Yes, by separating them with commas
B
No, only one column can be updated at a time
C
Only with a stored procedure
D
Yes, but only for numeric columns
Analysis & Theory
You can update multiple columns like this: SET col1 = val1, col2 = val2
What will this query do?
`UPDATE students SET grade = 'B';`
A
Update grade to 'B' for all students
B
Update grade for students with B
C
Insert new students with grade B
D
Deletes students with grade B
Analysis & Theory
Without a WHERE clause, all rows in the `students` table will have grade set to 'B'.
Which clause in an UPDATE query prevents accidental changes to all records?
Analysis & Theory
The WHERE clause limits updates to only the rows that match the condition.
Which of the following will update the `email` of user with id=5?
A
UPDATE users SET email = 'new@example.com' WHERE id = 5;
B
UPDATE users email = 'new@example.com' WHERE id = 5;
C
MODIFY users SET email TO 'new@example.com' WHERE id = 5;
D
SET users.email = 'new@example.com' WHERE id = 5;
Analysis & Theory
The correct SQL syntax is: UPDATE users SET email = '...' WHERE id = ...;
What should you always include in an UPDATE query to avoid accidental data modification?
Analysis & Theory
Always use a WHERE clause to restrict updates to specific rows. Otherwise, all rows may be updated.