来自数据库系统概念,用于对象关系数据库的 SQL 命令:
create type Publisher as
(name varchar(20),
branch varchar(20));
create type Book as
(title varchar(20),
author array varchar(20) array [10],
pub date date,
publisher Publisher,
keyword set varchar(20) multiset);
insert into books
values (’Compilers’, array[’Smith’, ’Jones’],
new Publisher(’McGraw-Hill’, ’New York’),
multiset[’parsing’, ’analysis’]);
create table flat_Book as
(title varchar(20),
author array varchar(20) array [10],
pub_name varchar(20),
pub_branch varchar(20));
select title, author, Publisher(pub_name, pub_branch) as publisher
from flat_books
group by title, author, publisher;
里面的Publisher(pub_name, pub_branch)
和Publisher()
是什么?
Publisher()
好像不是Publisher
类型的构造方法,因为调用构造方法需要new
,例如new Publisher('McGraw-Hill', 'New York')
。
我猜 PostgreSQL 可能有类似的命令,因为它很好地遵循 SQL 标准并且也是对象关系型 DBMS。
谢谢。
最佳答案
作为Laurenz Albe在他对昨天类似问题的回答中提到,create table T
在幕后做了一个隐式的 create type T
来创建一个与表具有相同结构的复合类型。
然后我们看type casts in the fine manual :
A type cast specifies a conversion from one data type to another. PostgreSQL accepts two equivalent syntaxes for type casts:
CAST ( expression AS type ) expression::type
[...]
It is also possible to specify a type cast using a function-like syntax:
typename ( expression )
和:
Note
The function-like syntax is in fact just a function call. When one of the two standard cast syntaxes is used to do a run-time conversion, it will internally invoke a registered function to perform the conversion. By convention, these conversion functions have the same name as their output type, and thus the “function-like syntax” is nothing more than a direct invocation of the underlying conversion function.
综合起来:
create table publisher
还执行create type publisher
。- 对于给定的类型
T
,T(expr)
是一个类型转换。 - 所以
publisher(a, b)
本质上是一个使用底层类型转换函数的类型转换。
审查 Composite Types文档的一部分也会有所帮助。
关于sql - `Publisher()` 中的 `Publisher(pub_name, pub_branch)` 是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51432380/