Objectives:
- Learn the
UPDATE
statement syntax - Use
SET
to modify column values - Apply conditions with the
WHERE
clause to specify which rows to update
1. Introduction to UPDATE Statement (5 minutes)
Syntax Overview:
- The
UPDATE
statement 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
LastName
of the student withStudentID
1 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
FirstName
to ‘Alice’ andLastName
to ‘Brown’ forStudentID
4. - Change
EnrollmentDate
to ‘2024-01-20’ forStudentID
6.
4. Using WHERE Clause to Specify Rows (10 minutes)
Importance of WHERE Clause:
- The
WHERE
clause 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
FirstName
to ‘Kelly’ andLastName
to ‘Lewis’ for students withEnrollmentDate
after ‘2024-01-25’. - Change
LastName
to ‘Walker’ for all students whoseFirstName
is ‘Ian’.
5. Practical Exercise: Updating Data (10 minutes)
- Task:
- Use the
Students
table. - Insert more sample data into
Students
table:
INSERT INTO Students (StudentID, FirstName, LastName, EnrollmentDate)
VALUES
(16, 'Mona', 'Hall', '2024-01-29'),
(17, 'Liam', 'Young', '2024-01-28');
- Exercises:
- Update the
FirstName
to ‘Jack’ andLastName
to ‘Green’ forStudentID
12. - Change
EnrollmentDate
to ‘2024-01-30’ for all students withLastName
‘Moore’. - Set
FirstName
to ‘Grace’ andLastName
to ‘Wilson’ forStudentID
9.
Exercises
- Updating a Single Column:
- Exercise: Write SQL queries to update the
LastName
to ‘Smith’ forStudentID
2.
- Updating Multiple Columns:
- Exercise: Write SQL queries to:
- Change
FirstName
to ‘John’ andLastName
to ‘Doe’ forStudentID
1. - Update
FirstName
to ‘Alice’ andLastName
to ‘Brown’ forStudentID
4.
- Change
- Using WHERE Clause:
- Exercise: Write SQL queries to:
- Update
EnrollmentDate
to ‘2024-01-25’ for all students withLastName
‘Smith’. - Change
LastName
to ‘Walker’ for all students withFirstName
‘Ian’.
- Update
- Sample Update Tasks:
- Exercise: Execute the following tasks:
- Update
FirstName
to ‘Kelly’ andLastName
to ‘Lewis’ for students withEnrollmentDate
after ‘2024-01-25’. - Change
LastName
to ‘Green’ forStudentID
12.
- Update
Homework:
- Practice Questions:
- Write SQL queries to:
- Update
LastName
to ‘Moore’ for all students withFirstName
‘Hannah’. - Set
EnrollmentDate
to ‘2024-02-01’ for students whoseLastName
is ‘Evans’.
- Update