PostgreSQLã§è¤ååã®ã«ã©ã ã使ã£ã¦ã¿ã
ååã®é
ååã«ç¶ããä»åº¦ã¯è¤ååã使ã£ã¦ã¿ãã
ãã®è¤ååã¨ããã®ã¯è¤æ°ã®ãã£ã¼ã«ãã®çµãä¸ã¤ã®ãã¼ã¿åã¨ãã¦æ±ãããã¡ããã©Cè¨èªã®æ§é ä½ã®ãããªæ©è½ã¨ãªãã
ååã®è¨äºï¼PostgreSQLで配列型のカラムを使ってみる - ぱせらんメモ
ä½ã¯ã¨ããããã¼ã¿åãä½æãã
SQLã¯ãããªæãã
CREATE TYPE shop_item AS ( name varchar(100), -- ååå category varchar(20), -- ã«ãã´ãªå price numeric -- ä¾¡æ ¼ );
CREATE TABLEã¨ä¼¼ããããªæãã ããååã¨å®ç¾©ã®å¾ã«ASãå¿
è¦ãªãã¨ã¨ãç¾å¨ã®ã¨ããå¶ç´ï¼NOT NULLãªã©ï¼ãã¤ããããªãã¨ããç¹ãç°ãªãã
ä½æãããã¼ã¿åãå©ç¨ãããã¼ãã«ãä½æãã
CREATE TABLE stocks ( item shop_item, -- åå count integer -- å¨åº«æ° );
ãã©ã¤ããªãã¼å¶ç´ãã¤ãããã¨æã£ãã次ã®ãããªã¨ã©ã¼ãåºã¦ãã¾ã£ãã
ERROR: data type shop_item has no default operator class for access method "btree" HINT: You must specify an operator class for the index or define a default operator class for the data type.
ã©ãããshop_itemã«å¯¾ããæ¼ç®åãå®ç¾©ãã¦ãããªããã°ãªããªããããã
é£ããããªã®ã§ä»åã¯æ念ã
ãã¼ã¿ãå ¥ãã¦ã¿ã
ãã¼ãã«ãä½æã§ããã®ã§æ©éãã¼ã¿ãæå ¥ãã¦ã¿ãã
INSERT INTO stocks (item, count) VALUES ('(ãªã¬ã³ã¸,æç©,100)', 10);
é
åã®æã¨åãããã«æ¬å¼§ã®ä¸ã«é
ç®ãã«ã³ãåºåãã§ä¸¦ã¹ã¦æå®ããããã¯ãå
¨ä½ãã·ã³ã°ã«ã¯ã©ã¼ãã§æ¬ã£ã¦ãããå¿
è¦ããããã«ã³ããæ¬å¼§ãªã©ãå«ã¾ããå ´åã¯ããã«åã
ã®è¦ç´ ãããã«ã¯ã©ã¼ãã§æ¬ãå¿
è¦ãããã
é
åã®ã¨ãã®ARRAYæ§æã®ããã«ROWæ§æã使ã£ãæ¸ãæ¹ãåºæ¥ãããã¡ãã®ã»ããåã
ã®è¦ç´ ãã¯ã©ã¼ãã§æ¬ããã®ã§ãæè¦çã«ã¯ããããããã
INSERT INTO stocks (item, count) VALUES (ROW('ããã', 'æç©', 120), 20);
ROWã®ä¸ã«äºã¤ä»¥ä¸ã®ãã£ã¼ã«ããããå ´åã¯ROWãã¼ã¯ã¼ããçç¥ãã¦('ããã', 'æç©', 120)
ã ããæ¸ããã¨ãåºæ¥ãï¼ä¾ã¯å²æï¼ã
ãã¼ã¿ãæ½åºãã¦ã¿ã
ãã¼ã¿ãä½æã§ããã®ã§ãä»åº¦ã¯ãããåãåºãã¦ã¿ãã
SELECT item, count FROM stocks;
item | count
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- -
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
ãã®ãããªæãã§åºã¦ããã
è¤ååã®ç¹å®ã®ãã£ã¼ã«ãã«ã¢ã¯ã»ã¹ããå ´åã¯ãããã¨ãã£ã¼ã«ãåã§æå®ãããä½ãããã®æ§æã¯ãã¼ãã«.ãã£ã¼ã«ã
ã¨åãã§ãããããæ¬å¼§ã使ç¨ããªããã°ãªããªãå ´åãããã
[æ¬å¼§ãã¤ããªãã£ãå ´å]
SELECT item.name, count FROM stocks;
ERROR: missing FROM-clause entry for table "item" LINE 1: SELECT item.name, count FROM stocks;
[æ¬å¼§ãã¤ããå ´å]
SELECT (item).name, count FROM stocks;
name | count
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- -
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
æ¤ç´¢ãã¦ã¿ã
WHEREå¥ãæå®ãã¦æ¤ç´¢ãã¦ã¿ãã
SELECT item, count FROM stocks WHERE (item).price > 100;
item | count
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- -
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
é åã¨ã¯ç°ãªãè¤ååã®ãã£ã¼ã«ããã¾ã¨ãã¦æ±ããããªãã¨ã¯åºæ¥ãªãã
SELECT item, count FROM stocks WHERE (item).name LIKE 'ãª%';
item | count
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- -
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
é
åã®æã¨ã¯éãLIKEãæ£å¸¸ã«åä½ããã
ã¾ãéå(?)ã«å¯¾ããæä½ã§ã¯ãªãã®ã§å½ç¶ã ãâ¦â¦ã
æ´æ°ãã¦ã¿ã
è¤ååã®ãã¼ã¿ãæ´æ°ãã¦ã¿ãã
UPDATE stocks SET item = '(ãã¤ãããã«,æç©,260)' WHERE (item).name = 'ããã';
[æ´æ°å¾] item | count
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- -
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
ä¸ã®ä¾ã¯itemã丸ãã¨å¤æ´ããä¾ãINSERTã®æã¨åæ§ã«ROWæ§æã使ããã¨ãåºæ¥ãï¼ä¾ã¯çç¥ï¼ã
次ã®ããã«é
åã®ç¹å®ã®è¦ç´ ã ãæ´æ°ãããã¨ãåºæ¥ãã
UPDATE stocks SET item.price = 240 WHERE (item).name = 'ãã¤ãããã«';
[æ´æ°å¾] item | count
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- -
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
ããã§æ³¨æãããã®ã¯SETã§æå®ãããã£ã¼ã«ãã®é¨åã§ãããã§ã¯æ¬å¼§ã§æ¬ãå¿
è¦ã¯ãªããã¨ãããæ¬ãã¨ã¨ã©ã¼ã«ãªãã
ãã¹ãè¨èªããã®å©ç¨
è¤ååããã¹ãè¨èªã§æ±ã£ãå ´åã«æããã¦ã©ããªãã®ãã
ä»åã¯èª¿ã¹ã¦ãããªãã®ã ããæããé
åã®æã¨åæ§ã«(ãªã¬ã³ã¸,æç©,100)
ã¨ããæååã§è¿ã£ã¦ããã®ã§ã¯ãªããã¨æãããã
Javaã®å ´åã¯java.sql.ResultSet#getMetaData()ã§èª¿ã¹ã¦ã¿ãã¨ãããjava.sql.Types.OTHERåã¨ãã¦èªèããã¦ããã
çµå±è¤ååã¯ã©ããªã®ã
ãã¯ãé
åã®æã¨åæ§ã«å¥ãã¼ãã«ã«æ¬ãåºãã®ãç´ ç´ã ã¨æãã®ã§ãç¹å¥ãªçç±ã§ããªãéãã¯å¿
è¦ãªãã¨æãããã
ãã¹ãè¨èªããè¦ã¦ãæ±ãã«ããããªã®ã¯æããã ãã
â åè
http://www.postgresql.jp/document/pg823doc/html/rowtypes.htmlï¼日本PostgreSQLユーザ会 | 日本PostgreSQLユーザ会ï¼