update.out 4.9 KB
Newer Older
1
--
2
-- UPDATE ... SET <col> = DEFAULT;
3 4 5
--
CREATE TABLE update_test (
    a   INT DEFAULT 10,
6 7
    b   INT,
    c   TEXT
8
);
9 10 11 12
CREATE TABLE upsert_test (
    a   INT PRIMARY KEY,
    b   TEXT
);
13 14
INSERT INTO update_test VALUES (5, 10, 'foo');
INSERT INTO update_test(b, a) VALUES (15, 10);
15
SELECT a,b,c FROM update_test ORDER BY a,b,c;
16 17 18 19
 a  | b  |  c  
----+----+-----
  5 | 10 | foo
 10 | 15 | 
20 21 22
(2 rows)

UPDATE update_test SET a = DEFAULT, b = DEFAULT;
23
SELECT a,b,c FROM update_test ORDER BY a,b,c;
24 25 26 27
 a  | b |  c  
----+---+-----
 10 |   | foo
 10 |   | 
28 29
(2 rows)

30 31
-- aliases for the UPDATE target table
UPDATE update_test AS t SET b = 10 WHERE t.a = 10;
32
SELECT a,b,c FROM update_test ORDER BY a,b,c;
33 34 35 36
 a  | b  |  c  
----+----+-----
 10 | 10 | foo
 10 | 10 | 
37 38 39
(2 rows)

UPDATE update_test t SET b = t.b + 10 WHERE t.a = 10;
40
SELECT a,b,c FROM update_test ORDER BY a,b,c;
41 42 43 44
 a  | b  |  c  
----+----+-----
 10 | 20 | foo
 10 | 20 | 
45 46
(2 rows)

47 48 49 50
--
-- Test VALUES in FROM
--
UPDATE update_test SET a=v.i FROM (VALUES(100, 20)) AS v(i, j)
51
  WHERE update_test.b = v.j;
52
SELECT a,b,c FROM update_test ORDER BY a,b,c;
53 54 55 56
  a  | b  |  c  
-----+----+-----
 100 | 20 | foo
 100 | 20 | 
57 58
(2 rows)

59 60 61 62 63 64 65
-- fail, wrong data type:
UPDATE update_test SET a = v.* FROM (VALUES(100, 20)) AS v(i, j)
  WHERE update_test.b = v.j;
ERROR:  column "a" is of type integer but expression is of type record
LINE 1: UPDATE update_test SET a = v.* FROM (VALUES(100, 20)) AS v(i...
                                   ^
HINT:  You will need to rewrite or cast the expression.
66 67 68
--
-- Test multiple-set-clause syntax
--
69 70 71 72 73 74 75 76 77 78
INSERT INTO update_test SELECT a,b+1,c FROM update_test;
SELECT * FROM update_test;
  a  | b  |  c  
-----+----+-----
 100 | 20 | foo
 100 | 20 | 
 100 | 21 | foo
 100 | 21 | 
(4 rows)

79
UPDATE update_test SET (c,b,a) = ('bugle', b+11, DEFAULT) WHERE c = 'foo';
80
SELECT a,b,c FROM update_test ORDER BY a,b,c;
81 82 83
  a  | b  |   c   
-----+----+-------
  10 | 31 | bugle
84
  10 | 32 | bugle
85
 100 | 20 | 
86
 100 | 21 | 
87
(4 rows)
88 89

UPDATE update_test SET (c,b) = ('car', a+b), a = a + 1 WHERE a = 10;
90
SELECT a,b,c FROM update_test ORDER BY a,b,c;
91 92 93
  a  | b  |  c  
-----+----+-----
  11 | 41 | car
94
  11 | 42 | car
95
 100 | 20 | 
96
 100 | 21 | 
97
(4 rows)
98 99 100 101

-- fail, multi assignment to same column:
UPDATE update_test SET (c,b) = ('car', a+b), b = a + 1 WHERE a = 10;
ERROR:  multiple assignments to same column "b"
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
-- uncorrelated sub-select:
UPDATE update_test
  SET (b,a) = (select a,b from update_test where b = 41 and c = 'car')
  WHERE a = 100 AND b = 20;
SELECT * FROM update_test;
  a  | b  |  c  
-----+----+-----
 100 | 21 | 
  11 | 41 | car
  11 | 42 | car
  41 | 11 | 
(4 rows)

-- correlated sub-select:
UPDATE update_test o
  SET (b,a) = (select a+1,b from update_test i
               where i.a=o.a and i.b=o.b and i.c is not distinct from o.c);
SELECT * FROM update_test;
 a  |  b  |  c  
----+-----+-----
 21 | 101 | 
 41 |  12 | car
 42 |  12 | car
 11 |  42 | 
(4 rows)

-- fail, multiple rows supplied:
UPDATE update_test SET (b,a) = (select a+1,b from update_test);
ERROR:  more than one row returned by a subquery used as an expression
-- set to null if no rows supplied:
UPDATE update_test SET (b,a) = (select a+1,b from update_test where a = 1000)
  WHERE a = 11;
SELECT * FROM update_test;
 a  |  b  |  c  
----+-----+-----
 21 | 101 | 
 41 |  12 | car
 42 |  12 | car
    |     | 
(4 rows)

143 144 145 146
-- if an alias for the target table is specified, don't allow references
-- to the original table name
UPDATE update_test AS t SET b = update_test.b + 10 WHERE t.a = 10;
ERROR:  invalid reference to FROM-clause entry for table "update_test"
147 148
LINE 1: UPDATE update_test AS t SET b = update_test.b + 10 WHERE t.a...
                                        ^
149
HINT:  Perhaps you meant to reference the table alias "t".
R
Robert Haas 已提交
150 151 152
-- Make sure that we can update to a TOASTed value.
UPDATE update_test SET c = repeat('x', 10000) WHERE c = 'car';
SELECT a, b, char_length(c) FROM update_test;
153 154 155 156 157 158 159
 a  |  b  | char_length 
----+-----+-------------
 21 | 101 |            
    |     |            
 41 |  12 |       10000
 42 |  12 |       10000
(4 rows)
R
Robert Haas 已提交
160

161 162 163 164 165 166
-- Test ON CONFLICT DO UPDATE
INSERT INTO upsert_test VALUES(1, 'Boo');
-- uncorrelated  sub-select:
WITH aaa AS (SELECT 1 AS a, 'Foo' AS b) INSERT INTO upsert_test
  VALUES (1, 'Bar') ON CONFLICT(a)
  DO UPDATE SET (b, a) = (SELECT b, a FROM aaa) RETURNING *;
167
ERROR:  modification of distribution columns in OnConflictUpdate is not supported
168 169 170 171
-- correlated sub-select:
INSERT INTO upsert_test VALUES (1, 'Baz') ON CONFLICT(a)
  DO UPDATE SET (b, a) = (SELECT b || ', Correlated', a from upsert_test i WHERE i.a = upsert_test.a)
  RETURNING *;
172
ERROR:  modification of distribution columns in OnConflictUpdate is not supported
173 174 175 176
-- correlated sub-select (EXCLUDED.* alias):
INSERT INTO upsert_test VALUES (1, 'Bat') ON CONFLICT(a)
  DO UPDATE SET (b, a) = (SELECT b || ', Excluded', a from upsert_test i WHERE i.a = excluded.a)
  RETURNING *;
177
ERROR:  modification of distribution columns in OnConflictUpdate is not supported
178
DROP TABLE update_test;
179
DROP TABLE upsert_test;