如何将列中的所有值移动到psql中的新列

How to move all the values in a column to a new column in psql

这是我的 table 结构:

Table name:Items
--------------------------------------------------------------
 id   | category_id |  name                |  alt_name
--------------------------------------------------------------
 1   | 1            |  Cochin State Manual |  Cochin
 2   | 1            |  Kuttavum Shikshayum |  Kuttavum
 2   | 1            |  Rajarshi            |  Rajarshihgfh

这里我需要将 alt_name 列的所有值移动到一个名为 temp.After 的新列,将名称列中的所有值移动到 alt_name column.In which name and alt_name are string type.The new column temp also a string field

首先,您需要检查 alt_name 列的类型,以便能够使 temp 正确的类型和长度以适应数据;

\d+ Items

例如,如果 alt_nameVARCHAR(64);

-- Add the column
ALTER TABLE Items ADD COLUMN temp VARCHAR(64);

-- Copy alt_name to temp
UPDATE Items SET temp = alt_name;

-- Copy name to alt_name
UPDATE Items SET alt_name = name;

An SQLfiddle to test with.

一如既往,在 运行 来自互联网上随机用户的潜在破坏性查询之前,请始终备份您的数据。