sql - Oracle:在 PostgreSQL 中有与 ROW 等价的东西吗?

标签 sql oracle postgresql types insert

当我在 PostgreSQL 中实例化一个类型时,我可以做,

INSERT INTO mountain VALUES ('Meru',4567,ROW(6.8,-3.2));

并且 ROW(...) 将被类型转换为表中对应的类型。在 Oracle 中,我必须这样做:

INSERT INTO mountain VALUES ('Meru',4567,GeoCoord(6.8,-3.2));

并手动将类型放入 INSERT。

有没有办法让 Oracle 的行为像 Postgres 一样?

编辑:表定义

CREATE TABLE Mountain (
 Name VARCHAR(20) CONSTRAINT MountainKey PRIMARY KEY,
 Height NUMERIC CONSTRAINT MountainHeight
 CHECK (Height >= 0),
 Coordinates GeoCoord CONSTRAINT MountainCoord
 CHECK (((Coordinates).Longitude >= -180) AND 
        ((Coordinates).Longitude <= 180) AND
        ((Coordinates).Latitude >= -90) AND
        ((Coordinates).Latitude <= 90)));

最佳答案

Oracle 中 PostgreSQL 的 ROW 构造函数等价于 object or collection constructor ,正如您在发布的代码中所指出的那样。

然而,没有单数表达式可以创建所需类型的实例。来自Oracle 11g R2 database documentation ,显然必须显式调用构造函数来创建和引用类型:

To initialize a nested table or varray, you use a constructor, a system-defined function with the same name as the collection type. This function constructs collections from the elements passed to it.

You must explicitly call a constructor for each varray and nested table variable. Associative arrays, the third kind of collection, do not use constructors. Constructor calls are allowed wherever function calls are allowed.

此外,请注意默认构造函数适用于所有类型(关联数组除外),因此您实际上不需要编写自己的构造函数。

在考虑使用 CREATE TYPE 语句创建的架构级别类型时,行为也没有区别 - 在这种情况下也需要调用构造函数。来自documentation :

The system-defined attribute value constructor requires you to pass the constructor a value for each attribute of the type. The constructor then sets the attributes of the new object instance to those values

关于sql - Oracle:在 PostgreSQL 中有与 ROW 等价的东西吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4268615/

相关文章:

sql - 在几行中增加唯一值

postgresql - 基于 jsonb 字段中的多个属性更新 postgres jsonb

php - SELECT * FROM table WHERE field IN (SELECT id FROM table ORDER BY field2)

来自同一个表的 SQL 求和值

php - 如何更改 mysql 和 php 中的表列?

java - Apache Spark with Java,从 Oracle 中的 Varchar2 转换为日期类型失败

oracle - 数据加载到巨大的分区表

php - 如何在 PHP 中获取有关 SQL 警告的更多信息?

postgresql - 将 spark 数据框写入 postgres 数据库

ruby-on-rails - Rails 唯一列有重复项