Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Postgresql 将表列作为唯一索引时保持为空_Postgresql - Fatal编程技术网

Postgresql 将表列作为唯一索引时保持为空

Postgresql 将表列作为唯一索引时保持为空,postgresql,Postgresql,如果表列被定义为唯一的,是否可以将其保持为空 表模式 Column | Type | Modifiers | Description -------------------+------------------------+---------------+------------- id | integer | not null | name

如果表列被定义为唯一的,是否可以将其保持为空

表模式

      Column       |          Type          |   Modifiers   | Description 
-------------------+------------------------+---------------+-------------
 id                | integer                | not null      | 
 name              | character varying(64)  |               | 
索引

Indexes:
    "clients_pkey" PRIMARY KEY, btree (id)
    "clients_name_idx" UNIQUE, btree (name)
Has OIDs: yes

由于对应用程序的修改,有时名称列需要为空,这可能吗?

如果该列可以包含
NULL
值,则这是可以的,因为
NULL
不包括在索引中


请注意,有些数据库没有正确地实现该标准(有些版本的SQL Server只允许每个唯一约束有一个
NULL
值,但我确信情况仍然如此)。

使用NULL是更好的选择,但也可以使用条件唯一索引:

CREATE UNIQUE INDEX unique_clients_name ON clients (name) WHERE name <> '';
在名称为“”的客户端(name)上创建唯一索引UNIQUE_clients_name;
避免老年痴呆症,这些都是无用的和过时的