在 Postgresql 中将一列的注释设置为另一列的注释
假设我在 Postgresql 中创建一个表,其中有一列有注释:
create table t1 (
c1 varchar(10)
);
comment on column t1.c1 is 'foo';
一段时间后,我决定添加另一列:
alter table t1 add column c2 varchar(20);
我想查找第一列的注释内容,并与新列关联:
select comment_text from (what?) where table_name = 't1' and column_name = 'c1'
(什么?)将是一个系统表,但是在 pgAdmin 中查看并在网络上搜索后,我还没有知道它的名称。
理想情况下,我希望能够:
comment on column t1.c1 is (select ...);
但我有一种感觉,事情有点牵强了。 感谢您的任何想法。
更新:根据我在这里收到的建议,我最终编写了一个程序来自动执行传输注释的任务,作为更改 Postgresql 列数据类型的更大过程的一部分。 您可以在我的博客上阅读相关内容。
Suppose I create a table in Postgresql with a comment on a column:
create table t1 (
c1 varchar(10)
);
comment on column t1.c1 is 'foo';
Some time later, I decide to add another column:
alter table t1 add column c2 varchar(20);
I want to look up the comment contents of the first column, and associate with the new column:
select comment_text from (what?) where table_name = 't1' and column_name = 'c1'
The (what?) is going to be a system table, but after having looked around in pgAdmin and searching on the web I haven't learnt its name.
Ideally I'd like to be able to:
comment on column t1.c1 is (select ...);
but I have a feeling that's stretching things a bit far. Thanks for any ideas.
Update: based on the suggestions I received here, I wound up writing a program to automate the task of transferring comments, as part of a larger process of changing the datatype of a Postgresql column. You can read about that on my blog.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
接下来要知道的是如何获取表 oid。 我认为,正如您所怀疑的那样,将此用作评论的一部分是行不通的。
不管怎样,我创建了一个快速的 plpgsql 函数来将注释从一个表/列对复制到另一个。 您必须在数据库上 createlang plpgsql 并像这样使用它:
The next thing to know is how to obtain the table oid. I think that using this as part of comment on will not work, as you suspect.
Anyhow, I whipped up a quick plpgsql function to copy comments from one table/column pair to another. You have to createlang plpgsql on the database and use it like this:
您可以使用系统函数 col_description(table_oid, column_number) 检索列上的注释。 请参阅此页面了解更多细节。
You can retrieve comments on columns using the system function col_description(table_oid, column_number). See this page for further details.