【问题标题】:Syntax for array of nested composite type without using ARRAY[]不使用 ARRAY[] 的嵌套复合类型数组的语法
【发布时间】:2016-06-28 20:40:09
【问题描述】:
CREATE TYPE pencil_count AS(
    pencil_color varchar(30),
    count integer
);

CREATE TYPE pencil_count_with_date(
date_ date,
pencil_count pencil_count[]
);

CREATE TABLE pencils(id serial, pencils_ pencil_count_with_date[]);

INSERT INTO pencils(pencils_) 
VALUES('{"(\"2016-03-13\",{"(\"blue\",1)","(\"red\",2)"})"}');

如果我想在不使用ARRAY[...] 的情况下添加此复合数组,正确的语法是什么?

【问题讨论】:

  • 请在您的前两个命令中添加一些;。 DDL 也需要分号...
  • (1) 你为什么不想使用array[...],这让一切变得如此简单。 (2) 这和your other question recent question不一样吗?
  • @mu 太短了,我觉得不够

标签: sql arrays postgresql postgresql-9.4


【解决方案1】:

当您添加新的嵌套级别时,使用文字字符串的可读性会降低:

CREATE TYPE pencil_count AS(pencil_color varchar(30)
       ,"count" int);

CREATE TYPE pencil_count_with_date AS(date_ date
                                     ,pencil_count pencil_count[]);

CREATE TABLE pencils(id serial, pencils_ pencil_count_with_date[]);

INSERT INTO pencils(pencils_) 
VALUES('{"(
               \"2016-03-13\",
               \"{
                   \"\"(Blue,5)\"\",
                   \"\"(Red,2)\"\"
               }\"
          )"}');   


SELECT pencils_[1].pencil_count[1].pencil_color
FROM pencils;

SqlFiddleDemo

备注:

  1. 您需要根据嵌套级别使用" 转义\ 引用每个级别。
  2. 这种模式看起来像是试图将 OO 世界引入数据库。它可能比标准化版本更难维护和更慢。 Related question.
  3. 如果需要,您可以使用 $$ 引用字符串文字。
  4. 使用ARRAYROW 可能更容易发现每个关卡的开始和停止位置。

【讨论】:

    【解决方案2】:

    再次询问 Postgres。扩展your previous question的过程:

    CREATE TEMP TABLE pencil_count (
      pencil_color varchar(30)
    , count integer
    );
    
    CREATE TABLE pencil_count_with_date (
     date_ date,
     pencil_count pencil_count[]
    );
    
    CREATE TABLE pencils (
      id serial
    , pencils_ pencil_count_with_date[]
    );
    

    向 Postgres 询问每个嵌套级别:

    INSERT INTO pencil_count VALUES ('red' , 1), ('blue', 2);
    
    SELECT ARRAY(SELECT p FROM pencil_count p)::text AS p_row_arr;
    
    -- with result from above:
    INSERT INTO pencil_count_with_date(date_, pencil_count)
    VALUES ('2016-04-14', '{"(red,1)","(blue,2)"}')
         , ('2016-04-14', '{"(red,3)","(blue,4)"}');
    
    SELECT ARRAY(SELECT p FROM pencil_count_with_date p)::text AS p2_row_arr;
    
    -- with result from above:
    INSERT INTO pencils(pencils_)
    VALUES
      ('{"(2016-04-14,\"{\"\"(red,1)\"\",\"\"(blue,2)\"\"}\")"
        ,"(2016-04-15,\"{\"\"(red,3)\"\",\"\"(blue,4)\"\"}\")"}');
    
    SELECT id, pencils_::text FROM pencils;
    

    结果:

    id | pencils_
    ---+-------------------------------------------------------
    1  | {"(2016-04-14,\"{\"\"(red,1)\"\",\"\"(blue,2)\"\"}\")"
         ,"(2016-04-15,\"{\"\"(red,3)\"\",\"\"(blue,4)\"\"}\")"}
    

    SQL Fiddle.

    到目前为止,我完全同意建议:多级嵌套行类型通常是复杂且低效的。考虑标准化。

    我对您之前问题的回答中的更多内容:

    【讨论】:

    • 如果我们使用嵌套复合数组作为输入参数传递给 postgresFunction 并将它们插入规范化表中是否可以?如果有例子,请欣赏。示例 - 每个货件有多个货件和多个联系地址。我们想在一个 postgres 函数调用中加载 100 批货物。
    猜你喜欢
    • 2016-06-27
    • 2013-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-06
    • 2016-10-29
    • 2013-03-22
    • 2012-08-10
    相关资源
    最近更新 更多