AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / user-50410

ceving's questions

Martin Hope
ceving
Asked: 2024-05-27 23:22:12 +0800 CST

为什么我的部分唯一索引不足以用作外键?

  • 6

我有以下代码,它会引发错误:

错误:没有与引用表“person”的给定键匹配的唯一约束

DROP TABLE IF EXISTS person;
CREATE TABLE person (
  sn        serial     PRIMARY KEY,
  id        int        NOT NULL,
  modified  timestamp,
  name      text
);
CREATE UNIQUE INDEX person_id_key ON person (id) WHERE modified IS NULL;
DROP TABLE IF EXISTS account;
CREATE TABLE account (
  id        int        NOT NULL REFERENCES person(id),
  name      text
);

有一个唯一索引id:

                                       Table "public.person"
  Column  |            Type             | Collation | Nullable |              Default               
----------+-----------------------------+-----------+----------+------------------------------------
 sn       | integer                     |           | not null | nextval('person_sn_seq'::regclass)
 id       | integer                     |           | not null | 
 modified | timestamp without time zone |           |          | 
 name     | text                        |           |          | 
Indexes:
    "person_pkey" PRIMARY KEY, btree (sn)
    "person_id_key" UNIQUE, btree (id) WHERE modified IS NULL

为什么外键不够?

postgresql
  • 1 个回答
  • 49 Views
Martin Hope
ceving
Asked: 2024-04-10 16:48:35 +0800 CST

如何捕获特定的异常?

  • 6

可以在 plpgsql 中捕获异常:

EXCEPTION WHEN unique_violation THEN

但如何检查是否违反特定约束呢?

以下示例创建两个键:一个用于a,一个用于b。如何知道是哪一个导致了异常呢?

create table t (
  a text unique,
  b text unique
);

create procedure insert_t(a text, b text)
language plpgsql
as $$
begin
  insert into t values (a, b);
  exception when unique_violation then
  raise notice 'is it a or b?';
end
$$;

call insert_t('x', 'foo');
call insert_t('x', 'bar');

select * from t;
postgresql
  • 1 个回答
  • 11 Views
Martin Hope
ceving
Asked: 2023-12-12 19:03:24 +0800 CST

如何使外键检查 NULL 值?

  • 6

我有一个表指定类型来区分标量和列表。当“标量”存在时,该值是一个标量,当它为空时,它是一个列表。

drop table type1 cascade;
create table type1 (
  id     integer primary key,
  scalar "char",
  name   text,
  unique (id, scalar)
);
insert into type1 values
  (1, 'x', 'single'),
  (2, null, 'multi');

现在我有一个包含值的表,它引用类型并在元组(类型,标量)上添加唯一约束。这是可行的,因为 NULL 是不同的。如果“标量”为 NULL,则每个“类型”可能有多于一行,因为所有“标量”都是不同的。

drop table value1 cascade;
create table value1 (
  id     integer primary key,
  type   integer,
  scalar "char",
  value  text,
  foreign key (type, scalar) references type1 (id, scalar),
  unique (type, scalar)
);

可以插入单个值:

insert into value1 values
  (11, 1, 'x', 'just one');

但不可能再添加一个:

insert into value1 values
  (12, 1, 'x', 'another one'); -- ERROR

但如果标量为 NULL,则可能有多个:

insert into value1 values
  (21, 2, null, 'first'),
  (22, 2, null, 'second');

我的问题:可以引用不存在的类型。

这可行,但不应该:

insert into value1 values
  (12, 1, null, 'another');

允许使用 (1, NULL) 作为类型,尽管类型表中只有 (1, 'x') 和 (2, NULL)。

我尝试在外键上使用match full,但这似乎做了不同的事情。它还阻止了我对多个空值的预期使用,因为它根本拒绝多个空值。

value1.scalar我想在if is NULL中允许多个 NULL type1.scalar。但我想拒绝value1.scalarif type1.scalaris not NULL 中的任何 NULL。我该如何表达这一点呢?

postgresql
  • 1 个回答
  • 38 Views
Martin Hope
ceving
Asked: 2023-12-11 23:24:24 +0800 CST

如何使用查询来设置自定义选项?

  • 5

我尝试了这个,但它不起作用:

CREATE OR REPLACE FUNCTION answer()
  RETURNS bigint
  LANGUAGE sql AS
$$
SELECT 42
$$;

SET session.answer = select answer();
postgresql
  • 1 个回答
  • 19 Views
Martin Hope
ceving
Asked: 2023-12-11 18:58:14 +0800 CST

如何定义一个只接受三个字符的函数?

  • 7

我试过这个:

create or replace function c3 (arg char(3))
  returns void
  language plpgsql as
$$
begin
  raise notice 'Value: %', arg;
end;
$$;

但它接受三个以上的字符:

select c3('1234');
postgresql
  • 1 个回答
  • 168 Views
Martin Hope
ceving
Asked: 2023-02-24 17:59:44 +0800 CST

`SQLSTATE '23000'` 是标准的一部分吗?

  • 5

以下答案使用SQLSTATE '23000'.

https://dba.stackexchange.com/a/130121/50410

这是 Postgresql 特有的东西还是 SQL 标准的一部分?

postgresql
  • 2 个回答
  • 17 Views
Martin Hope
ceving
Asked: 2020-01-23 08:36:11 +0800 CST

Postgres 多久根据 file_fdw 解析外部表?

  • 0

我有一个用 file_fdw 定义的外部表,它从 CSV 文件中读取数据。Postgresql 解析 CSV 文件并将日期例如从转换YYYYMMDD为YYYY-MM-DD. 这工作正常。

我的问题是解析 CSV 文件的频率如何?它会被解析select吗?还是被缓存了?如果是这样,它会被缓存多长时间?

postgresql csv
  • 1 个回答
  • 73 Views
Martin Hope
ceving
Asked: 2019-04-18 03:42:06 +0800 CST

如何使用部分索引向表添加约束?

  • 0

我有一个简单的表:

create table item (
  lan      text not null,
  disabled date
);

我的目标是确保lan列在所有行中的唯一性,其中disabledis null。

我创建了一个部分索引:

create unique index lan_idx on item (lan) where disabled is not null;

现在我尝试将索引添加到表中,但我在语法上苦苦挣扎。(我从本教程中获取了代码)

alter table item add constraint lan_idx using index lan_idx;

这会在“使用”之前引发语法错误。

我还尝试了以下方法:

alter table item add constraint lan_idx unique using index lan_idx;

但这会引发错误,即无法使用部分索引创建唯一约束。

谁能告诉我我做错了什么?

postgresql index
  • 1 个回答
  • 459 Views

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    连接到 PostgreSQL 服务器:致命:主机没有 pg_hba.conf 条目

    • 12 个回答
  • Marko Smith

    如何让sqlplus的输出出现在一行中?

    • 3 个回答
  • Marko Smith

    选择具有最大日期或最晚日期的日期

    • 3 个回答
  • Marko Smith

    如何列出 PostgreSQL 中的所有模式?

    • 4 个回答
  • Marko Smith

    列出指定表的所有列

    • 5 个回答
  • Marko Smith

    如何在不修改我自己的 tnsnames.ora 的情况下使用 sqlplus 连接到位于另一台主机上的 Oracle 数据库

    • 4 个回答
  • Marko Smith

    你如何mysqldump特定的表?

    • 4 个回答
  • Marko Smith

    使用 psql 列出数据库权限

    • 10 个回答
  • Marko Smith

    如何从 PostgreSQL 中的选择查询中将值插入表中?

    • 4 个回答
  • Marko Smith

    如何使用 psql 列出所有数据库和表?

    • 7 个回答
  • Martin Hope
    Jin 连接到 PostgreSQL 服务器:致命:主机没有 pg_hba.conf 条目 2014-12-02 02:54:58 +0800 CST
  • Martin Hope
    Stéphane 如何列出 PostgreSQL 中的所有模式? 2013-04-16 11:19:16 +0800 CST
  • Martin Hope
    Mike Walsh 为什么事务日志不断增长或空间不足? 2012-12-05 18:11:22 +0800 CST
  • Martin Hope
    Stephane Rolland 列出指定表的所有列 2012-08-14 04:44:44 +0800 CST
  • Martin Hope
    haxney MySQL 能否合理地对数十亿行执行查询? 2012-07-03 11:36:13 +0800 CST
  • Martin Hope
    qazwsx 如何监控大型 .sql 文件的导入进度? 2012-05-03 08:54:41 +0800 CST
  • Martin Hope
    markdorison 你如何mysqldump特定的表? 2011-12-17 12:39:37 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 对 SQL 查询进行计时? 2011-06-04 02:22:54 +0800 CST
  • Martin Hope
    Jonas 如何从 PostgreSQL 中的选择查询中将值插入表中? 2011-05-28 00:33:05 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 列出所有数据库和表? 2011-02-18 00:45:49 +0800 CST

热门标签

sql-server mysql postgresql sql-server-2014 sql-server-2016 oracle sql-server-2008 database-design query-performance sql-server-2017

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve