简体   繁体   English

将自定义类型的数组插入到Postgresql中的表中

[英]Insert an array of custom type into a table in postgresql

I'm studying SQL using PostgreSQL, but now I have a problem that I can't resolve. 我正在使用PostgreSQL学习SQL,但是现在我有一个无法解决的问题。 I have a type that I call Phones: 我有一种称为电话的类型:

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

And i have to create a Array of Phones into the table Students: 我必须在学生表中创建一个电话阵列:

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[] 
)

And I have created. 我创造了。 But, when i try insert into Students table like that: 但是,当我尝试像这样插入学生表时:

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

I got this error message : 我收到此错误消息

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"

How can i do that insert? 我该如何插入?

You have to explicitly cast an array to the type phone[] : 您必须将数组显式转换为phone[]类型:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM