ARRAY'lerle çalışmak

PostgreSQL'de Veriyi İşlemek için Fonksiyonlar

Brian Piccolo

Sr. Director, Digital Strategy

Başlamadan önce

CREATE TABLE örneği

CREATE TABLE my_first_table (
    first_column text,
    second_column integer
);

INSERT örneği

INSERT INTO my_first_table 
    (first_column, second_column) VALUES ('text value', 12);
PostgreSQL'de Veriyi İşlemek için Fonksiyonlar

ARRAY: özel bir tür

İki dizi sütunlu basit bir tablo oluşturalım.

CREATE TABLE grades (
    student_id int,
    email text[][],
    test_scores int[]
);
PostgreSQL'de Veriyi İşlemek için Fonksiyonlar

ARRAY'lerle INSERT ifadeleri

Örnek INSERT ifadesi:

INSERT INTO grades 
    VALUES (1, 
    '{{"work","[email protected]"},{"other","[email protected]"}}', 
    '{92,85,96,88}' );
PostgreSQL'de Veriyi İşlemek için Fonksiyonlar

ARRAY'lere erişim

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 dizi indeksleri sıfırdan değil birden başlar.

PostgreSQL'de Veriyi İşlemek için Fonksiyonlar

ARRAY'lerde arama

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'de Veriyi İşlemek için Fonksiyonlar

ARRAY işlevleri ve operatörleri

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'de Veriyi İşlemek için Fonksiyonlar

ARRAY işlevleri ve operatörleri

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'de Veriyi İşlemek için Fonksiyonlar

Haydi pratik yapalım!

PostgreSQL'de Veriyi İşlemek için Fonksiyonlar

Preparing Video For Download...