[英]Add a column with a default value to an existing table in postgresql
Is there a postgres query to add a new column to an existing table and to automatically populate that column for all rows of the table with a certain value, let's say "A1"
, just once , as the column is created, so that I can still set the DEFAULT value of the column to another value, let's say "B2"
?是否有 postgres 查询将新列添加到现有表并使用特定值自动填充该表的所有行的该列,例如
"A1"
,仅一次,因为该列被创建,以便我可以仍然将列的 DEFAULT 值设置为另一个值,比如说"B2"
?
Just to be clear, I am looking for something like this:为了清楚起见,我正在寻找这样的东西:
Given my_table
:鉴于
my_table
:
name | work
------------------------
bob | fireman
carl | teacher
alice | policeman
my query我的查询
ALTER TABLE my_table
ADD COLUMN description varchar(100)
DEFAULT "B2"
COMMAND_I_D_WISH_TO_KNOW "A1";
changes my_table
into将
my_table
更改为
name | work | description
-------------------------------------
bob | fireman | "A1"
carl | teacher | "A1"
alice | policeman | "A1"
so that if afterwards I run the query这样如果之后我运行查询
INSERT INTO my_table(name, work)
VALUES karen, developer;
my_tables
becomes my_tables
变成
name | work | description
-------------------------------------
bob | fireman | "A1"
carl | teacher | "A1"
alice | policeman | "A1"
karen | developer | "B2"
Referencing the most recent docs , this operation can be done using two statements.参考最新的文档,可以使用两条语句完成此操作。
ALTER TABLE my_table ADD COLUMN description varchar(100) DEFAULT 'A1';
ALTER TABLE my_table ALTER COLUMN description SET DEFAULT 'B2'
A full reproducible sample has been included below:下面包含了一个完整的可重现样本:
CREATE TABLE my_table (
"name" VARCHAR(5),
"work" VARCHAR(9)
);
INSERT INTO my_table
("name", "work")
VALUES
('bob', 'fireman'),
('carl', 'teacher'),
('alice', 'policeman');
Query #1查询#1
select * from my_table;
name![]() |
work![]() |
---|---|
bob![]() |
fireman![]() |
carl![]() |
teacher![]() |
alice![]() |
policeman![]() |
Query #2查询#2
ALTER TABLE my_table
ADD COLUMN description varchar(100)
DEFAULT 'A1';
There are no results to be displayed.没有要显示的结果。
Query #3查询 #3
select * from my_table;
name![]() |
work![]() |
description![]() |
---|---|---|
bob![]() |
fireman![]() |
A1 ![]() |
carl![]() |
teacher![]() |
A1 ![]() |
alice![]() |
policeman![]() |
A1 ![]() |
Query #4查询 #4
ALTER TABLE my_table
ALTER COLUMN description SET DEFAULT 'B2';
There are no results to be displayed.没有要显示的结果。
Query #5查询 #5
INSERT INTO my_table("name", "work")
VALUES ('karen', 'developer');
There are no results to be displayed.没有要显示的结果。
Query #6查询 #6
select * from my_table;
name![]() |
work![]() |
description![]() |
---|---|---|
bob![]() |
fireman![]() |
A1 ![]() |
carl![]() |
teacher![]() |
A1 ![]() |
alice![]() |
policeman![]() |
A1 ![]() |
karen![]() |
developer![]() |
B2 ![]() |
View working demo on DB Fiddle查看 DB Fiddle 上的工作演示
Let me know if this works for you.让我知道这是否适合您。
Yes, you can do that by using two actions in one ALTER.是的,您可以通过在一个 ALTER 中使用两个操作来做到这一点。
ALTER TABLE my_table
ADD COLUMN description varchar(100) DEFAULT 'A1',
ALTER COLUMN description SET DEFAULT 'B2';
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.