【问题标题】:Insert an array of custom type into a table in postgresql将自定义类型的数组插入到 postgresql 中的表中
【发布时间】:2016-03-05 07:10:59
【问题描述】:

我正在使用 PostgreSQL 学习 SQL,但现在我遇到了一个无法解决的问题。我有一种我称之为电话的类型:

CREATE TYPE Phone as 
(
  DDD varchar(3),
  Number Varchar(10),
  TypeOf Varchar(15)
)

我必须在学生表中创建一个电话数组:

CREATE TABLE Students
(
   Id INT NOT NULL,
   PRIMARY KEY (Id),
   name Varchar(50),
   status Char,
   codCourse int,
    FOREIGN KEY (codCourse )  REFERENCES Course (IdCourse) ON DELETE CASCADE,
   phones Phone[] 
)

我已经创造了。 但是,当我尝试像这样插入学生表时:

INSERT INTO Students (Id, name, status, codCourse , phones) 
VALUES (
    00001, 'Joaquim Soares Fernando', 'I', 4,       
    Array[('22','33548795','Telefone')])

我收到此错误消息

ERROR: column "phones" is of type phone[] but expression is of type record[]
SQL state: 42804
Hint: You will need to rewrite or cast the expression.
Character: 118"

我该如何插入?

【问题讨论】:

    标签: sql arrays postgresql types


    【解决方案1】:

    您必须将数组显式转换为 phone[] 类型:

    INSERT INTO students (id, name, status, codcourse , phones) 
    VALUES (
        00001, 'Joaquim Soares Fernando', 'I', 4, 
        array[('22','33548795','Telefone')]::phone[]);
    

    【讨论】:

      猜你喜欢
      • 2017-04-13
      • 2018-12-15
      • 2022-11-17
      • 2021-11-11
      • 1970-01-01
      • 1970-01-01
      • 2013-06-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多