【问题标题】:Can we use "name" as a column name in PostgreSQL?我们可以在 PostgreSQL 中使用“name”作为列名吗?
【发布时间】:2018-05-09 05:06:42
【问题描述】:

我们可以在 PostgreSQL 中使用“name”作为列名吗?

我已经尝试过了,表创建成功。当我对表或表数据执行任何操作时会出错吗?

name是保留关键字吗?

更新:试过这个

select * from pg_get_keywords()

发现“name”是无保留的。还想确认我是否可以使用该名称?

【问题讨论】:

  • 我猜你已经回答了你自己的问题。
  • NAME 不是根据Postgres documentation 的保留关键字。但是,如果它出现在该列表中,您可能仍然不应该使用它。

标签: postgresql ddl create-table


【解决方案1】:

https://www.postgresql.org/docs/current/static/datatype-character.html#DATATYPE-CHARACTER-SPECIAL-TABLE

“name”是对象名称的内部类型

所以是的 - 你可以:

t=# create table "b@dPract!c3" ("name" text, "date" timestamptz);
CREATE TABLE
t=# insert into "b@dPract!c3" values('wrong',now());
INSERT 0 1
t=# select * from "b@dPract!c3";
 name  |             date
-------+-------------------------------
 wrong | 2018-05-09 08:38:53.612212+01
(1 row)

你应该这样做吗?肯定不是!用数据类型名称命名列很容易,但非常糟糕的做法

【讨论】:

  • 我会说最好避免任何需要引用的名称。它使使用更加困难。 +1
猜你喜欢
  • 2021-09-15
  • 1970-01-01
  • 2023-03-24
  • 1970-01-01
  • 2017-06-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-23
相关资源
最近更新 更多