ARRAYs के साथ काम

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 एक विशेष टाइप

आइए दो array कॉलम वाला एक सरल टेबल बनाएँ.

CREATE TABLE grades (
    student_id int,
    email text[][],
    test_scores int[]
);
PostgreSQL में डेटा मैनिपुलेट करने के लिए फ़ंक्शंस

ARRAYS के साथ INSERT स्टेटमेंट्स

उदाहरण INSERT स्टेटमेंट:

INSERT INTO grades 
    VALUES (1, 
    '{{"work","[email protected]"},{"other","[email protected]"}}', 
    '{92,85,96,88}' );
PostgreSQL में डेटा मैनिपुलेट करने के लिए फ़ंक्शंस

ARRAYs को एक्सेस करना

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 array इंडेक्स 0 से नहीं, 1 से शुरू होते हैं.

PostgreSQL में डेटा मैनिपुलेट करने के लिए फ़ंक्शंस

ARRAYs में खोज

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...