使用“()”和“.”引用列有什么区别?

What is the difference between referencing a column using "()" and "."?

我尝试使用以下语句创建联接 table:

CREATE TABLE directors_and_films(
id serial PRIMARY KEY,
directors_id int REFERENCES directors.id
films_id int REFERENCES films.id,
);

这会导致 Postgres 响应:

ERROR: schema "films" does not exist

当我把它改成:

CREATE TABLE directors_films (
  id serial PRIMARY KEY,
  director_id integer REFERENCES directors (id),
  film_id integer REFERENCES films (id)
);

代码执行良好。

我的问题是使用 () 访问列与使用句点有什么区别? SQL 这两者一般有什么区别?

Postgres 确实支持列引用的函数符号 属性符号。所以这个 works for a table tbl with a column col:

SELECT col(tbl) FROM tbl;

The manual:

but this usage is deprecated since it's easy to get confused

参见:

  • Store common query as column?

但这与手头的案件无关。 CREATE TABLE 语句中 FK 约束的简短语法要求在引用的列周围加上括号。 (显然,在您的示例中, column 约束只能引用单个列。)您尝试过的属性表示法 (directors.id) 是此处的语法错误。
仅此而已。 The manual:

REFERENCES reftable [ ( refcolumn ) ] [ MATCH FULL | MATCH PARTIAL | MATCH SIMPLE ]