我的大多数约束名称都简洁而简短,但也有少数是长的,包含在一行中的名称超出了我正在使用的代码样式约定。
我不假思索地做了一个断线和一个双管,所以:
history uint4 constraint "[tablename] The beginning of history must be sooner " ||
"than the current day" check (history <= hdate)
这当然失败了,因为它是一个标识符,而不是字符串文字。我理解它为什么失败,这让我很沮丧,因为我几乎可以肯定,没有办法在第二行中断和继续标识符。
我错了吗?这里有什么把戏吗?看起来我应该能够在行的末尾使用,但不能缩进连续部分(因为这些空格将是标识符名称的一部分(。
您可以execute
一条语句,其中包含原始命令、格式、函数调用等。您可能希望将约束放在alter table
语句中,而不是将整个表创建语句包装在execute
中
do $$ BEGIN
EXECUTE 'CREATE TABLE tex (' ||
'hdate int, ' ||
'history int constraint "[tablename] The beginning of history must be sooner ' ||
'than the current day" check (history <= hdate));';
end $$;
d tex
Table "public.tex"
Column | Type | Collation | Nullable | Default
---------+---------+-----------+----------+---------
hdate | integer | | |
history | integer | | |
Check constraints:
"[tablename] The beginning of history must be sooner than the cu" CHECK (history <= hdate)
PS:尽管文本似乎太长,将被截断,无论字符串连接