1

I have these two tables which I would like to update with one SQL query using Java code:

CREATE TABLE ACCOUNT(
 ID INTEGER NOT NULL,
 USER_NAME TEXT NOT NULL,
 PASSWD TEXT,
 FIRST_NAME TEXT,
 LAST_NAME TEXT,
 E_MAIL TEXT NOT NULL,
 COUNTRY TEXT,
 STATE TEXT,
 LAST_PASSWD_RESET DATE,
 DESCRIPTION TEXT,
 LAST_UPDATED DATE,
 CREATED DATE
)
;

-- ADD KEYS FOR TABLE ACCOUNT

ALTER TABLE ACCOUNT ADD CONSTRAINT KEY1 PRIMARY KEY (ID)
;

ALTER TABLE ACCOUNT ADD CONSTRAINT USER_NAME UNIQUE (USER_NAME)
;

ALTER TABLE ACCOUNT ADD CONSTRAINT E_MAIL UNIQUE (E_MAIL)
;

-- TABLE ACCOUNT_ROLE

CREATE TABLE ACCOUNT_ROLE(
 ID INTEGER NOT NULL,
 USER_NAME TEXT NOT NULL,
 ROLE INTEGER,
 PERMISSION TEXT,
 LAST_UPDATED DATE,
 CREATED DATE
)
;

-- CREATE INDEXES FOR TABLE ACCOUNT_ROLE

CREATE INDEX IX_RELATIONSHIP19 ON ACCOUNT_ROLE (ID)
;

-- ADD KEYS FOR TABLE ACCOUNT_ROLE

ALTER TABLE ACCOUNT_ROLE ADD CONSTRAINT KEY26 PRIMARY KEY (ID)
;

ALTER TABLE ACCOUNT_ROLE ADD CONSTRAINT RELATIONSHIP19 FOREIGN KEY (ID) REFERENCES ACCOUNT (ID) ON DELETE CASCADE ON UPDATE CASCADE
;

I use this SQL query to update first table data. For the second table I use similar SQL query:

UPDATE ACCOUNT SET ID = ?, USER_NAME = ?, PASSWD = ?, FIRST_NAME = ?, LAST_NAME = ?, E_MAIL = ?, COUNTRY = ?, STATE = ?, " CITY = ?, ADDRESS = ?, STATUS = ?, SECURITY_QUESTION = ?, SECURITY_ANSWER = ?, DESCRIPTION = ?, LAST_UPDATED = CURRENT_DATE WHERE ID = ?

How this can solve this?

4
  • 1
    Solve what? You seem to understand how to write an update statement. Commented Jun 9, 2016 at 16:25
  • Correct but how to update two tables with one SQL query? Commented Jun 9, 2016 at 16:27
  • @PeterPenzov: why do you think you need to do that in a single statement? Why not simply run two update statements if you want to update two tables? Commented Jun 10, 2016 at 6:26
  • Because if one SQL statement fails I need to rowback both SQL inserts. Commented Jun 10, 2016 at 6:32

2 Answers 2

1

In Postgres, you can update two tables by using CTEs:

with t1 as (
      update . . .
      returning *
     )
update t2
    . . .

It is unclear how this fits into what you are trying to do. But it is possible to write one statement in Postgres that does two updates.

Sign up to request clarification or add additional context in comments.

Comments

1

you can't unless you have two tables that use inheritance

I do not know any SQL DB that will allow changes to two or more tables at the same time.

Not sure if you will ever need such a functionality.

If you would like to keep changes together - just open a transaction and put those two updates together, if one will fail, whole transaction can be rolled-back.

Of course one can use explicit locking with SELECT .. FOR UPDATE which is fine too.

If you are looking something to simplify code - maybe you should look at stored procedures, so you will be able to write complex code and call it with some parameters.

1 Comment

Postgres can update two tables in a single statement using data modifying CTEs: postgresql.org/docs/current/static/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.