加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Postgresql中的奇怪行为

发布时间:2020-12-13 18:06:34 所属栏目:百科 来源:网络整理
导读:我是 Postgresql的新手,我正在尝试从 MySQL迁移我的应用程序. 我有一个具有以下结构的表: Table "public.tbl_point" Column | Type | Modifiers | Storage | Description ------------------------+-----------------------+-----------+----------+-------
我是 Postgresql的新手,我正在尝试从 MySQL迁移我的应用程序.
我有一个具有以下结构的表:
Table "public.tbl_point"
          Column         |         Type          | Modifiers | Storage  | Description
 ------------------------+-----------------------+-----------+----------+-------------
  Tag_Id                 | integer               | not null  | plain    |
  Tag_Name               | character varying(30) | not null  | extended |
  Quality                | integer               | not null  | plain    |
  Execute                | integer               | not null  | plain    |
  Output_Index           | integer               | not null  | plain    |
  Last_Update            | abstime               |           | plain    |
Indexes:
"tbl_point_pkey" PRIMARY KEY,btree ("Tag_Id")
Triggers:
add_current_date_to_tbl_point BEFORE UPDATE ON tbl_point FOR EACH ROW EXECUTE PROCEDURE update_tbl_point()
Has OIDs: no

当我使用libpq通过C程序运行查询时:

UPDATE tbl_point SET "Execute"=0 WHERE "Tag_Id"=0

我得到以下输出:

ERROR:  record "new" has no field "last_update"
CONTEXT:  PL/pgSQL function "update_tbl_point" line 3 at assignment

当我尝试使用pgAdminIII更改“执行”或任何其他列的值时,我得到完全相同的错误.

如果我将列名称从“Last_Update”更改为“last_update”,一切正常.

我在我的数据库中找到了与其他表相同的问题,并且该列始终显示为abstime或timestamp列.

您的update_tbl_point函数可能正在执行以下操作:
new.last_update = current_timestamp;

但它应该使用new.“Last_Update”所以修复你的触发器功能.

列名是normalized to lower case in PostgreSQL(与SQL标准所说的相反)但是双引号的标识符保持其大小写:

Quoting an identifier also makes it case-sensitive,whereas unquoted names are always folded to lower case. For example,the identifiers FOO,foo,and “foo” are considered the same by PostgreSQL,but “Foo” and “FOO” are different from these three and each other. (The folding of unquoted names to lower case in PostgreSQL is incompatible with the SQL standard,which says that unquoted names should be folded to upper case. Thus,foo should be equivalent to “FOO” not “foo” according to the standard. If you want to write portable applications you are advised to always quote a particular name or never quote it.)

所以,如果你这样做:

create table pancakes (
    Eggs integer not null
)

然后你可以做以下任何一件事:

update pancakes set eggs = 11;
update pancakes set Eggs = 11;
update pancakes set EGGS = 11;

它会起作用,因为所有三种形式都标准化为鸡蛋.但是,如果你这样做:

create table pancakes (
    "Eggs" integer not null
)

然后你可以这样做:

update pancakes set "Eggs" = 11;

但不是这个:

update pancakes set eggs = 11;

PostgreSQL的通常做法是在任何地方使用小写标识符,这样您就不必担心它.我也建议在其他数据库中使用相同的命名方案,不得不引用一切只是在SQL中留下一堆双引号(标准),反引号(MySQL)和括号(SQL Server),而不是让你成为朋友.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读