简体   繁体   English

Postgresql:“ public”是在Postgresql中完全限定表名的标准方法吗?

[英]Postgresql: is “public.” an standard way to fully qualify table name in Postgresql?

If i don't qualify a "public." 如果我不符合“公众”的条件。 on account_category table, out account_category will have a conflict with account_category table name. 在account_category表上, account_category输出将与account_category表名称冲突。

Does "public." 是否“公开”。 also works on other rdbms? 也可以在其他rdbms上使用吗?

CREATE OR REPLACE FUNCTION X_RAIN(x VARCHAR, OUT user_id VARCHAR, out account_category varchar, out depth int) returns setof record
AS 
$$
BEGIN
     return query 
     select uar.account_id, c.account_category, c.depth
     from account_with_types_chart_of_account uar
     inner join public.account_category c using(account_category_id);
END;
$$ LANGUAGE 'plpgsql';

Regarding public in PostgreSQL, public is defined as the default schema name when no schema name is specified. 关于PostgreSQL中的public,当未指定任何模式名称时,public被定义为默认模式名称。 However, this can changed in the postgresql.conf file on the search_path = xxx line. 但是,这可以在search_path = xxx行的postgresql.conf文件中更改。 To see what your current default schemas are set to issue the following SQL command: 若要查看当前的默认架构设置为发出以下SQL命令,请执行以下操作:

SHOW_ search_path;

If you want to change your default schema path in your open query session, issue the following SQL command: 如果要在打开的查询会话中更改默认架构路径,请发出以下SQL命令:

SET search_path = new_path;

However, in the example you posted I believe that the naming conflict you are having problems with is not with the schema name but with the function parameter name account_category and the table name account_category. 但是,在您发布的示例中,我认为您遇到的命名冲突不是模式名称,而是函数参数名称 account_category和表名称account_category。 You could rename your parameter name to avoid this conflict. 您可以重命名参数名称以避免这种冲突。 In databases with many schemas, for clarities sake I often explicitly specify public at the start of database object names. 在具有许多模式的数据库中,为清楚起见,我经常在数据库对象名称的开头明确指定public。

Regarding your second question, I don't think PostgreSQL is unique in its usage of public, but I do know that many other databases do their schemas in a different way. 关于您的第二个问题,我认为PostgreSQL在公共用途上不是唯一的,但是我确实知道许多其他数据库以不同的方式来执行其模式。

Public is the default schema name. Public是默认架构名称。

For example, in MySQL it doesn't have schema (if I recall). 例如,在MySQL中,它没有架构(如果我记得的话)。 Also, if you use another schema instead of public, your code will break. 同样,如果您使用其他模式而不是公共模式,则代码将中断。

http://www.postgresql.org/docs/8.3/interactive/ddl-schemas.html http://www.postgresql.org/docs/8.3/interactive/ddl-schemas.html

I'd recommend using another variable name. 我建议使用另一个变量名。 There's probably another way too. 可能还有另一种方式。

The conflict happens because you used the same name for your variable and table name. 发生冲突是因为您为变量和表名使用了相同的名称。

This is a very bad choice of naming, and can lead to many problems. 这是一个非常糟糕的命名选择,并可能导致许多问题。

For example: 例如:

create function x (somename TEXT) returns bool as $$
declare
  tempbool int4;
begin
  select true INTO tempbool from some_table where somename = somename;
  return tempbool;
end;
$$ language plpgsql;

This code basically doesn't make any sense, because parser is not able to tell what "somename = somename" means. 这段代码基本上没有任何意义,因为解析器无法分辨“ somename = somename”的含义。 Same goes for table names (to some degree). 表名称(在某种程度上)也是如此。

Generally you want your identifiers (table names, column names, variable names) to be unique. 通常,您希望标识符(表名,列名,变量名)是唯一的。

I prefer to use "in_" prefix for arguments, but your choice of naming schema might be different. 我更喜欢对参数使用“ in_”前缀,但是您对命名模式的选择可能有所不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM