sql - `Publisher()` 中的 `Publisher(pub_name, pub_branch)` 是什么?

标签 sql postgresql object-relational-model

来自数据库系统概念,用于对象关系数据库的 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
  • 对于给定的类型 TT(expr) 是一个类型转换。
  • 所以 publisher(a, b) 本质上是一个使用底层类型转换函数的类型转换。

审查 Composite Types文档的一部分也会有所帮助。

关于sql - `Publisher()` 中的 `Publisher(pub_name, pub_branch)` 是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51432380/

相关文章:

SQL - 组合多个类似的查询

c# - 从 .Net 3.5 应用程序的数据库表架构生成模型类的 C# 源代码

sql - 无法直接转换为十进制值?

sql - 在什么情况下 SELECT by PRIMARY KEY 会很慢?

sql - 如何在 Postgres 中对多个表中出现的事件求和

linux - 如何修复主机 "::1"没有 pg_hba.conf 条目

mysql - 我应该将成分存储为单独的表格还是列表

sql - 如何构建 SQL 以按日期获取分组数据

c# - 数据注释 vs Fluent API?支持各种 ORM 的最佳实践