PostgreSQL/Array/Array Column Update — различия между версиями

Материал из SQL эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Текущая версия на 10:14, 26 мая 2010

An array may also be updated at a single element

postgres=#
postgres=#
postgres=# CREATE TABLE sal_emp (
postgres(#    name            text,
postgres(#    pay_by_quarter  integer[],
postgres(#    schedule        text[][]
postgres(# );
CREATE TABLE
postgres=#
postgres=# INSERT INTO sal_emp
postgres-#    VALUES ("Bill",
postgres(#    ARRAY[10000, 10000, 10000, 10000],
postgres(#    ARRAY[["meeting", "lunch"], ["training", "presentation"]]);
INSERT 0 1
postgres=#
postgres=# INSERT INTO sal_emp
postgres-#    VALUES ("Carol",
postgres(#    ARRAY[20000, 25000, 25000, 25000],
postgres(#    ARRAY[["breakfast", "consulting"], ["meeting", "lunch"]]);
INSERT 0 1
postgres=#
postgres=# SELECT * FROM sal_emp;
 name  |      pay_by_quarter       |                 schedule
-------+---------------------------+-------------------------------------------
 Bill  | {10000,10000,10000,10000} | {{meeting,lunch},{training,presentation}}
 Carol | {20000,25000,25000,25000} | {{breakfast,consulting},{meeting,lunch}}
(2 rows)
postgres=#
postgres=#
postgres=# -- An array may also be updated at a single element:
postgres=#
postgres=# UPDATE sal_emp SET pay_by_quarter[4] = 15000 WHERE name = "Bill";
UPDATE 1
postgres=#
postgres=# select * from sal_emp;
 name  |      pay_by_quarter       |                 schedule
-------+---------------------------+-------------------------------------------
 Carol | {20000,25000,25000,25000} | {{breakfast,consulting},{meeting,lunch}}
 Bill  | {10000,10000,10000,15000} | {{meeting,lunch},{training,presentation}}
(2 rows)
postgres=#
postgres=# drop table sal_emp;
DROP TABLE
postgres=#
postgres=#



An array value can be replaced completely

postgres=#
postgres=#
postgres=# CREATE TABLE sal_emp (
postgres(#    name            text,
postgres(#    pay_by_quarter  integer[],
postgres(#    schedule        text[][]
postgres(# );
CREATE TABLE
postgres=#
postgres=# INSERT INTO sal_emp
postgres-#    VALUES ("Bill",
postgres(#    ARRAY[10000, 10000, 10000, 10000],
postgres(#    ARRAY[["meeting", "lunch"], ["training", "presentation"]]);
INSERT 0 1
postgres=#
postgres=# INSERT INTO sal_emp
postgres-#    VALUES ("Carol",
postgres(#    ARRAY[20000, 25000, 25000, 25000],
postgres(#    ARRAY[["breakfast", "consulting"], ["meeting", "lunch"]]);
INSERT 0 1
postgres=#
postgres=# SELECT * FROM sal_emp;
 name  |      pay_by_quarter       |                 schedule
-------+---------------------------+-------------------------------------------
 Bill  | {10000,10000,10000,10000} | {{meeting,lunch},{training,presentation}}
 Carol | {20000,25000,25000,25000} | {{breakfast,consulting},{meeting,lunch}}
(2 rows)
postgres=#
postgres=# -- An array value can be replaced completely:
postgres=#
postgres=# UPDATE sal_emp SET pay_by_quarter = "{25000,25000,27000,27000}" WHERE name = "Carol";
UPDATE 1
postgres=#
postgres=# SELECT * FROM sal_emp;
 name  |      pay_by_quarter       |                 schedule
-------+---------------------------+-------------------------------------------
 Bill  | {10000,10000,10000,10000} | {{meeting,lunch},{training,presentation}}
 Carol | {25000,25000,27000,27000} | {{breakfast,consulting},{meeting,lunch}}
(2 rows)
postgres=#
postgres=# drop table sal_emp;
DROP TABLE
postgres=#
postgres=#



Modifying an array subscript

postgres=#
postgres=# CREATE TABLE book (id integer,titles text[]);
CREATE TABLE
postgres=#
postgres=# INSERT INTO book VALUES (102, "{"AAA\"s VVVV"}");
INSERT 0 1
postgres=# INSERT INTO book VALUES (103, "{"AAA", "VVVV"}");
INSERT 0 1
postgres=#
postgres=# -- Modifying an array subscript
postgres=#
postgres=#
postgres=# UPDATE book
postgres-#         SET titles[1] = "New\"s New"
postgres-#         WHERE titles[1] = "AAA";
UPDATE 1
postgres=#
postgres=# SELECT titles[1] FROM book;
   titles
------------
 AAA"s VVVV
 New"s New
(2 rows)
postgres=#
postgres=# drop table book;
DROP TABLE
postgres=#
postgres=#



Update array value

postgres=# CREATE TABLE book (id integer,titles text[]);
CREATE TABLE
postgres=#
postgres=# INSERT INTO book VALUES (102, "{"AAA\"s VVVV"}");
INSERT 0 1
postgres=# INSERT INTO book VALUES (103, "{"AAA", "VVVV"}");
INSERT 0 1
postgres=#
postgres=# select * from book;
 id  |     titles
-----+----------------
 102 | {"AAA"s VVVV"}
 103 | {AAA,VVVV}
(2 rows)
postgres=#
postgres=# UPDATE book
postgres-#         SET titles="{"AAA\"s VVV","ABC"}"
postgres-#         WHERE id = 102;
UPDATE 1
postgres=#
postgres=# select * from book;
 id  |      titles
-----+-------------------
 103 | {AAA,VVVV}
 102 | {"AAA"s VVV",ABC}
(2 rows)
postgres=#
postgres=# drop table book;
DROP TABLE
postgres=#
postgres=#



Update in a slice

postgres=#
postgres=#
postgres=# CREATE TABLE sal_emp (
postgres(#    name            text,
postgres(#    pay_by_quarter  integer[],
postgres(#    schedule        text[][]
postgres(# );
CREATE TABLE
postgres=#
postgres=# INSERT INTO sal_emp
postgres-#    VALUES ("Bill",
postgres(#    ARRAY[10000, 10000, 10000, 10000],
postgres(#    ARRAY[["meeting", "lunch"], ["training", "presentation"]]);
INSERT 0 1
postgres=#
postgres=# INSERT INTO sal_emp
postgres-#    VALUES ("Carol",
postgres(#    ARRAY[20000, 25000, 25000, 25000],
postgres(#    ARRAY[["breakfast", "consulting"], ["meeting", "lunch"]]);
INSERT 0 1
postgres=#
postgres=# SELECT * FROM sal_emp;
 name  |      pay_by_quarter       |                 schedule
-------+---------------------------+-------------------------------------------
 Bill  | {10000,10000,10000,10000} | {{meeting,lunch},{training,presentation}}
 Carol | {20000,25000,25000,25000} | {{breakfast,consulting},{meeting,lunch}}
(2 rows)
postgres=#
postgres=# -- Update in a slice:
postgres=#
postgres=# UPDATE sal_emp SET pay_by_quarter[1:2] = "{27000,27000}" WHERE name = "Carol";
UPDATE 1
postgres=#
postgres=# select * from sal_emp;
 name  |      pay_by_quarter       |                 schedule
-------+---------------------------+-------------------------------------------
 Bill  | {10000,10000,10000,10000} | {{meeting,lunch},{training,presentation}}
 Carol | {27000,27000,25000,25000} | {{breakfast,consulting},{meeting,lunch}}
(2 rows)
postgres=#
postgres=# drop table sal_emp;
DROP TABLE
postgres=#



Using the ARRAY expression syntax

postgres=#
postgres=#
postgres=#
postgres=# CREATE TABLE sal_emp (
postgres(#    name            text,
postgres(#    pay_by_quarter  integer[],
postgres(#    schedule        text[][]
postgres(# );
CREATE TABLE
postgres=#
postgres=# INSERT INTO sal_emp
postgres-#    VALUES ("Bill",
postgres(#    ARRAY[10000, 10000, 10000, 10000],
postgres(#    ARRAY[["meeting", "lunch"], ["training", "presentation"]]);
INSERT 0 1
postgres=#
postgres=# INSERT INTO sal_emp
postgres-#    VALUES ("Carol",
postgres(#    ARRAY[20000, 25000, 25000, 25000],
postgres(#    ARRAY[["breakfast", "consulting"], ["meeting", "lunch"]]);
INSERT 0 1
postgres=#
postgres=# SELECT * FROM sal_emp;
 name  |      pay_by_quarter       |                 schedule
-------+---------------------------+-------------------------------------------
 Bill  | {10000,10000,10000,10000} | {{meeting,lunch},{training,presentation}}
 Carol | {20000,25000,25000,25000} | {{breakfast,consulting},{meeting,lunch}}
(2 rows)
postgres=#
postgres=# -- using the ARRAY expression syntax:
postgres=#
postgres=# UPDATE sal_emp SET pay_by_quarter = ARRAY[25000,25000,27000,27000] WHERE name = "Carol";
UPDATE 1
postgres=#
postgres=# select * from sal_emp;
 name  |      pay_by_quarter       |                 schedule
-------+---------------------------+-------------------------------------------
 Bill  | {10000,10000,10000,10000} | {{meeting,lunch},{training,presentation}}
 Carol | {25000,25000,27000,27000} | {{breakfast,consulting},{meeting,lunch}}
(2 rows)
postgres=#
postgres=# drop table sal_emp;
DROP TABLE
postgres=#
postgres=#
postgres=#