我很好奇 Postgres 是否有任何可以限制为 ID 保留的行数的地方。
例如,假设我有一个 users 表和 login_coordinates 表。每次用户登录时,最新的坐标都会输入到用户表中的一列,并插入到 login_coordinates 表中。
我只想保留最后 10 条记录,并删除所有用户的 login_coordinates 表中的第 11 条(最旧)记录。
用户
user_id | current_coordinates |
----------------------+----------------------------+
1 | aaaa.bbbbb, aaaaa.bbbbbb |
2 | zzzz.xxxxxx, xxxxx.xxxcxx |
3 | dddd.xxxxxx, xxxxx.xxxcxx |
Run Code Online (Sandbox Code Playgroud)
登录坐标
coordinates_id | old_login_coordinates | user_id |
----------------------+----------------------------+--------------------------+
1 | aaaa.bbbbb, aaaaa.bbbbbb | 1 |
2 | xxxxx.xxxxxx, xxxxx.xxxcxx | 1 |
3 | xxxxx.xxxxxx, xxxxx.xxxcxx | 1 |
Run Code Online (Sandbox Code Playgroud)
是否有任何将记录限制为每个用户 10 个坐标标识的东西,总是删除最旧的记录?
我正在使用 PostgreSQL 9.5。
第1部分:
我正在使用 Postgres 9.5,我试图弄清楚如何使用 JSON 数组插入 postgres 表。我用以下命令创建了一个表:
CREATE TABLE inputtable (
data_point_id SERIAL PRIMARY KEY NOT NULL,
chart_id INTEGER NOT NULL,
user_id INTEGER NOT NULL,
properties jsonb NOT NULL
);
Run Code Online (Sandbox Code Playgroud)
json 数据格式如下所示:
[
{ col1: a, col2: 5, col3: 1, col4: one},
{ col1: b, col2: 6, col3: 2, col4: two},
{ col1: c, col2: 7, col3: 3, col4: three}
]
Run Code Online (Sandbox Code Playgroud)
由于一些变化,我现在必须能够基于 user_id 每行插入一个 json 对象。最后,我希望输出如下所示:
data_point_id | chart_id | user_id | properties
---------------+----------+----------+--------------
1 | 1 | 1 …Run Code Online (Sandbox Code Playgroud) 我使用的是 postgres 9.5,我想知道 postgres 是否允许对 IP 地址进行任何类型的子网划分或计算,特别是 IPv6。我知道他们有存储数据的cidr格式,如 IpV4 和 Ipv6 格式。但是,我可以做任何事情,例如找出存储在数据库中的子网中有多少个客户端 IP?
例如。如果我存储了 的 IPv6 地址2001:dd8::/64,我能否找出有多少主机 IP(根据某些计算器,为 18446744073709551616)。
我的猜测是答案是否定的,postgres 不支持这一点。
我正在使用 Postgres 9.5,我想弄清楚如何使用 JSON 数组更新 postgres 表。我希望数组中的每个对象都对应一个新行,每个键对应一个列,每个值都是要插入到该列中的数据。我试图用一个函数来做到这一点。这里的数据格式:
[
{ col1: a, col2: 5, col3: 1, col4: one},
{ col1: b, col2: 6, col3: 2, col4: two},
{ col1: c, col2: 7, col3: 3, col4: three},
{ col1: d, col2: 8, col3: 4, col4: four},
]
Run Code Online (Sandbox Code Playgroud)
这是我的预期输出:
col1 (varchar)| col2 (integer) | col3 (integer) | col4 (varchar)
-----------------+----------------+--------------------+------------------
a | 5 | 1 | one
b | 6 | 2 | two
c | 7 | 3 | three
d | …Run Code Online (Sandbox Code Playgroud)