UpdateQueryBuilder is used to build and execute UPDATE queries in Kysely. It provides a fluent API for updating rows with type safety.
Basic Usage
Update a Single Row
Update a row in a table:Update with Complex Values
You can provide a callback to theset method to get access to an expression builder:
Update Column by Column
You can also provide two arguments where the first is the column and the second is the value:Returning Data
On PostgreSQL you can chainreturning to get the updated rows’ columns:
Advanced Expressions
Values can be arbitrary expressions including raw SQL snippets and subqueries:Update with Joins
PostgreSQL: Update from Join
On PostgreSQL, you can use thefrom method:
MySQL: Direct Table Joins
MySQL allows you to join tables directly and update rows of all joined tables:Using Join Methods (PostgreSQL)
Limit and Order By
Limit (MySQL)
On MySQL and some other databases, you can limit the number of updated rows:Order By (MySQL)
Top Clause (MS SQL Server)
Update the first N rows:Where Conditions
All the standard where methods are available:API Reference
Main Methods
updateTable(table)- Specify the table to updateset(updates)- Set the values to updateset(column, value)- Set a single column valuewhere(...)- Add WHERE conditionswhereRef(...)- Add WHERE conditions comparing columnsfrom(table)- Add FROM clause (PostgreSQL)innerJoin(),leftJoin(),rightJoin(),fullJoin()- Join tablesorderBy(...)- Order rows (MySQL)limit(n)- Limit updated rows (MySQL)top(n)- Update top N rows (MS SQL Server)returning(...)- Return columns from updated rowsreturningAll()- Return all columnsclearWhere()- Clear WHERE conditionsclearOrderBy()- Clear ORDER BY clausemodifyEnd(modifier)- Add custom SQLexecute()- Execute the queryexecuteTakeFirst()- Execute and return first resultexecuteTakeFirstOrThrow()- Execute and return first result or throw
Result Object
The return value is an instance ofUpdateResult: