简体   繁体   English

向 postgresql 中的现有表添加具有默认值的列

[英]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 intomy_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.参考最新的文档,可以使用两条语句完成此操作。

  1. Adds the column with the old default value添加具有旧默认值的列

ALTER TABLE my_table ADD COLUMN description varchar(100) DEFAULT 'A1';

  1. Modifies the column to use a different default value修改列以使用不同的默认值

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 A1
carl卡尔 teacher老师 A1 A1
alice爱丽丝 policeman警察 A1 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 A1
carl卡尔 teacher老师 A1 A1
alice爱丽丝 policeman警察 A1 A1
karen卡伦 developer开发商 B2 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.

相关问题 使用约束和默认值将列添加到现有PostgreSQL表中 - Adding a column to existing PostgreSQL table with constraint and default value 向表中添加一列,其默认值等于现有列的值 - Add a column to a table with a default value equal to the value of an existing column 有条件地向 SQL Server 中的现有表添加具有默认值的列 - Conditionally add a column with a default value to an existing table in SQL Server JOOQ如何将具有默认值的列添加到现有表中 - JOOQ how to add column with default value into an existing table 向 SQL Server 中的现有表添加具有默认值的列 - Add a column with a default value to an existing table in SQL Server 如何将具有默认值的列添加到现有的 sql 表中? - How to add a column with a default value to an existing sql table? Postgres - 将列添加到具有默认值的表中,但将现有行设置为不同的默认值 - Postgres - Add a column to a table with a default value, but set existing rows to a different default value postgresql - 将boolean列添加到表集默认值 - postgresql - add boolean column to table set default 通过 Apache Phoenix 向表中添加一个列,其默认值等于 HBase 中现有列的值 - Add a column to a table with a default value equal to the value of an existing column in HBase through Apache Phoenix 使用默认值将列添加到表 - Add Column to Table with Default Value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM