当我在 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/