How to SELECT MAX(...) ... FOR UPDATE?
650073Jul 15 2008 — edited Jul 15 2008Hello,
I'm kind of stuck in an SQL problem with Oracle 11. From MySQL and PostgreSQL, I'm used to get new primary keys (like ID values) from a table with the following statement:
SELECT MAX(Column) FROM table FOR UPDATE;
Like in PostgreSQL, I could use sequences, but in this special case it doesn't work, because I want a partial key which depends on other columns. The table I have looks like this:
CREATE TABLE table2 (
col1 NUMBER NOT NULL,
col2 NUMBER NOT NULL,
col3 VARCHAR...,
PRIMARY KEY (col1, col2),
FOREIGN KEY (col1) REFERENCES table1);
Now what I want to do is get a new value or col2 for a given value of col1 (which comes from another table and is already predefined). With a sequence, I could only get values for col2 that are unique on its own but not only together with col1 values.
I had the following in mind:
SELECT MAX(col2) FROM table2 WHERE col1 = ? FOR UPDATE;
But the for update clause is not supported with aggregate functions. Anyway, for update doesn't seem to prevent read access by other users which is actually the whole idea behind it... Also a LOCK TABLE in exclusive mode won't do that.
How can I lock anything against read access in Oracle?
Or: How can I get a sequence that depends on the value of another column?