Unit 3 Database - Implementation - SQL UPDATE Flashcards
What is the purpose of the UPDATE query?
The UPDATE query is used to amend rows in a table and can update multiple rows at the same time.
What is the basic syntax of an UPDATE query?
UPDATE table_name SET column1 = value1, column2 = value2, … WHERE condition;
Why is the WHERE clause important in an UPDATE query?
The WHERE clause specifies which rows to update; without it, all rows in the table will be updated.
Example: How would you update a user’s last login date to ‘01/01/2019’ where userid is 7914?
UPDATE user SET lastlogin = ‘01/01/2019’ WHERE userid = 7914;
How do you update one row value in a table?
Use the UPDATE query with a specific WHERE condition to target one row.
Example: Update the salon to ‘Westside’ for hairdresserid 2210.
UPDATE hairdresser SET salon = ‘Westside’ WHERE hairdresserid = 2210;
How do you update several row values at once?
Use the UPDATE query with a condition that matches multiple rows.
Example: Change hairdresserid to 3030 where it equals 1928.
UPDATE client SET hairdresserid = 3030 WHERE hairdresserid = 1928;
What is an equi-join used for in an UPDATE query?
An equi-join updates rows based on matching values between two tables.
Example: Update client.hairdresserid to 3030 where it matches hairdresser.hairdresserid and equals 1928.
UPDATE client, hairdresser SET client.hairdresserid = 3030 WHERE client.hairdresserid = hairdresser.hairdresserid AND hairdresser.hairdresserid = 1928;
What should you do when updating a primary key?
Ensure corresponding foreign key values are updated to maintain referential integrity.
Example: Update hairdresserid to 9255 where userId is 2210.
UPDATE hairdresser SET hairdresserid = 9255 WHERE userId = 2210;
What happens if your RDBMS does not support cascade updates?
You must manually update corresponding foreign keys using another UPDATE query.
Example: Manually update foreign keys for client table after changing a primary key.
UPDATE client SET hairdresserid = 9255 WHERE hairdresserid = 2210;
How can you update text columns based on other text values?
Use the UPDATE query with a condition matching text values in the WHERE clause.
Example: Update lastname to ‘McLeod’ where salon is ‘On The Corner’.
UPDATE hairdresser SET lastname = ‘McLeod’ WHERE salon = ‘On The Corner’;
How can you update number columns based on other numbers?
Use the UPDATE query with numeric conditions in the WHERE clause.
Example: Change hairdresserid to 3030 where it equals 2019.
UPDATE client SET hairdresserid = 3030 WHERE hairdresserid = 2019;
How can you update text columns based on numbers?
Use the UPDATE query with numeric conditions in the WHERE clause.
Example: Update clientfirstname to ‘George’ where clientid equals 11766.
UPDATE client SET clientfirstname = ‘George’ WHERE clientid = 11766;
What precaution should you take before running an UPDATE query?
Run a SELECT query with the same WHERE clause to verify which records will be updated.
What happens if you omit the WHERE clause in an UPDATE query?
All rows in the table will be updated, which can lead to unintended changes.