duanbei6427 2018-09-24 19:32
浏览 333
已采纳

PostgreSQL表行在更新时会显示在底部

I am currently migrating from MySQL to PostgreSQL in a Laravel application, and I noticed that when updating, the row goes to the end of the table (bottom).

In the application I know I can use ORDER BY to sort, but I am referring to the internal behavior of the database while performing the UPDATE action.

In Mysql, it remains in the same position it occupied before the update. Is there any way to apply this function? Would it be a InnoDB feature? Using Navicat Premium 12.1 DBMS.

I think this is just an aesthetic factor, but even so I would like to learn how to carry out this "permanent ordination".

The database is in UTF-8 encoding and pt_BR.UTF8 collation and ctype. Following is the table:

CREATE TABLE `properties` (
   `id` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
   `title` varchar(255) NOT NULL,
   `description` text NOT NULL,
   `name` varchar(255),
   `rental_price` decimal(10, 2),
   `sale_price` decimal(10, 2)
);

Thank you all!

  • 写回答

1条回答 默认 最新

  • dongyuyi5680 2018-09-24 20:04
    关注

    Part 1: Generally use ORDER BY

    If you do not use the ORDER BY statement, both MySQL and PostgreSQL (and for that matter most relational DBMS systems) do not make any promises about the order of records.

    You should refactor your application to use the ORDER BY statement. If you want your data set to be ordered by newest first, you could use something like:

    SELECT * FROM yourtable ORDER BY id DESC;
    SELECT * FROM youtable ORDER BY creation_date DESC; -- if your table has such a column
    

    Similarly, you can have oldest objects first by using one of the following:

    SELECT * FROM yourtable ORDER BY id ASC;
    SELECT * FROM youtable ORDER BY creation_date ASC; -- if your table has such a column
    

    Part 2: Looking into the mechanics

    You added to your question a more detailed inquiry:

    [...] I know I can use ORDER BY to sort, but I am referring to the internal behavior of the database while performing the UPDATE action.

    There is multiple things that influence the sequence of database records displayed on your screen, when performing a query. In a real life application, it is not (practially) possible to predict this sequence.

    I assume this is simply an effect of PostgreSQL creating a new record for the updated record as can be found here in the Updating a Row section. I suggest to not rely on this behvaiour in any of your applications.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据