PostgreSQLã§é ååã®ã«ã©ã ã使ã£ã¦ã¿ã
PostgreSQLã§ã¯ã«ã©ã ã®ãã¼ã¿åã«é åã使ããã¨ãåºæ¥ãããã¡ããSQL99æºæ ãªã®ããªã
ä½ã¯ã¨ããããã¼ãã«ãä½æãã
SQLã¯ãããªæãã
CREATE TABLE favorites ( username varchar(20), -- ã¦ã¼ã¶å category varchar(20), -- ã«ãã´ãªå items varchar(100)[], -- ã¢ã¤ãã åï¼è¤æ°ï¼ PRIMARY KEY(username, category) );
itemsã®é¨åãé
åãããã°ã©ãã³ã°è¨èªã®ããã«ååã«æ¬å¼§ãä»ããå½¢ã§æå®ããã
æ¬å¼§å
ã«è¦ç´ æ°ãæå®ãããã¨ãã§ããããç¾å¨ã®å®è£
ã§ã¯è¦ç´ æ°ã®æå®ã¯ç¡è¦ãããç¡å¶éã¨ãã¦æ±ãããã¨ã®ãã¨ï¼ä½ãSQL99ã®ä»æ§ã§ã¯è¦ç´ æ°ã¯åºå®ã«ããã§ããªãï¼ã
ã¾ããé
åã®å®£è¨æ¹æ³ã¯SQL99ã«æºæ ããitems varchar(100) ARRAY[10]
ã®ãããªæ¸ãæ¹ãåºæ¥ãããã®å ´åè¦ç´ æ°ã®æå®ã¯å¿
é ã¨ãªãããç¾å¨ã®å®è£
ã§ã¯ãã¯ãä½ãæå®ãã¦ãç¡å¶éã¨ãã¦æ±ãããã¨ã®ãã¨ã
items varchar(100)[][]
ã®ããã«æ¸ãã°å¤æ¬¡å
é
åãå¯è½ã
ãã¼ã¿ãå ¥ãã¦ã¿ã
ãã¼ãã«ãä½æã§ããã®ã§æ©éãã¼ã¿ãæå ¥ãã¦ã¿ãã
INSERT INTO favorites (username, category, items) VALUES ('ã±ããã', 'æç©', '{ãªã¬ã³ã¸, ã¡ãã³, ããã}');
ããã°ã©ãã³ã°è¨èªã®é
åãæ§é ä½ã®ãããªå½¢ã§æå®ãããä½ãå
¨ä½ãã·ã³ã°ã«ã¯ã©ã¼ãã§æ¬ã£ã¦ãããå¿
è¦ããããã«ã³ããä¸æ¬å¼§ãªã©ãå«ã¾ããå ´åã¯ããã«åã
ã®è¦ç´ ãããã«ã¯ã©ã¼ãã§æ¬ãå¿
è¦ãããã
ãã¡ãã次ã®ããã«SQL99æºæ ãªæ¸ãæ¹ãã§ããããã¡ãã®ã»ããåã
ã®è¦ç´ ãã¯ã©ã¼ãã§æ¬ããã®ã§ãæè¦çã«ã¯ãããããããããããªãã
INSERT INTO favorites (username, category, items) VALUES ('ã±ããã', 'è²', ARRAY['Black', 'Green', 'Blue']);
ãã¼ã¿ãæ½åºãã¦ã¿ã
ãã¼ã¿ãä½æã§ããã®ã§ãä»åº¦ã¯ãããåãåºãã¦ã¿ãã
SELECT username, category, items FROM favorites;
username | category | items
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- -
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
ãã®ãããªæãã§åºã¦ããã
ç¹å®ã®ã¤ã³ããã¯ã¹ã ãåå¾ããå ´åã¯æ·»åãæå®ãããæ·»åã¯1ããå§ã¾ãã
SELECT username, category, items[1] FROM favorites;
username | category | items
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- -
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
æ·»åé¨åã«lower:upperã¨ããæå®ããããã¨ã§ç¹å®ã®ç¯å²ãæ½åºãããã¨ãåºæ¥ãã
次ã®ä¾ã¯2çªç®ã¨3çªç®ã®è¦ç´ ãããªãå¯é
åã¨ãã¦æ½åºããã
SELECT username, category, items[2:3] FROM favorites;
username | category | items
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- -
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
ç¯å²å¤ãNULLãæ·»åã«æå®ããå ´åã¯ã¨ã©ã¼ã«ã¯ãªããã«NULLãè¿ãã
SELECT username, category, items[NULL] FROM favorites;
SELECT username, category, items[20] FROM favorites;
username | category | items
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- -
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
æ¤ç´¢ãã¦ã¿ã
WHEREå¥ãæå®ãã¦æ¤ç´¢ãã¦ã¿ãã
SELECT username, category, items FROM favorites WHERE items[1] = 'ãªã¬ã³ã¸';
username | category | items
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- -
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
é
åã®é·ããããã£ã¦ããæã¯items[1] = 'A' OR items[2] = 'A' ...
ã¨ããããã«æ¸ããã¨ãåºæ¥ãããé·ããä¸æãªå ´åãé常ã«é·ãå ´åã«ã¯å°ãã®ã§ANYæ§æã使ãï¼SOMEã§ãããï¼ã
åæ§ã«å
¨ã¦ã®å¤ã«å¯¾ãã¦è©ä¾¡ãè¡ãALLãããï¼ä¾ã¯å²æï¼ã
SELECT username, category, items FROM favorites WHERE 'ãªã¬ã³ã¸' = ANY (items);
username | category | items
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- -
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
ãã¿ã¼ã³ããããåæ§ã«è¡ãããã®ã ããä½æ
ãä¸æããããªãã£ãï¼2007å¹´3æ1æ¥ç¾å¨ï¼ã
SELECT username, category, items FROM favorites WHERE 'ãª%' LIKE ANY (items);
[æå¾ ããçµæ] username | category | items
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- -
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- -
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
ä»»æã®æ¡ä»¶ã«åè´ããè¦ç´ ãåå¾ããæ¹æ³ã¯è¦ã¤ãããªãã£ãï¼ä¾ãã°ã¡ãã³ãä½çªç®ã®è¦ç´ ãªã®ãããªã©ï¼ã
ã¹ãã¢ãããã·ã¼ã¸ã£ãªã©ãé§ä½¿ããã°åºæ¥ãã®ãããããªãããä¸çºã§åºæ¥ããããªæ¹æ³ã¯ãªãããã ã
æ´æ°ãã¦ã¿ã
é åã®ãã¼ã¿ãæ´æ°ãã¦ã¿ãã
UPDATE favorites SET items = '{ãªã¬ã³ã¸, ãã¤ãããã«, ããã}' WHERE username = 'ã±ããã' AND category = 'æç©';
[æ´æ°å¾] username | category | items
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- -
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
ä¸ã®ä¾ã¯itemsã丸ãã¨å¤æ´ããä¾ãINSERTã®æã¨åæ§ã«ARRAYæ§æã使ããã¨ãåºæ¥ãï¼ä¾ã¯çç¥ï¼ã
次ã®ããã«é
åã®ç¹å®ã®è¦ç´ ã ãæ´æ°ãããã¨ãåºæ¥ãï¼ãããããã ã平仮åã ã£ãã®ã§çä»®åã«ãã¦ã¿ãï¼ã
UPDATE favorites SET items[3] = 'ãªã³ã´' WHERE username = 'ã±ããã' AND category = 'æç©';
[æ´æ°å¾] username | category | items
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- -
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
ä»»æã®æ¡ä»¶ã«åè´ããè¦ç´ ãåå¾ã§ãããã«ãªãã®ã§ããé
åä¸ã®ããããããããªã³ã´ãã«ãããã¨ãã£ãSQLã¯æ¸ããªãããã ã
ãã¹ãè¨èªããã®å©ç¨
é
åããã¹ãè¨èªã§æ±ã£ãå ´åã«æããã¦ã©ããªãã®ãã
èªåã§ã¯ã¾ã 試ãã¦ããªãã®ã ãããã£ã¨èª¿ã¹ãã¨ããã«ããã¨PHPã§ã¯é
åã«ã¯å¯¾å¿ãã¦ããªããããã
æ®å¿µãªãã{ãªã¬ã³ã¸,ãã¤ãããã«,ãªã³ã´}
ã¨ããæååã§è¿ã£ã¦ããã®ã§ãèªåã§å解ããå¿
è¦ãããããã ã
Javaã¯JDBCãã©ã¤ãã®å®è£ ã«ããã®ãããããªãããjava.sql.Arrayåã¨ãã¦æ±ããã¨ãã§ããããã ã
Perlâ¦â¦æªèª¿æ»ã
Rubyâ¦â¦æªèª¿æ»ã
çµå±é åã¯ã©ããªã®ã
åºæ¬çã«ã¯é
åçãªãã¼ã¿æ§é ã«ãªã£ãå ´åã¯æ£è¦åãã¦å¥ãã¼ãã«ã«æ¬ãåºããã¨ã«ãªãã¯ããªã®ã§ãä½ãç¹å¥ãªçç±ã§ããªãéãã¯é
ååãå©ç¨ããå¿
è¦ã¯ãªãã¨æãããã
å®éPostgreSQLã®ããã¥ã¢ã«ã«ã次ã®ããã«æ¸ããã¦ããã
ãã£ãã: é åã¯éåã§ã¯ããã¾ãããç¹å®ã®é åè¦ç´ ã«æ¤ç´¢ãããããã¨ã¯ãã¼ã¿ãã¼ã¹è¨è¨ã誤ã£ã¦ããå¯è½æ§ã示åãã¦ãã¾ããé åã®è¦ç´ ã¨ã¿ãªãããããããã®é ç®ãè¡ã«æã¤å¥ã®ãã¼ãã«ã使ããã¨ãæ¤è¨ãã¦ãã ããããã®æ¹ãæ¤ç´¢ãããç°¡åã«ãªãè¦ç´ æ°ã大ãããªã£ã¦ãæ¡å¼µæ§ãããã¾ãã
ãã¹ãè¨èªããè¦ã¦ãæ±ãã«ãããã¨ãã®ä¸ãªããããªã®ã§ãåºæ¥ãã°ä½¿ããªãã»ããããããã ã
ã¡ãªã¿ã«ã使ããªãã»ããããã¨æãããé ååãããããåºã¦ããçç±ã«ã¤ãã¦ã¯ã新しい業界標準「SQL99」詳細解説ï¼ITエキスパートのための問題解決メディア - @ITï¼ã§æ¬¡ã®ããã«æ¸ããã¦ããã
åã®ä¸ã«é åã許ãã¨ãããã¨ã¯ããªã¬ã¼ã·ã§ãã«ã¢ãã«ã®èãæ¹ããããã°ã第ä¸æ£è¦å½¢ã«éåããéæ£è¦å½¢ãªã®ã§ã¨ã¦ã許ãããªããã¨ã®ããã«æããããã¦ã¼ã¶ã¼ãå¸å ´ã®å¼·ãè¦æ±ã«ãã£ã¦å®ç¾ãããå®éãSQL99ã®é¢é£è¦æ ¼ã§ããSQL/MMã¯ããã«ããã¹ãã空éãã¼ã¿æ å ±ã表ç¾ããã®ã«é ååãå¤ç¨ãã¦ããã®ã§ãä¸å±¤æ¨æºåãæ¥ãããã
èªåã«ã¯ãã®è¾ºã®ç¥èããªãã®ã§ã©ã®ãããªå®æ
ãè¦æãããã®ãã¯ããããªããã空éãã¼ã¿ã¨ããããããã{x座æ¨,y座æ¨,z座æ¨}ã®ããã²ã¨ã¾ã¨ã¾ãã®ãã¼ã¿ãæ±ãã®ã«é½åãããã®ãããããªãã
ãã£ã¨ãPostgreSQLã«ã¯å¹¾ä½ãã¼ã¿ç¨ã®ãã¼ã¿åãå¥éç¨æããã¦ããããã¦ã¼ã¶å®ç¾©ã®è¤ååãå®ç¾©ãããã¨ãã§ããã®ã§ãé
åã§ãããã表ç¾ããå¿
è¦ã¯ãªãããã ã
â åè
http://www.postgresql.jp/document/pg823doc/html/arrays.htmlï¼日本PostgreSQLユーザ会 | 日本PostgreSQLユーザ会ï¼
http://www.akimoto-jp.com/java/Database/jdbcSQL99.htmlï¼http://www.akimoto-jp.com/java/index.htmlï¼
新しい業界標準「SQL99」詳細解説ï¼ITエキスパートのための問題解決メディア - @ITï¼
ç¶ããã©ããï¼PostgreSQLで複合型のカラムを使ってみる - ぱせらんメモ