Array का Type
PostgreSQL columns किसी भी type के arrays hold कर सकते हैं, इन्हें search करने के लिए operators के साथ।
In this page:
Syntax
column_name integer[]
INSERT INTO table_name (column) VALUES (ARRAY[1, 2, 3]);
SELECT column[1] FROM table_name;
Array Type
एक column को integer[] या text[] declare करें और values को ARRAY[1,2,3] या '{1,2,3}' की तरह लिखें।
arr[1] से elements index करें (arrays 1-based हैं), = ANY(arr) से membership test करें, @> से containment, और && से overlap। unnest एक array को rows में बदलता है और array_agg rows को एक array में बदलता है। Arrays छोटी, simple lists के लिए suit करते हैं, relationships के लिए नहीं।
Note:
PostgreSQL arrays 1-indexed हैं, 0-indexed नहीं।
उदाहरण: Array type
shop=# CREATE TABLE posts (id serial PRIMARY KEY, title text, tags text[]);
shop=# INSERT INTO posts (title, tags) VALUES ('Intro', ARRAY['sql','postgres']), ('Tips', ARRAY['postgres','index']);
shop=# SELECT title FROM posts WHERE 'index' = ANY(tags);
title
-------
Tips
shop=# SELECT title, tags[1] AS first_tag FROM posts;
title | first_tag
-------+-----------
Intro | sql
Tips | postgres
shop=# SELECT unnest(tags) AS tag, count(*) FROM posts GROUP BY 1 ORDER BY 1;
tag | count
----------+-------
index | 1
postgres | 2
sql | 1
⚠️ Run this in your own terminal or Node.js environment.
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the
view already swaps in the hi_ translation fields (or blanks these
out if untranslated), so this renders correctly for both languages
without a lang_code check here. #}
आम गलतियां
- ऐसे data के लिए arrays इस्तेमाल करना जो एक related table होना चाहिए
- यह भूल जाना कि arrays 1 से शुरू होते हैं
- बिना एक GIN index के fast searches की उम्मीद करना
चैप्टर सारांश
- कोई भी type एक array हो सकता है
- Arrays 1-based हैं
- ANY, @>, और && arrays search करते हैं
- unnest और array_agg convert करते हैं
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: