使用 PostgreSQL 9.6,我可以创建一个类型为“not-null-array of string”的列:
CREATE TABLE example (
foo TEXT[] NOT NULL
);
Run Code Online (Sandbox Code Playgroud)
但这允许元素为空,即我可以这样做:
INSERT INTO example VALUES('{NULL}')
Run Code Online (Sandbox Code Playgroud)
有没有办法创建一个类型为“非空字符串的非空数组”的列?我想要这样的东西:
CREATE TABLE example (
foo (NOT NULL TEXT)[] NOT NULL
);
Run Code Online (Sandbox Code Playgroud)
但它在语法上无效。有没有有效的方式来表达这一点?
自第 9.5 页起更简单,添加了array_position()
CREATE TABLE example (
foo TEXT[] NOT NULL check (array_position(foo, null) is null)
);
Run Code Online (Sandbox Code Playgroud)
您可能还想检查空数组:
CREATE TABLE example (
foo TEXT[] NOT NULL check (foo <> '{}' and array_position(foo, null) is null)
);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2782 次 |
| 最近记录: |