· 4 years ago · Feb 24, 2021, 11:34 AM
1create table IF NOT EXISTS table_a
2(
3 id serial primary key not null,
4 title varchar(100) not null unique,
5 author varchar(50),
6 price float not null
7);
8
9create table IF NOT EXISTS table_b
10(
11 id serial primary key not null,
12 title varchar(100) not null,
13 author varchar(50),
14 price float not null
15);
16
17insert into table_a(title, author, price)
18values ('Harry Potter e il prigioniero di Azkaban.', 'J. K. Rowling', 21.75);
19
20insert into table_b(title, author, price)
21values ('Titolo del libro', 'autore', 0);
22
23BEGIN;
24update table_b b
25set title = a.title,
26 author = a.author,
27 price = a.price
28from table_a a
29where b.id = a.id;
30--Eseguo la select per vedere il risultato
31select * from table_b;
32--Eseguo il commit per confermare l'update del database
33COMMIT;
34--Eseguo la select per vedere che il database è stato aggiornato
35select * from table_b;
36
37BEGIN;
38update table_b b
39set title = a.title,
40 author = a.author,
41 price = a.price
42from table_a a
43where b.id = a.id;
44--Eseguo la select per vedere il risultato
45select * from table_b;
46--Eseguo il commit per annullare l'update del database
47ROLLBACK;
48--Eseguo la select per vedere che il database è stato aggiornato
49select * from table_b;
50
51