简体   繁体   English

如何从同一个表中的现有列中添加一个表中的新列?

[英]How to add new column in a table from existing column in the same table?

I have a table with only 1 column which has several details concatenated into it like Product Name, Shipment Date etc:我有一个只有 1 列的表,其中连接了几个详细信息,如产品名称、发货日期等:

MASTERCOLUMN
Row1_Prod1_ShipDate_01-Dec-21
Row2_Prod2_ShipDate_03-Dec-21
Row3_Prod3_ShipDate_07-Dec-21
.
.

The requirement is to add another 2 columns containing only ProductName and ShipmentDate details corresponding to MASTERCOLUMN: (Eg below)要求是添加另外 2 列,其中仅包含与 MASTERCOLUMN 对应的 ProductName 和 ShipmentDate 详细信息:(例如下面)

MASTERCOLUMN                    ProductName   ShipmentDate
Row1_Prod1_ShipDate_01-Dec-21   Prod1         01-Dec-21
Row2_Prod2_ShipDate_03-Dec-21   Prod2         03-Dec-21
Row3_Prod3_ShipDate_07-Dec-21   Prod3         07-Dec-21
.
.
.

I've altered the table and added these new columns.我已经更改了表格并添加了这些新列。 Is there any way of updating the table so that corresponding values gets copied to these new columns?有什么方法可以更新表,以便将相应的值复制到这些新列中? Can this be achieved using SQL query?这可以使用 SQL 查询来实现吗? I am using SQL Developer with Oracle 19.c version.我正在使用 SQL 开发人员和 Oracle 19.c 版本。

I've tried updating the column after adding the 2 columns.. The below sample query is for updating the ProductName column:我尝试在添加 2 列后更新该列。下面的示例查询用于更新 ProductName 列:

UPDATE TABLE
SET ProductName = (SELECT SUBSTR(MASTERCOLUMN,6,5) FROM TABLE);

But this returns ORA-01427: single-row subquery returns more than one row error.但这会返回 ORA-01427:单行子查询返回多行错误。

However, I tried to update only one row and it worked fine;但是,我尝试只更新一行并且效果很好;

UPDATE TABLE
SET ProductName = (SELECT SUBSTR(MASTERCOLUMN,6,5) FROM TABLE)
WHERE MASTERCOLUMN = 'Row1_Prod1_ShipDate_01-Dec-21';

But I need to populate the entire table with corresponding values from MASTERCOLUMN column..但我需要用 MASTERCOLUMN 列中的相应值填充整个表。

Don't use subquery, you don't need it.不要使用子查询,你不需要它。

Sample table and rows:示例表和行:

SQL> create table test (mastercolumn varchar2(30));

Table created.

SQL> insert into test
  2    select 'Row1_Prod1_ShipDate_01-Dec-21' from dual union all
  3    select 'Row2_Prod2_ShipDate_03-Dec-21' from dual union all
  4    select 'Row3_Prod3_ShipDate_07-Dec-21' from dual;

3 rows created.

Add new columns:添加新列:

SQL> alter table test add
  2    (product_name varchar2(10),
  3     shipmentdate date);

Table altered.

Update:更新:

SQL> update test set
  2    product_name = substr(mastercolumn,
  3                          instr(mastercolumn, '_', 1, 1) + 1,
  4                          instr(mastercolumn, '_', 1, 2) - instr(mastercolumn, '_', 1,1 ) - 1
  5                         ),
  6    shipmentdate = to_date(substr(mastercolumn, -9), 'dd-mon-yy', 'nls_date_language=english');

3 rows updated.

Result:结果:

SQL> select * From test;

MASTERCOLUMN                   PRODUCT_NA SHIPMENTDA
------------------------------ ---------- ----------
Row1_Prod1_ShipDate_01-Dec-21  Prod1      01.12.2021
Row2_Prod2_ShipDate_03-Dec-21  Prod2      03.12.2021
Row3_Prod3_ShipDate_07-Dec-21  Prod3      07.12.2021

SQL>

But, as you've already been told, you'd rather choose another approach, one of them being a view :但是,正如您已经被告知的那样,您宁愿选择另一种方法,其中一种方法是view

SQL> create or replace view v_test as
  2    select mastercolumn,
  3           substr(mastercolumn,
  4                  instr(mastercolumn, '_', 1, 1) + 1,
  5                  instr(mastercolumn, '_', 1, 2) - instr(mastercolumn, '_', 1,1 ) - 1
  6                  ) as product_name,
  7           to_date(substr(mastercolumn, -9), 'dd-mon-yy', 'nls_date_language=english')
  8             as shipmentdate
  9  from test;

View created.

SQL> select * From v_test;

MASTERCOLUMN                   PRODUCT_NAME         SHIPMENTDA
------------------------------ -------------------- ----------
Row1_Prod1_ShipDate_01-Dec-21  Prod1                01.12.2021
Row2_Prod2_ShipDate_03-Dec-21  Prod2                03.12.2021
Row3_Prod3_ShipDate_07-Dec-21  Prod3                07.12.2021

SQL>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM