我遇到了这样一个问题:更新后,我的行被“隐藏”(或select*无法检索它们)。
我尝试过使用cli和gui(dbeaver),但结果是一样的,下面是一个示例:
select * from users limit 4;
id | email | password | status | role | created_at | updated_at
----+--------------------------------+-----------------+----------+--------+----------------------------+----------------------------
8 | Brad.Bailey@gmail.com | qYHsmrKWaiaiZxI | disabled | seller | 2019-09-06 21:43:08.043-03 | 2019-08-13 16:04:25.233-03
9 | Marcelino_Prohaska97@gmail.com | sUMuOM_gXCPxz19 | disabled | seller | 2019-06-14 15:39:45.447-03 | 2019-06-25 12:54:01.023-03
10 | Gino_Blick@gmail.com | iOkZQhc7JSsQcpY | disabled | seller | 2020-02-13 13:39:16.26-03 | 2019-12-18 17:02:37.938-03
11 | Tiffany.Schuster16@yahoo.com | Bw2OhPUtIRcWxZF | active | seller | 2018-07-30 08:01:29.942-03 | 2019-09-03 10:50:40.314-03
(4 rows)
然后
update users set email = 'test@test.com' where id = 8;
UPDATE 1
然后,这种情况发生了:
select * from users limit 4;
id | email | password | status | role | created_at | updated_at
----+--------------------------------+-----------------+----------+--------+----------------------------+----------------------------
9 | Marcelino_Prohaska97@gmail.com | sUMuOM_gXCPxz19 | disabled | seller | 2019-06-14 15:39:45.447-03 | 2019-06-25 12:54:01.023-03
10 | Gino_Blick@gmail.com | iOkZQhc7JSsQcpY | disabled | seller | 2020-02-13 13:39:16.26-03 | 2019-12-18 17:02:37.938-03
11 | Tiffany.Schuster16@yahoo.com | Bw2OhPUtIRcWxZF | active | seller | 2018-07-30 08:01:29.942-03 | 2019-09-03 10:50:40.314-03
12 | Brody_Pollich@yahoo.com | ZlFy3kEUSrmxHAa | disabled | seller | 2018-07-06 13:18:29.936-03 | 2019-08-03 21:46:22.296-03
(4 rows)
问题是,行仍然存在,但它不是由select显示的*
select * from users where id = 8 limit 10;
id | email | password | status | role | created_at | updated_at
----+---------------+-----------------+----------+--------+----------------------------+----------------------------
8 | test@test.com | qYHsmrKWaiaiZxI | disabled | seller | 2019-09-06 21:43:08.043-03 | 2019-08-13 16:04:25.233-03
(1 row)
以下是我的表的结构(dbeaver编写的代码)和我的postgresql版本:
CREATE TABLE public.users (
id serial NOT NULL,
email varchar(255) NOT NULL,
"password" varchar(255) NOT NULL,
status varchar(255) NOT NULL,
"role" varchar(255) NOT NULL,
created_at timestamptz NOT NULL,
updated_at timestamptz NOT NULL,
CONSTRAINT users_email_unique UNIQUE (email),
CONSTRAINT users_pkey PRIMARY KEY (id)
);
SELECT version();
version
--------------------------------------------------------------------------------------------
-------------------------------------
PostgreSQL 12.3 (Ubuntu 12.3-1.pgdg18.04+1) on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0, 64-bit
(1 row)
谢谢。
1条答案
按热度按时间ws51t4hk1#
删除
limit
并申请order by
:是
ID = 8
回来?文件上说:
由于数据库表中行的顺序未指定,因此在使用limit子句时,应始终使用order by子句来控制行顺序。如果不这样做,将得到一个结果集,其行的顺序未指定。
也就是说你看到了
ID = 8
偶然地;不能保证你会再次得到它仅仅用limit
条款。