假设我有一个包含字段A和的表B。我在A+上进行常规查询B,所以我在 上创建了一个复合索引(A,B)。A复合索引是否也会对查询进行全面优化?
此外,我在 上创建了一个索引A,但 Postgres 仍然只使用复合索引来查询A。如果前面的答案是肯定的,我想这并不重要,但是为什么它默认选择复合索引,如果单个A索引可用?
我有一个专栏: standard BOOLEAN NOT NULL
我想强制执行一行 True,而所有其他行都是 False。根据此约束,没有 FK 或其他任何东西。我知道我可以用 plpgsql 完成它,但这似乎是一个大锤。我更喜欢 a CHECKorUNIQUE约束之类的东西。越简单越好。
一行必须为 True,它们不能全部为 False(因此插入的第一行必须为 True)。
该行将需要更新,这意味着我必须等待检查约束,直到更新完成,因为所有行可能先设置为 False,然后再将一行设置为 True。
products.tax_rate_id和之间有一个 FK tax_rate.id,但它与默认或标准税率无关,用户可选择以轻松创建新产品。
PostgreSQL 9.5 如果重要的话。
表是税率。其中一种税率是默认值(standard因为默认值是 Postgres 命令)。添加新产品时,标准税率适用于该产品。如果没有standard,则数据库必须进行猜测或进行各种不需要的检查。我想,简单的解决方案是确保有一个standard.
上面的“默认”是指表示层(UI)。用户可以选择更改默认税率。我要么需要添加额外的检查以确保 GUI/用户不会尝试将 tax_rate_id 设置为 NULL,或者只是设置默认税率。
我在 SQL 中对电气原理图建模时遇到了一些麻烦。我想捕获的结构是
part ??????????? pin
? ?
part_inst ?????? pin_inst
Run Code Online (Sandbox Code Playgroud)
其中“inst”是“instance”的缩写。
例如,我可能part将 LM358 运算放大器pin用作 1OUT、1IN-、1IN+、GND、2IN+、2IN-、2OUT 和 V CC。然后我可能会将这部分放在原理图上,创建 apart_inst和 8
pin_insts。
忽略数据字段,我对模式的最初尝试是
create table parts (
part_id bigserial primary key
);
create table pins (
pin_id bigserial primary key,
part_id bigint not null references parts
);
create table part_insts (
part_inst_id bigserial primary key,
part_id bigint not null references parts
);
create table pin_insts (
pin_inst_id bigserial primary key,
part_inst_id bigint …Run Code Online (Sandbox Code Playgroud) postgresql foreign-key database-design referential-integrity polymorphic-associations
假设我有一个名为 的表people,其中id是主键:
+-----------+---------+---------+
| id | fname | lname |
| (integer) | (text) | (text) |
+===========+=========+=========+
| 1 | Daniel | Edwards |
| 2 | Fred | Holt |
| 3 | Henry | Smith |
+-----------+---------+---------+
Run Code Online (Sandbox Code Playgroud)
我正在尝试编写一个行重复查询,该查询足够健壮,可以解释表的架构更改。每当我向表中添加一列时,我都不想回去修改重复查询。
我知道我可以做到这一点,这将复制记录 ID 2 并为复制的记录提供一个新 ID:
INSERT INTO people (fname, lname) SELECT fname, lname FROM people WHERE id = 2;
Run Code Online (Sandbox Code Playgroud)
但是,如果我添加一age列,则需要修改查询以同时考虑年龄列。
显然我不能执行以下操作,因为它还会复制主键,从而导致duplicate key value violates unique constraint-- 而且,无论如何我都不希望它们共享相同的 ID:
INSERT INTO people …Run Code Online (Sandbox Code Playgroud) 考虑以下业务领域:
Airline具有唯一的航空公司 ID ( aid) 并包含零个或多个Planes。Plane有一个唯一的飞机 id ( pid) Airline(但来自不同国家的飞机Airlines可以有重叠ids)。Plane都有一个或多个Seats。Seat有一个唯一的座位 id ( sid) (但Seats不同的座位 idPlanes可能有重叠ids)。这是我解决这个问题的尝试:
CREATE SEQUENCE planes_seq;
CREATE TABLE Airlines (
aid INTEGER PRIMARY KEY DEFAULT nextval('planes_seq')
);
CREATE TABLE Planes (
aid INTEGER REFERENCES Airlines(aid)
, pid INTEGER
, PRIMARY KEY(aid, pid)
);
CREATE TABLE Seats ( …Run Code Online (Sandbox Code Playgroud) 我已经啮合在一起的方式来确定什么data_type是在data_type创建基于掀起了新表时,你的语法使用PostgreSQL的维基页面。
如果我的查询有问题,我需要真正知道在给定场景中什么会在显式上下文中抛出它在纯粹的测试数据库/表上运行一个或多个查询以修改该数据库/表,所以运行这个查询以测试任何误报。
SELECT pg_attribute.attname,
format_type(pg_attribute.atttypid, pg_attribute.atttypmod),
CASE
WHEN format_type(pg_attribute.atttypid, pg_attribute.atttypmod)='bigint' THEN 'bigserial'
WHEN format_type(pg_attribute.atttypid, pg_attribute.atttypmod)='integer' THEN 'serial'
END AS type
FROM pg_index, pg_class, pg_attribute
WHERE pg_class.oid = 'delete2'::regclass
AND indrelid = pg_class.oid
AND pg_attribute.attrelid = pg_class.oid
AND pg_attribute.attnum = any(pg_index.indkey)
AND indisprimary;
Run Code Online (Sandbox Code Playgroud)
这是一个带有主键的表,该表不使用此查询返回主键:
CREATE TABLE delete_key_bigserial (
test1 integer,
id bigserial NOT NULL,
col1 text,
col2 text,
test2 integer
);
Run Code Online (Sandbox Code Playgroud) postgresql ×6
constraint ×2
catalogs ×1
datatypes ×1
dynamic-sql ×1
foreign-key ×1
index ×1
index-tuning ×1
performance ×1
primary-key ×1
sequence ×1