Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL semantics for "FOR UPDATE" & "JOIN"

Tags:

I'd like to understand the exact semantics of using "FOR UPDATE" in connection with "JOIN". Does it just lock all the rows that were effectively used to build the end result? Does it do something else?

From this topic:

http://postgresql.1045698.n5.nabble.com/Select-For-Update-and-Left-Outer-Join-td4363154.html

I understand there are some important differences between database implementations. However I'm not sure. I'm interested in behaviour of any popular RDBMS out there, however PostgreSQL in particular.

like image 405
julx Avatar asked Aug 09 '11 15:08

julx


People also ask

What is the SQL query for update?

The UPDATE statement in SQL is used to update the data of an existing table in database. We can update single columns as well as multiple columns using UPDATE statement as per our requirement. UPDATE table_name SET column1 = value1, column2 = value2,...

What are the 3 update commands in SQL?

Data modification side of DML language in T-SQL includes three statements used for modifying data in SQL Server and those are: INSERT, UPDATE, and DELETE.

What is the syntax for update statement?

The UPDATE statement has the following form: UPDATE table_name SET column_name = value [, column_name = value ...] [ WHERE condition]

How do you update data in SQL?

The Syntax for SQL UPDATE Command The columns that you want to modify are listed after the SET statement and are equated to their new updated values. Commas separate these columns. The condition in the WHERE clause dictates which rows from the mentioned columns will be updated.


1 Answers

You've got it correct. In a "SELECT FOR UPDATE" with a JOIN, any rows that contribute to the returned rows will be locked. You can change this behavior by adding an "OF table_a" to the "FOR UPDATE" so that only the rows from table_a will be locked. You can read more about this in the Postgres docs here:

http://www.postgresql.org/docs/9.0/static/sql-select.html#SQL-FOR-UPDATE-SHARE

like image 189
Greg Aponte Avatar answered Sep 19 '22 06:09

Greg Aponte