在电子商务设置中,我们有产品和变体。变体也是产品,但它们在某些属性上有所不同。
例如为:A短衫可以变化Size和Color属性。假设我们有以下选项Size和Color属性
Color:红色、绿色
Size:小、大
然后我们可以为那件 T 恤设计 4 个变体
整个问题将使用以下术语:
选项类型:有助于创建变体的产品属性,例如:尺寸、颜色、材料等。
选项值:特定选项类型可能的值。例如:Size选项类型可以有以下选项值Small,Medium,Large,Extra-Large
变体主题:变体主题是option type创建变体所需的列表
原型:原型不过是产品的模板。原型帮助我们轻松创建产品。它存储以下信息: - 产品属性 - 变体主题
我们有2个角色:Admin,Seller
以下是上述角色可以做的事情
prototypeSize选项类型可以有S, M, L,XL选项值我想建立一个具有不同模式的数据库,这些模式对应于不同的数据输入系统。需要有一个“master”(管理)模式和一个“party”表,以及多个其他模式,它们有自己的“party”表,这些表继承了 master party 表的字段
这是结构的一个简化示例:
'家长'
CREATE TABLE master.parties
(
party_key serial NOT NULL,
name text,
date_of_birth date,
phone text,
CONSTRAINT "PK_master_parties" PRIMARY KEY (party_key)
)
Run Code Online (Sandbox Code Playgroud)
'孩子'
CREATE TABLE corporate.parties
(
corp_key integer,
corp_role text
) INHERITS (master.parties)
Run Code Online (Sandbox Code Playgroud)
我希望能够删除添加到 master.parties 表中的一行,并将其插入到其他模式(即公司)中的任何party表中,而无需在主表中创建重复的行。
我在另一篇文章(/sf/ask/2065641371/)上找到的这个解决方案非常接近我正在寻找的为了:
WITH deleted AS (
DELETE FROM ONLY master.parties
WHERE party_key = 1
returning *
)
INSERT INTO corporate.parties (name, date_of_birth, phone, corp_key, corp_role)
SELECT name, date_of_birth, phone, 2, 'President'
from deleted;
Run Code Online (Sandbox Code Playgroud)
但是,在我看来,应该有更好的方法将“父”表中的一行添加到“子”表中,而不必删除“父”表中的现有行。
该解决方案还带来了另一个更严重的问题:如果对主模式中的参与方表进行了更改(例如添加了新列),我不想更新与子模式对应的所有数据输入系统。 -schemas 在每个 INSERT …
我的数据结构如下:
create table T
( field_1 char(1) not null
, field_2 int not null
, primary key(field_1, field_2)
);
insert into T (field_1, field_2)
values ('A',1)
,('A',2)
,('B',3)
,('C',3)
,('D',4)
,('E',4)
,('E',5)
,('F',6)
,('G',1);
Run Code Online (Sandbox Code Playgroud)
我想获得具有共同 field_1 或 field_2 的集合,如下所示:
field_1 | field_2 | set
-------------------------------
A | 1 | one
A | 2 | one
G | 1 | one
-------------------------------
B | 3 | two
C | 3 | two
-------------------------------
D | 4 | three
E | 4 …Run Code Online (Sandbox Code Playgroud) 我试图在带有数据列的表上强制执行唯一的月份和年份组合。例如
create table foo
(
mydate date
);
Run Code Online (Sandbox Code Playgroud)
我想强制每月和每年只有一行有效,即
insert into foo values ('2018-01-01'); -- valid
insert into foo values ('2018-01-15'); -- Wouldn't be valid as one row already exists for January 2018
insert into foo values ('2018-02-15'); -- valid
Run Code Online (Sandbox Code Playgroud)
天部分无关紧要。应用程序应该只插入当月的第一天,但只要每月和每年只有一行就没关系。
在 Oracle 或 SQL Server 中,我将能够在基于函数的唯一索引中使用确定性函数,因此我可以使用 date_trunc('month',mydate) 的结果,它会强制执行我想要的,但这似乎不是在 PostgreSQL 中是可能的。
我似乎也无法创建虚拟/计算/计算字段,因此也不能以这种方式强制执行。
我应该如何强制执行此约束?
我真的在做一些愚蠢的事情吗?
我正在寻找一种方法来消除 PostgreSQL 数组中的重复项,同时保留其元素的顺序。我目前拥有的是以下功能:
create function array_unique( anyarray )
returns anyarray immutable strict language sql as $$
select array( select distinct unnest( $1 ) ); $$;
create function array_unique_sorted( anyarray )
returns anyarray immutable strict language sql as $$
select array( select distinct unnest( $1 ) order by 1 ); $$;
/* ### TAINT there ought to be a simpler, declarative solution */
create function array_unique_stable( text[] )
returns text[] immutable strict parallel safe language plpgsql as $$
declare
R text[] …Run Code Online (Sandbox Code Playgroud) 我需要我的表,另外一个是在不同的数据库(比如表链接logs的device数据库和表accounts在user分贝,无论是同一台服务器上)。因此,使用外部数据包装器我创建了一个外部表(我通过select * from accounts;在device成功运行的db 中运行来检查它)。
但是我仍然无法从devicedb创建到外表的外键
CREATE TABLE public.logs
(
id bigint NOT NULL DEFAULT nextval('logs'::regclass),
...
account_id bigint,
...
CONSTRAINT logs_account_id_fkey FOREIGN KEY (account_id)
REFERENCES public.accounts (id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION
)
Run Code Online (Sandbox Code Playgroud)
错误信息是
ERROR: referenced relation "accounts" is not a table
********** Error **********
ERROR: referenced relation "accounts" is not a table
SQL state: 42809
Run Code Online (Sandbox Code Playgroud) 如何使用无限循环的 plperl 函数终止会话?
双方pg_terminate_backend并pg_cancel_backend没有任何影响。kill -1 pid什么也没做,kill -9 pid服务器崩溃了。
我的问题的小提琴可以在https://dbfiddle.uk/?rdbms=postgres_10&fiddle=e387589d446d9c9a952294f8c7a98494上找到。
我有简单的表格布局:
class
person: belongs to a class
room: belongs to a class
Run Code Online (Sandbox Code Playgroud)
以下查询选择嵌入了人员的所有类:
select class.identifier, array(select person.identifier from person where person.class_identifier = class.identifier) as persons
from class
order by class.identifier;
Run Code Online (Sandbox Code Playgroud)
在试验和学习更多有关相关子查询的信息时,我注意到可以通过用 aLEFT JOIN组合替换相关子查询来重写上面的查询GROUP BY:
select class.identifier, array_agg(person.identifier) as persons
from class
left join (
select person.identifier, person.class_identifier
from person
) as person
on class.identifier = person.class_identifier
group by class.identifier
order by class.identifier;
Run Code Online (Sandbox Code Playgroud)
请注意,我假设每个班级至少有一个人。如果没有,我可以添加coalesce()周围json_agg。
在我的第二种情况下,我将选择所有嵌入人员和房间的类。让我们首先以与上面第一个查询相同的方式编写:
select class.identifier, array(select …Run Code Online (Sandbox Code Playgroud) 大多数情况下,我们使用整数序列作为主键来保持表中行的唯一性。为此,我们不能使用时间戳而不是整数吗?该策略是否有任何缺陷,因为我认为机器不能同时“同时”做两件事......对吗?
我正在处理一个在键中包含大量休眠序列的数据库,所以我想使用时间戳作为解决方案。在实践中是否有任何类似的方法可以与任何数据库一起使用?要是知道就好了。
编辑:我要问的是,由于对我们(人类)来说,没有两个时刻是相同的,这对计算机也适用吗?我知道计算机通过某些晶体的振动来跟踪时间(不确定它可以测量多小的时间)。
在数据库中,我有一些表,除了时间(审计跟踪)外没有主键适用,并且指定一个序列(hibernate_sequence)将数字增加到极高的程度,这使得使用其他表不方便,因为它会影响所有表序列作为主键。
因此,如果 PostgreSQL 可以保证不会在同一时间戳上发生两次插入/更新操作,那么我可以将其用作主键,但如果不能,则显然我不能使用此解决方案。
基本问题是:PostgreSQL的规定,任何两个记录将被插入/同时更新任何保证(通过某些方法或配置)“时间”?
谢谢
我有这个数据:
CREATE TABLE tickets(user_id int NOT NULL);
INSERT INTO tickets VALUES (1);
INSERT INTO tickets VALUES (2);
INSERT INTO tickets VALUES (3); -- 3 times
INSERT INTO tickets VALUES (4); -- 10 times
Run Code Online (Sandbox Code Playgroud)
现在我想显示每个用户的票数百分比。
我试过这个:
WITH number_of_tickets AS (
SELECT user_id, COUNT(user_id) AS number_of_tickets_per_user
FROM tickets
GROUP BY user_id
)
SELECT
number_of_tickets_per_user,
ROUND((COUNT(user_id) * 100.0) / (SELECT COUNT(DISTINCT(user_id)) FROM tickets), 3) -- No no no no
FROM number_of_tickets
GROUP BY number_of_tickets_per_user
ORDER BY number_of_tickets_per_user;
Run Code Online (Sandbox Code Playgroud)
但我可能不能很好地处理百分比计算。结果总是显示每个用户每个票数的 25%。
谢谢
postgresql ×10
duplication ×2
array ×1
constraint ×1
foreign-key ×1
group-by ×1
hang ×1
inheritance ×1
kill ×1
order-by ×1
perl ×1
primary-key ×1
schema ×1
timestamp ×1