sq14

The SQL UPDATE statement allows you to update an existing record in the database.
The UPDATE command uses a WHERE clause. If you don't use a WHERE clause, all rows will be updated. In fact, the syntax for a basic UPDATE statement is very similar to a SELECT statement.

SQL statement

UPDATE Individual
SET UserName = 'funnyman'
WHERE IndividualId = '6';

Source Table

IndividualIdFirstNameLastNameUserName
1FredFlinstonefreddo
2HomerSimpsonhomey
3HomerBrownnotsofamous
4OzzyOzzbournesabbath
5HomerGainnoplacelike
6BennyHillhillbenny

Result

Now if we select this record, we can see the updated value.
IndividualIdFirstNameLastNameUserName
6BennyHillfunnyman

Updating Multiple Fields

To update multiple fields, separate each field assignment with a comma.

SQL statement

UPDATE Individual
SET UserName = 'getserious', FirstName = 'Onetree'
WHERE IndividualId = '6';

Result

IndividualIdFirstNameLastNameUserName
6OnetreeHillgetserious


Next lesson covers the DELETE statement.

No comments: