Objectives:
- Learn the
UPDATEstatement syntax - Use
SETto modify column values - Apply conditions with the
WHEREclause to specify which rows to update
1. Introduction to UPDATE Statement (5 minutes)
Syntax Overview:
- The
UPDATEstatement is used to modify existing records in a table.
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
Example:
UPDATE TestTable
SET Name = 'Jane Doe', DateOfBirth = '1995-01-01'
WHERE ID = 1;
2. Updating a Single Column (10 minutes)
Basic Syntax:
- Update a single column in a table.
UPDATE Students
SET FirstName = 'John'
WHERE StudentID = 1;
Exercise:
- Write SQL queries to update the
LastNameof the student withStudentID1 to ‘Doe’.
3. Updating Multiple Columns (10 minutes)
Syntax for Multiple Columns:
- Update multiple columns in a single statement.
UPDATE Students
SET FirstName = 'Jane', LastName = 'Smith'
WHERE StudentID = 2;
Example:
UPDATE Students
SET FirstName = 'Emily', LastName = 'Johnson', EnrollmentDate = '2024-01-17'
WHERE StudentID = 3;
Exercise:
- Write SQL queries to:
- Update
FirstNameto ‘Alice’ andLastNameto ‘Brown’ forStudentID4. - Change
EnrollmentDateto ‘2024-01-20’ forStudentID6.
4. Using WHERE Clause to Specify Rows (10 minutes)
Importance of WHERE Clause:
- The
WHEREclause is crucial to prevent updating all rows.
UPDATE Students
SET EnrollmentDate = '2024-01-25'
WHERE LastName = 'Smith';
Example:
UPDATE Students
SET FirstName = 'Charlie', LastName = 'Davis'
WHERE StudentID = 7;
Exercise:
- Write SQL queries to:
- Update
FirstNameto ‘Kelly’ andLastNameto ‘Lewis’ for students withEnrollmentDateafter ‘2024-01-25’. - Change
LastNameto ‘Walker’ for all students whoseFirstNameis ‘Ian’.
5. Practical Exercise: Updating Data (10 minutes)
- Task:
- Use the
Studentstable. - Insert more sample data into
Studentstable:
INSERT INTO Students (StudentID, FirstName, LastName, EnrollmentDate)
VALUES
(16, 'Mona', 'Hall', '2024-01-29'),
(17, 'Liam', 'Young', '2024-01-28');
- Exercises:
- Update the
FirstNameto ‘Jack’ andLastNameto ‘Green’ forStudentID12. - Change
EnrollmentDateto ‘2024-01-30’ for all students withLastName‘Moore’. - Set
FirstNameto ‘Grace’ andLastNameto ‘Wilson’ forStudentID9.
Exercises
- Updating a Single Column:
- Exercise: Write SQL queries to update the
LastNameto ‘Smith’ forStudentID2.
- Updating Multiple Columns:
- Exercise: Write SQL queries to:
- Change
FirstNameto ‘John’ andLastNameto ‘Doe’ forStudentID1. - Update
FirstNameto ‘Alice’ andLastNameto ‘Brown’ forStudentID4.
- Change
- Using WHERE Clause:
- Exercise: Write SQL queries to:
- Update
EnrollmentDateto ‘2024-01-25’ for all students withLastName‘Smith’. - Change
LastNameto ‘Walker’ for all students withFirstName‘Ian’.
- Update
- Sample Update Tasks:
- Exercise: Execute the following tasks:
- Update
FirstNameto ‘Kelly’ andLastNameto ‘Lewis’ for students withEnrollmentDateafter ‘2024-01-25’. - Change
LastNameto ‘Green’ forStudentID12.
- Update
Homework:
- Practice Questions:
- Write SQL queries to:
- Update
LastNameto ‘Moore’ for all students withFirstName‘Hannah’. - Set
EnrollmentDateto ‘2024-02-01’ for students whoseLastNameis ‘Evans’.
- Update
