【问题标题】:creating a table with column names having brackets创建一个带有括号的列名的表
【发布时间】:2020-11-30 02:46:51
【问题描述】:

我尝试使用 postgresql 查询创建表:

CREATE TABLE customer_account(ID_account integer primary key, customer_name (lastname) text);

但它给出了错误消息:

ERROR:  syntax error at or near "("
LINE 5: customer_name (lastname) text,

可能问题出在括号里,我已经试过了

CREATE TABLE customer_account("ID_account" primary key, "customer_name (lastname)" text);

但它也给了我类似的错误信息。

如何更正查询?我真的需要使用括号。

【问题讨论】:

  • @a_horse_with_no_name 我已经尝试使用 " 但它不起作用。或者我应该在不同的位置添加 " 吗?

标签: postgresql quoted-identifier


【解决方案1】:

使用" 工作,但您缺少主键的数据类型:

CREATE TABLE customer_account
(
  "ID_account" integer primary key, 
             --^ here
  "customer_name (lastname)" text
);

Online example


但我强烈建议您不要使用带引号的标识符。

从长远来看,它们会给你带来更多的麻烦,那么它们是值得的。

我建议使用这样的东西:

CREATE TABLE customer_account
(
  account_id        integer primary key, 
  customer_lastname text
);

(“ID”作为前缀在英语中听起来很奇怪)

【讨论】:

  • 谢谢。我忘记了主键中的添加整数。我已经添加了数据类型并使用了“”,但是它不起作用。我知道使用报价,但如果必须,我应该怎么做?
  • 好吧,显然你没有向我们展示你正在运行的代码。正如我在online example 中展示的那样,我的代码有效。但说真的:不要再努力了,不要使用带引号的标识符。期间。
猜你喜欢
  • 1970-01-01
  • 2017-07-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多