ARRAY 다루기

PostgreSQL에서 데이터 조작을 위한 함수

Brian Piccolo

Sr. Director, Digital Strategy

시작하기 전에

CREATE TABLE 예시

CREATE TABLE my_first_table (
    first_column text,
    second_column integer
);

INSERT 예시

INSERT INTO my_first_table 
    (first_column, second_column) VALUES ('text value', 12);
PostgreSQL에서 데이터 조작을 위한 함수

특수 타입: ARRAY

두 개의 배열 컬럼이 있는 간단한 테이블을 만듭니다.

CREATE TABLE grades (
    student_id int,
    email text[][],
    test_scores int[]
);
PostgreSQL에서 데이터 조작을 위한 함수

ARRAY와 함께 INSERT하기

INSERT 문 예시:

INSERT INTO grades 
    VALUES (1, 
    '{{"work","[email protected]"},{"other","[email protected]"}}', 
    '{92,85,96,88}' );
PostgreSQL에서 데이터 조작을 위한 함수

ARRAY 접근

SELECT
   email[1][1] AS type,
   email[1][2] AS address,
   test_scores[1],
FROM grades;
+--------+--------------------+-------------+
| type   |  address           | test_scores |
|--------|--------------------|-------------|
| work   | [email protected] | 92          |
| work   | [email protected] | 76          |
+--------+--------------------+-------------+

PostgreSQL 배열 인덱스는 0이 아니라 1부터 시작합니다.

PostgreSQL에서 데이터 조작을 위한 함수

ARRAY 검색

SELECT
   email[1][1] as type,
   email[1][2] as address,
   test_scores[1]
FROM grades
WHERE email[1][1] = 'work';
+--------+--------------------+-------------+
| type   |  address           | test_scores |
|--------|--------------------|-------------|
| work   | [email protected] | 92          |
| work   | [email protected] | 76          |
+--------+--------------------+-------------+
PostgreSQL에서 데이터 조작을 위한 함수

ARRAY 함수와 연산자

SELECT
   email[2][1] as type,
   email[2][2] as address,
   test_scores[1]
FROM grades
WHERE 'other' = ANY (email);
+---------+---------------------+-------------+
| type    |  address            | test_scores |
|---------|-----------------------------------|
| other   | [email protected] | 92          |
| null    | null                | 76          |
+---------+---------------------+-------------+
PostgreSQL에서 데이터 조작을 위한 함수

ARRAY 함수와 연산자

SELECT
   email[2][1] as type,
   email[2][2] as address,
   test_scores[1]
FROM grades
WHERE email @> ARRAY['other'];
+---------+---------------------+-------------+
| type    |  address            | test_scores |
|---------|-----------------------------------|
| other   | [email protected] | 92          |
| null    | null                | 76          |
+---------+---------------------+-------------+
PostgreSQL에서 데이터 조작을 위한 함수

연습해 봅시다!

PostgreSQL에서 데이터 조작을 위한 함수

Preparing Video For Download...