亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

PostgreSQL數(shù)據(jù)庫(kù)timestamp數(shù)據(jù)類型精度進(jìn)位問(wèn)題解析

 更新時(shí)間:2024年11月04日 16:45:23   作者:靖節(jié)先生  
PostgreSQL是一款功能強(qiáng)大的開(kāi)源關(guān)系型數(shù)據(jù)庫(kù)管理系統(tǒng),起源于1986年的POSTGRES項(xiàng)目,它支持多種數(shù)據(jù)類型,包括數(shù)值類型、字符串類型、日期時(shí)間類型等,本文介紹PostgreSQL數(shù)據(jù)庫(kù)timestamp數(shù)據(jù)類型精度進(jìn)位問(wèn)題,感興趣的朋友一起看看吧

PostgreSQL數(shù)據(jù)庫(kù)timestamp數(shù)據(jù)類型精度進(jìn)位問(wèn)題

1. PostgreSQL簡(jiǎn)介

PostgreSQL 起源于 1986 年加州大學(xué)伯克利分校開(kāi)發(fā)的 POSTGRES 項(xiàng)目。它經(jīng)過(guò)多年的發(fā)展和演進(jìn),成為了一個(gè)功能強(qiáng)大、開(kāi)源的關(guān)系型數(shù)據(jù)庫(kù)管理系統(tǒng)。

PostgreSQL 本身是開(kāi)源的,通常不存在嚴(yán)格意義上的商業(yè)版和社區(qū)版之分。
官網(wǎng)地址:https://www.postgresql.org/
在官網(wǎng)上,您可以獲取更詳細(xì)和準(zhǔn)確的信息,包括最新的特性、下載安裝指南、文檔教程等。

2. PostgreSQL特點(diǎn)

1.強(qiáng)大的功能:支持豐富的特性,如復(fù)雜查詢、事務(wù)處理、存儲(chǔ)過(guò)程、視圖等。
2.開(kāi)源和社區(qū)驅(qū)動(dòng):擁有活躍的開(kāi)源社區(qū),不斷進(jìn)行改進(jìn)和更新。
3.可擴(kuò)展性:易于擴(kuò)展功能和數(shù)據(jù)類型。
4.標(biāo)準(zhǔn)符合性:高度符合 SQL 標(biāo)準(zhǔn)。
5.安全性:提供多種安全機(jī)制,包括用戶認(rèn)證、權(quán)限管理等。

3. PostgreSQL優(yōu)缺點(diǎn)

優(yōu)點(diǎn):
1.穩(wěn)定性高:經(jīng)過(guò)長(zhǎng)期的實(shí)踐和優(yōu)化,具有出色的穩(wěn)定性和可靠性。
2.數(shù)據(jù)完整性:確保數(shù)據(jù)的一致性和完整性。
3.跨平臺(tái)支持:可在多種操作系統(tǒng)上運(yùn)行。
4.豐富的文檔和支持:有詳細(xì)的官方文檔和活躍的社區(qū)支持。

缺點(diǎn):
1.配置相對(duì)復(fù)雜:對(duì)于新手來(lái)說(shuō),初始配置可能具有一定的難度。
2.資源消耗:在處理大型數(shù)據(jù)集時(shí),可能需要較多的系統(tǒng)資源。

4. PostgreSQL數(shù)據(jù)類型

包括數(shù)值類型(如整數(shù)、浮點(diǎn)數(shù))、字符串類型(固定長(zhǎng)度和可變長(zhǎng)度)、日期和時(shí)間類型、布爾類型、數(shù)組類型、枚舉類型、幾何類型等。

數(shù)據(jù)類型說(shuō)明精度特點(diǎn)案例
smallint小范圍整數(shù)2 字節(jié)占用存儲(chǔ)空間小,適用于數(shù)據(jù)范圍較小的整數(shù)smallint: 100
integer(或 int常規(guī)整數(shù)4 字節(jié)常見(jiàn)的整數(shù)類型,能滿足大多數(shù)常規(guī)整數(shù)需求integer: 2000
bigint大范圍整數(shù)8 字節(jié)用于存儲(chǔ)非常大的整數(shù)數(shù)值bigint: 9000000000
decimal(或 numeric用戶指定精度和小數(shù)位數(shù)的精確數(shù)值取決于指定的精度和小數(shù)位數(shù)精度和小數(shù)位數(shù)可自定義,適用于對(duì)精度要求高的數(shù)值計(jì)算decimal(5, 2): 123.45
real單精度浮點(diǎn)數(shù)4 字節(jié)大約 6 位十進(jìn)制精度存儲(chǔ)精度要求不高的浮點(diǎn)數(shù)
double precision雙精度浮點(diǎn)數(shù)8 字節(jié)大約 15 位十進(jìn)制精度適用于高精度的浮點(diǎn)數(shù)計(jì)算
char(n)固定長(zhǎng)度的字符字符串,不足長(zhǎng)度用空格填充n 個(gè)字符長(zhǎng)度固定,存儲(chǔ)和讀取效率較高char(10): 'hello '
varchar(n)可變長(zhǎng)度的字符字符串,最大長(zhǎng)度為 n最大 n 個(gè)字符節(jié)省存儲(chǔ)空間,適用于長(zhǎng)度不固定的字符串varchar(20): 'hello world'
text可變長(zhǎng)度的字符字符串,無(wú)長(zhǎng)度限制無(wú)限制可存儲(chǔ)大量文本數(shù)據(jù)text: 'This is a long text.'
date日期格式為 YYYY-MM-DD只存儲(chǔ)日期信息date: '2024-08-08'
time時(shí)間,不包含日期格式為 HH:MM:SS只存儲(chǔ)時(shí)間信息time: '12:30:00'
timestamp日期和時(shí)間格式為 YYYY-MM-DD HH:MM:SS ,支持到微秒級(jí)別(6 位小數(shù))同時(shí)包含日期和時(shí)間信息timestamp: '2024-08-08 12:30:00.123456'
boolean布爾值真或假用于表示邏輯值boolean: true
array數(shù)組取決于元素類型和數(shù)組長(zhǎng)度可以存儲(chǔ)相同數(shù)據(jù)類型的元素集合integer[]: '{1, 2, 3}'
jsonJSON 數(shù)據(jù)無(wú)固定精度方便存儲(chǔ)和處理 JSON 格式的數(shù)據(jù)json: '{"key": "value"}'
uuid通用唯一標(biāo)識(shí)符128 位用于生成唯一標(biāo)識(shí)符uuid: 'a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11'

5. PostgreSQL注意事項(xiàng)

PostgreSQL數(shù)據(jù)庫(kù)timestamp數(shù)據(jù)類型精度存在進(jìn)位問(wèn)題,先說(shuō)結(jié)論,在測(cè)試驗(yàn)證。

PostgreSQL只能存儲(chǔ)最多6位的小數(shù)部分。因此,當(dāng)嘗試存儲(chǔ)超過(guò)6位的分?jǐn)?shù)秒時(shí),會(huì)發(fā)生以下情況之一:
如果額外的數(shù)字導(dǎo)致整個(gè)分?jǐn)?shù)秒部分超過(guò) 999999 微秒,則整個(gè)時(shí)間值會(huì)進(jìn)位到下一秒,剩余的部分會(huì)被截?cái)嗷蛘咚纳嵛迦氲?6 位小數(shù)。
在這種情況下,2024-08-07 16:19:23.999999999 會(huì)被處理為 2024-08-07 16:19:24.000000。
如果你使用的是某種客戶端或者編程語(yǔ)言接口來(lái)處理這個(gè)時(shí)間值,在插入之前它可能會(huì)自動(dòng)進(jìn)行四舍五入或截?cái)唷?/p>

比如,
截取前三位數(shù)據(jù):小于 2024-08-07 16:19:23.999999500
進(jìn)位數(shù)據(jù):[2024-08-07 16:19:23.999999500 - 2024-08-07 16:19:23.999999999]

通過(guò)指定精度數(shù)據(jù)庫(kù)中仍然會(huì)保存6位,但是指定精度3時(shí)后三位始終為0,精度6則保存6位。

SELECT * FROM crm.t_wx_user_1 WHERE 1=1;
SELECT user_id, to_char(last_login_time, 'YYYY-MM-DD HH24:MI:SS.US'), to_char(create_time, 'YYYY-MM-DD HH24:MI:SS.US') FROM crm.t_wx_user_1 WHERE 1=1;
-- last_login_time timestamp,-- 》timestamp(3)
ALTER TABLE crm.t_wx_user_1 ALTER COLUMN last_login_time TYPE TIMESTAMP(3) WITHOUT TIME ZONE;
ALTER TABLE crm.t_wx_user_1 ALTER COLUMN create_time TYPE TIMESTAMP(6) WITHOUT TIME ZONE;

注意問(wèn)題:
PostgreSQL 的 timestamp 類型默認(rèn)只支持到微秒級(jí)別(6位小數(shù)),為什么select出來(lái)的數(shù)據(jù)中展示三位2024-08-07 16:19:23.999?
PostgreSQL 的 timestamp 類型確實(shí)支持到微秒級(jí)別(6位小數(shù))。然而,默認(rèn)情況下,當(dāng)您從數(shù)據(jù)庫(kù)中查詢 timestamp 值時(shí),PostgreSQL 會(huì)將結(jié)果顯示為毫秒級(jí)別(3位小數(shù))以提高可讀性。這是默認(rèn)的行為,但是您可以更改輸出的精度。案例如下。

--PostgreSQL數(shù)據(jù)庫(kù)timestamp數(shù)據(jù)類型精度進(jìn)位問(wèn)題
--# 1.PostgreSQL數(shù)據(jù)庫(kù)數(shù)據(jù)類型
-- 數(shù)據(jù)庫(kù)版本,PostgreSQL 15.3 on x86_64-pc-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit
SELECT version();
-- 創(chuàng)建數(shù)據(jù)庫(kù)
-- crm.t_wx_user_1 definition
-- Drop table
-- DROP TABLE crm.t_wx_user_1;
CREATE TABLE crm.t_wx_user_1 (
	user_id int8 NOT NULL,
	union_id varchar(50) NULL,
	open_id varchar(50) NULL,
	last_login_time timestamp NULL,
	privacy_agreement_version int8 NOT NULL DEFAULT 0,
	create_time timestamp NOT NULL,
	update_time timestamp NOT NULL DEFAULT '1970-01-01 00:00:00'::timestamp without time zone,
	delete_flag int2 NOT NULL DEFAULT 0,
	delete_time timestamp NOT NULL DEFAULT '1970-01-01 00:00:00'::timestamp without time zone,
	first_login_time timestamp NULL,
	created_by int8 NOT NULL DEFAULT '-1'::integer,
	updated_by int8 NOT NULL DEFAULT '-1'::integer,
	inst_id int8 NULL,
	CONSTRAINT t_wx_user_pkey_1 PRIMARY KEY (user_id)
);
CREATE INDEX idx_t_wx_user_open_id_1 ON crm.t_wx_user_1 USING btree (open_id);
CREATE INDEX idx_t_wx_user_union_id_1 ON crm.t_wx_user_1 USING btree (union_id);
CREATE UNIQUE INDEX uniq_wx_userid_unionid_openid_1 ON crm.t_wx_user_1 USING btree (user_id, union_id, open_id);
-- 測(cè)試驗(yàn)證數(shù)據(jù)庫(kù)
select * from crm.t_wx_user_1 order by user_id  ;
-- postgres數(shù)據(jù)庫(kù) timestamp類型,新增操作,支持類型 2024-08-07 16:19:23.142,正常插入數(shù)據(jù)庫(kù)
INSERT INTO crm.t_wx_user_1
(user_id, union_id, open_id, last_login_time, privacy_agreement_version, create_time, update_time, delete_flag, delete_time, first_login_time, created_by, updated_by, inst_id)
VALUES(1230000, '1', '1', '2024-08-07 16:19:23.142', 0, '2024-07-12 16:52:39.674', '2024-08-07 16:19:23.142', 0, '1970-01-01 00:00:00.000', '2024-07-12 16:52:39.674', -1, -1, 1);
-- postgres數(shù)據(jù)庫(kù) timestamp類型,新增操作,6位數(shù)據(jù)類型 2024-08-07 16:19:23.123456,截?cái)嗲叭徊迦霐?shù)據(jù)庫(kù)
INSERT INTO crm.t_wx_user_1
(user_id, union_id, open_id, last_login_time, privacy_agreement_version, create_time, update_time, delete_flag, delete_time, first_login_time, created_by, updated_by, inst_id)
VALUES(1230001, '1', '1', '2024-08-07 16:19:23.123456', 0, '2024-07-12 16:52:39.123456', '2024-08-07 16:19:23.123456', 0, '1970-01-01 00:00:00.123456', '2024-07-12 16:52:39.123456', -1, -1, 1);
-- postgres數(shù)據(jù)庫(kù) timestamp類型,新增操作,3個(gè)9驗(yàn)證 2024-08-07 16:19:23.999,正常插入數(shù)據(jù)庫(kù)
INSERT INTO crm.t_wx_user_1
(user_id, union_id, open_id, last_login_time, privacy_agreement_version, create_time, update_time, delete_flag, delete_time, first_login_time, created_by, updated_by, inst_id)
VALUES(1230002, '1', '1', '2024-08-07 16:19:23.999', 0, '2024-07-12 16:52:39.999', '2024-08-07 16:19:23.999', 0, '1970-01-01 00:00:00.999', '2024-07-12 16:52:39.999', -1, -1, 1);
-- postgres數(shù)據(jù)庫(kù) timestamp類型,新增操作,4個(gè)9驗(yàn)證 2024-08-07 16:19:23.9999,截?cái)嗲叭徊迦霐?shù)據(jù)庫(kù)
INSERT INTO crm.t_wx_user_1
(user_id, union_id, open_id, last_login_time, privacy_agreement_version, create_time, update_time, delete_flag, delete_time, first_login_time, created_by, updated_by, inst_id)
VALUES(1230003, '1', '1', '2024-08-07 16:19:23.9999', 0, '2024-07-12 16:52:39.9999', '2024-08-07 16:19:23.9999', 0, '1970-01-01 00:00:00.9999', '2024-07-12 16:52:39.9999', -1, -1, 1);
-- postgres數(shù)據(jù)庫(kù) timestamp類型,新增操作,6個(gè)9驗(yàn)證 2024-08-07 16:19:23.999999,截?cái)嗲叭徊迦霐?shù)據(jù)庫(kù)
INSERT INTO crm.t_wx_user_1
(user_id, union_id, open_id, last_login_time, privacy_agreement_version, create_time, update_time, delete_flag, delete_time, first_login_time, created_by, updated_by, inst_id)
VALUES(1230004, '1', '1', '2024-08-07 16:19:23.999999', 0, '2024-07-12 16:52:39.999999', '2024-08-07 16:19:23.999999', 0, '1970-01-01 00:00:00.999999', '2024-07-12 16:52:39.999999', -1, -1, 1);
-- postgres數(shù)據(jù)庫(kù) timestamp類型,新增操作,9個(gè)9驗(yàn)證 2024-08-07 16:19:23.999999999,會(huì)進(jìn)位,存到數(shù)據(jù)庫(kù)為:2024-08-07 16:19:24.000
INSERT INTO crm.t_wx_user_1
(user_id, union_id, open_id, last_login_time, privacy_agreement_version, create_time, update_time, delete_flag, delete_time, first_login_time, created_by, updated_by, inst_id)
VALUES(1230005, '1', '1', '2024-08-07 16:19:23.999999999', 0, '2024-07-12 16:52:39.999999999', '2024-08-07 16:19:23.999999999', 0, '1970-01-01 00:00:00.999999999', '2024-07-12 16:52:39.999999999', -1, -1, 1);
-- postgres數(shù)據(jù)庫(kù) timestamp類型,查詢操作,9個(gè)9驗(yàn)證 2024-08-07 16:19:23.999999999,會(huì)進(jìn)位,把2024-08-07 16:19:24.000的也查詢出來(lái)。
select * from crm.t_wx_user_1 where last_login_time <= '2024-08-07 16:19:23.999999999';
-- postgres數(shù)據(jù)庫(kù) timestamp類型,新增操作,9個(gè)值驗(yàn)證 2024-08-07 16:19:23.123456789,截?cái)嗲叭徊迦霐?shù)據(jù)庫(kù)
INSERT INTO crm.t_wx_user_1
(user_id, union_id, open_id, last_login_time, privacy_agreement_version, create_time, update_time, delete_flag, delete_time, first_login_time, created_by, updated_by, inst_id)
VALUES(1230006, '1', '1', '2024-08-07 16:19:23.123456789', 0, '2024-07-12 16:52:39.123456789', '2024-08-07 16:19:23.123456789', 0, '1970-01-01 00:00:00.123456789', '2024-07-12 16:52:39.123456789', -1, -1, 1);
-- postgres數(shù)據(jù)庫(kù) timestamp類型,查詢操作,9個(gè)9驗(yàn)證 2024-08-07 16:19:23.999999999,會(huì)進(jìn)位,把2024-08-07 16:19:24.000的也查詢出來(lái)。
select * from crm.t_wx_user_1 where last_login_time <= '2024-08-07 16:19:23.999999999';
-- postgres數(shù)據(jù)庫(kù) timestamp類型,新增操作,9個(gè)9驗(yàn)證 2024-08-07 16:19:23.999999998,會(huì)進(jìn)位,存到數(shù)據(jù)庫(kù)為:2024-08-07 16:19:24.000
INSERT INTO crm.t_wx_user_1
(user_id, union_id, open_id, last_login_time, privacy_agreement_version, create_time, update_time, delete_flag, delete_time, first_login_time, created_by, updated_by, inst_id)
VALUES(1230007, '1', '1', '2024-08-07 16:19:23.999999998', 0, '2024-07-12 16:52:39.999999998', '2024-08-07 16:19:23.999999998', 0, '1970-01-01 00:00:00.999999998', '2024-07-12 16:52:39.999999998', -1, -1, 1);
-- postgres數(shù)據(jù)庫(kù) timestamp類型,新增操作,9個(gè)9驗(yàn)證 2024-08-07 16:19:23.999999001,截?cái)嗲叭徊迦霐?shù)據(jù)庫(kù)
INSERT INTO crm.t_wx_user_1
(user_id, union_id, open_id, last_login_time, privacy_agreement_version, create_time, update_time, delete_flag, delete_time, first_login_time, created_by, updated_by, inst_id)
VALUES(1230008, '1', '1', '2024-08-07 16:19:23.999999001', 0, '2024-07-12 16:52:39.999999001', '2024-08-07 16:19:23.999999001', 0, '1970-01-01 00:00:00.999999001', '2024-07-12 16:52:39.999999001', -1, -1, 1);
-- postgres數(shù)據(jù)庫(kù) timestamp類型,新增操作,9個(gè)9驗(yàn)證 2024-08-07 16:19:23.999999991,進(jìn)位
INSERT INTO crm.t_wx_user_1
(user_id, union_id, open_id, last_login_time, privacy_agreement_version, create_time, update_time, delete_flag, delete_time, first_login_time, created_by, updated_by, inst_id)
VALUES(1230009, '1', '1', '2024-08-07 16:19:23.999999991', 0, '2024-07-12 16:52:39.999999991', '2024-08-07 16:19:23.999999991', 0, '1970-01-01 00:00:00.999999991', '2024-07-12 16:52:39.999999991', -1, -1, 1);
-- postgres數(shù)據(jù)庫(kù) timestamp類型,新增操作,9個(gè)9驗(yàn)證 2024-08-07 16:19:23.999999990,進(jìn)位
INSERT INTO crm.t_wx_user_1
(user_id, union_id, open_id, last_login_time, privacy_agreement_version, create_time, update_time, delete_flag, delete_time, first_login_time, created_by, updated_by, inst_id)
VALUES(1230010, '1', '1', '2024-08-07 16:19:23.999999990', 0, '2024-07-12 16:52:39.999999990', '2024-08-07 16:19:23.999999990', 0, '1970-01-01 00:00:00.999999990', '2024-07-12 16:52:39.999999990', -1, -1, 1);
-- postgres數(shù)據(jù)庫(kù) timestamp類型,新增操作,9個(gè)9驗(yàn)證 2024-08-07 16:19:23.999999900,進(jìn)位
INSERT INTO crm.t_wx_user_1
(user_id, union_id, open_id, last_login_time, privacy_agreement_version, create_time, update_time, delete_flag, delete_time, first_login_time, created_by, updated_by, inst_id)
VALUES(1230011, '1', '1', '2024-08-07 16:19:23.999999900', 0, '2024-07-12 16:52:39.999999900', '2024-08-07 16:19:23.999999900', 0, '1970-01-01 00:00:00.999999900', '2024-07-12 16:52:39.999999900', -1, -1, 1);
-- postgres數(shù)據(jù)庫(kù) timestamp類型,新增操作,9個(gè)9驗(yàn)證 2024-08-07 16:19:23.999999800,進(jìn)位
INSERT INTO crm.t_wx_user_1
(user_id, union_id, open_id, last_login_time, privacy_agreement_version, create_time, update_time, delete_flag, delete_time, first_login_time, created_by, updated_by, inst_id)
VALUES(1230012, '1', '1', '2024-08-07 16:19:23.999999800', 0, '2024-07-12 16:52:39.999999800', '2024-08-07 16:19:23.999999800', 0, '1970-01-01 00:00:00.999999800', '2024-07-12 16:52:39.999999800', -1, -1, 1);
-- postgres數(shù)據(jù)庫(kù) timestamp類型,新增操作,9個(gè)9驗(yàn)證 2024-08-07 16:19:23.999999000,截取前三位
INSERT INTO crm.t_wx_user_1
(user_id, union_id, open_id, last_login_time, privacy_agreement_version, create_time, update_time, delete_flag, delete_time, first_login_time, created_by, updated_by, inst_id)
VALUES(1230013, '1', '1', '2024-08-07 16:19:23.999999000', 0, '2024-07-12 16:52:39.999999000', '2024-08-07 16:19:23.999999000', 0, '1970-01-01 00:00:00.999999000', '2024-07-12 16:52:39.999999000', -1, -1, 1);
-- postgres數(shù)據(jù)庫(kù) timestamp類型,新增操作,9個(gè)9驗(yàn)證 2024-08-07 16:19:23.999999500,進(jìn)位
INSERT INTO crm.t_wx_user_1
(user_id, union_id, open_id, last_login_time, privacy_agreement_version, create_time, update_time, delete_flag, delete_time, first_login_time, created_by, updated_by, inst_id)
VALUES(1230014, '1', '1', '2024-08-07 16:19:23.999999500', 0, '2024-07-12 16:52:39.999999500', '2024-08-07 16:19:23.999999500', 0, '1970-01-01 00:00:00.999999500', '2024-07-12 16:52:39.999999000', -1, -1, 1);
-- postgres數(shù)據(jù)庫(kù) timestamp類型,新增操作,9個(gè)9驗(yàn)證 2024-08-07 16:19:23.999999100,進(jìn)位
INSERT INTO crm.t_wx_user_1
(user_id, union_id, open_id, last_login_time, privacy_agreement_version, create_time, update_time, delete_flag, delete_time, first_login_time, created_by, updated_by, inst_id)
VALUES(1230015, '1', '1', '2024-08-07 16:19:23.999999100', 0, '2024-07-12 16:52:39.999999100', '2024-08-07 16:19:23.999999100', 0, '1970-01-01 00:00:00.999999100', '2024-07-12 16:52:39.999999100', -1, -1, 1);
INSERT INTO crm.t_wx_user_1
(user_id, union_id, open_id, last_login_time, privacy_agreement_version, create_time, update_time, delete_flag, delete_time, first_login_time, created_by, updated_by, inst_id)
VALUES(1230016, '1', '1', '2024-08-07 16:19:23.999999200', 0, '2024-07-12 16:52:39.999999200', '2024-08-07 16:19:23.999999100', 0, '1970-01-01 00:00:00.999999100', '2024-07-12 16:52:39.999999100', -1, -1, 1);
INSERT INTO crm.t_wx_user_1
(user_id, union_id, open_id, last_login_time, privacy_agreement_version, create_time, update_time, delete_flag, delete_time, first_login_time, created_by, updated_by, inst_id)
VALUES(1230017, '1', '1', '2024-08-07 16:19:23.999999300', 0, '2024-07-12 16:52:39.999999300', '2024-08-07 16:19:23.999999100', 0, '1970-01-01 00:00:00.999999100', '2024-07-12 16:52:39.999999100', -1, -1, 1);
INSERT INTO crm.t_wx_user_1
(user_id, union_id, open_id, last_login_time, privacy_agreement_version, create_time, update_time, delete_flag, delete_time, first_login_time, created_by, updated_by, inst_id)
VALUES(1230018, '1', '1', '2024-08-07 16:19:23.999999400', 0, '2024-07-12 16:52:39.999999400', '2024-08-07 16:19:23.999999100', 0, '1970-01-01 00:00:00.999999100', '2024-07-12 16:52:39.999999100', -1, -1, 1);
-- 再次驗(yàn)證 500 進(jìn)位
INSERT INTO crm.t_wx_user_1
(user_id, union_id, open_id, last_login_time, privacy_agreement_version, create_time, update_time, delete_flag, delete_time, first_login_time, created_by, updated_by, inst_id)
VALUES(1230019, '1', '1', '2024-08-07 16:19:23.999999500', 0, '2024-07-12 16:52:39.999999500', '2024-08-07 16:19:23.999999100', 0, '1970-01-01 00:00:00.999999100', '2024-07-12 16:52:39.999999100', -1, -1, 1);
-- postgres數(shù)據(jù)庫(kù) timestamp類型,查詢操作,9個(gè)9驗(yàn)證 2024-08-07 16:19:23.999999999,會(huì)進(jìn)位,把2024-08-07 16:19:24.000的也查詢出來(lái)。
select * from crm.t_wx_user_1 ;
where last_login_time <= '2024-08-07 16:19:23.999999999';
--截取前三位數(shù)據(jù):小于 2024-08-07 16:19:23.999999500
--進(jìn)位數(shù)據(jù):[2024-08-07 16:19:23.999999500 - 2024-08-07 16:19:23.999999999]
結(jié)論
PostgreSQL只能存儲(chǔ)最多6位的小數(shù)部分。因此,當(dāng)嘗試存儲(chǔ)超過(guò)6位的分?jǐn)?shù)秒時(shí),會(huì)發(fā)生以下情況之一:
如果額外的數(shù)字導(dǎo)致整個(gè)分?jǐn)?shù)秒部分超過(guò) 999999 微秒,則整個(gè)時(shí)間值會(huì)進(jìn)位到下一秒,剩余的部分會(huì)被截?cái)嗷蛘咚纳嵛迦氲?6 位小數(shù)。
在這種情況下,2024-08-07 16:19:23.999999999 會(huì)被處理為 2024-08-07 16:19:24.000000。
如果你使用的是某種客戶端或者編程語(yǔ)言接口來(lái)處理這個(gè)時(shí)間值,在插入之前它可能會(huì)自動(dòng)進(jìn)行四舍五入或截?cái)唷?
比如:
截取前三位數(shù)據(jù):小于 2024-08-07 16:19:23.999999500
進(jìn)位數(shù)據(jù):[2024-08-07 16:19:23.999999500 - 2024-08-07 16:19:23.999999999]
-- 創(chuàng)建表
CREATE TABLE crm.wx_users (
    id SERIAL PRIMARY KEY,
    name VARCHAR(255) NOT NULL,
    created_at TIMESTAMP WITHOUT TIME ZONE NOT NULL
);
-- 插入數(shù)據(jù)
INSERT INTO crm.wx_users (name, created_at)
VALUES ('John Doe', '2024-08-07 16:19:23.999999');
INSERT INTO crm.wx_users (name, created_at)
VALUES ('Jerry', '2024-08-07 16:19:23.999996');
-- 查詢數(shù)據(jù)
SELECT * FROM crm.wx_users;
 PostgreSQL 的 timestamp 類型默認(rèn)只支持到微秒級(jí)別(6位小數(shù)),為什么select出來(lái)的數(shù)據(jù)中展示三位2024-08-07 16:19:23.999
 PostgreSQL 的 timestamp 類型確實(shí)支持到微秒級(jí)別(6位小數(shù))。然而,默認(rèn)情況下,當(dāng)您從數(shù)據(jù)庫(kù)中查詢 timestamp 值時(shí),PostgreSQL 會(huì)將結(jié)果顯示為毫秒級(jí)別(3位小數(shù))以提高可讀性。這是默認(rèn)的行為,但是您可以更改輸出的精度。
-- 使用 to_char 函數(shù)來(lái)顯示完整的微秒
SELECT id, name, to_char(created_at, 'YYYY-MM-DD HH24:MI:SS.US') AS created_at FROM crm.wx_users;

到此這篇關(guān)于PostgreSQL數(shù)據(jù)庫(kù)timestamp數(shù)據(jù)類型精度進(jìn)位問(wèn)題的文章就介紹到這了,更多相關(guān)PostgreSQL timestamp數(shù)據(jù)類型內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • PostgreSQL 更新JSON,JSONB字段的操作

    PostgreSQL 更新JSON,JSONB字段的操作

    這篇文章主要介紹了PostgreSQL 更新JSON,JSONB字段的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2021-01-01
  • PostgreSQL15.x安裝的詳細(xì)教程

    PostgreSQL15.x安裝的詳細(xì)教程

    PostgreSQL 是一個(gè)功能強(qiáng)大的開(kāi)源關(guān)系型數(shù)據(jù)庫(kù)系統(tǒng),基于 C 語(yǔ)言實(shí)現(xiàn),采用 PostgreSQL 許可證,這是一種自由軟件許可證,允許用戶自由使用、修改和分發(fā)源代碼,所以本文將給大家介紹PostgreSQL15.x安裝的詳細(xì)教程,需要的朋友可以參考下
    2024-09-09
  • postgresql無(wú)序uuid性能測(cè)試及對(duì)數(shù)據(jù)庫(kù)的影響

    postgresql無(wú)序uuid性能測(cè)試及對(duì)數(shù)據(jù)庫(kù)的影響

    小編最近在做一個(gè)超大表的性能測(cè)試,在過(guò)程中發(fā)現(xiàn)無(wú)序uuid做主鍵對(duì)表插入性能有些影響,糾結(jié)該怎么處理這一問(wèn)題呢?接下來(lái)小編給大家分享postgresql無(wú)序uuid性能測(cè)試的相關(guān)知識(shí)幫助大家學(xué)習(xí),需要的彭參考下吧
    2021-06-06
  • PostgreSQL數(shù)據(jù)庫(kù)中匿名塊的寫(xiě)法實(shí)例

    PostgreSQL數(shù)據(jù)庫(kù)中匿名塊的寫(xiě)法實(shí)例

    這篇文章主要介紹了PostgreSQL數(shù)據(jù)庫(kù)中匿名塊的寫(xiě)法實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2021-01-01
  • PostgreSQL表膨脹問(wèn)題解析及解決方案

    PostgreSQL表膨脹問(wèn)題解析及解決方案

    表膨脹是指表的數(shù)據(jù)和索引所占文件系統(tǒng)的空間在有效數(shù)據(jù)量并未發(fā)生大的變化的情況下不斷增大,這種現(xiàn)象會(huì)導(dǎo)致關(guān)系文件被大量空洞填滿,從而浪費(fèi)大量的磁盤(pán)空間,本文給大家介紹了PostgreSQL表膨脹問(wèn)題解析及解決方案,需要的朋友可以參考下
    2024-11-11
  • Postgresql 默認(rèn)用戶名與密碼的操作

    Postgresql 默認(rèn)用戶名與密碼的操作

    這篇文章主要介紹了Postgresql 默認(rèn)用戶名與密碼的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2021-01-01
  • PostgreSQL數(shù)據(jù)庫(kù)視圖及子查詢使用操作

    PostgreSQL數(shù)據(jù)庫(kù)視圖及子查詢使用操作

    這篇文章主要為大家介紹了PostgreSQL數(shù)據(jù)庫(kù)視圖及子查詢的使用操作,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步早日升職加薪
    2022-04-04
  • PostgreSQL教程(十八):客戶端命令(2)

    PostgreSQL教程(十八):客戶端命令(2)

    這篇文章主要介紹了PostgreSQL教程(十八):客戶端命令(2),本文講解了pg_dump、pg_restore、psql、內(nèi)置命令應(yīng)用示例等內(nèi)容,需要的朋友可以參考下
    2015-05-05
  • PostgreSQL行轉(zhuǎn)列的多種方法

    PostgreSQL行轉(zhuǎn)列的多種方法

    這篇文章主要介紹了PostgreSQL行轉(zhuǎn)列的多種方法,本文給大家分享三種方法,每種方法結(jié)合示例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2023-10-10
  • postgresql查詢自動(dòng)將大寫(xiě)的名稱轉(zhuǎn)換為小寫(xiě)的案例

    postgresql查詢自動(dòng)將大寫(xiě)的名稱轉(zhuǎn)換為小寫(xiě)的案例

    這篇文章主要介紹了postgresql查詢自動(dòng)將大寫(xiě)的名稱轉(zhuǎn)換為小寫(xiě)的案例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2021-01-01

最新評(píng)論