简体   繁体   English

动态地向数据库表添加列?

[英]Adding columns to database table dynamically?

I'm using Java EE, Oracle db and JPA: 我正在使用Java EE,Oracle db和JPA:

I need to create a common table in oracle. 我需要在oracle中创建一个公用表。 It can be edited using an ui, for example: 它可以使用ui编辑,例如:

id|tax1|tax2|tax3
------------------
1 | 5  |  16|  9
2 | 7  |  1 |  8

The user must be able to add a new column; 用户必须能够添加新列; the previous table must look like this one: 上一个表必须如下所示:

id|tax1|tax2|tax3|tax4    (the tax4 column was added)
-----------------------
1 | 5  |  16|  9 |  0
2 | 7  |  1 |  8 |  0

It seems easy just to add a botton that invokes an "alter table" instruction, but i don't want to give the user that kind of permission. 只是添加一个调用“alter table”指令的botton似乎很容易,但我不想给用户那种权限。 I was thinking about representing each of the columns as rows in another table and with a Java process build the table in the UI. 我正在考虑将每个列表示为另一个表中的行,并使用Java进程在UI中构建表。

Another unrelated problem I have is: the data in the table affects directly a calculation, if any column is added, the calculation must consider the new column/columns. 我遇到的另一个不相关的问题是:表中的数据直接影响计算,如果添加了任何列,则计算必须考虑新的列/列。

"Another unrelated problem I have is: the data in the table affects directly a calculation, if any column is added, the calculation must consider the new column/columns." “我遇到的另一个不相关的问题是:表中的数据直接影响计算,如果添加了任何列,计算必须考虑新的列/列。”

What you meant to say was "Another completely related problem is ...". 你的意思是“另一个完全相关的问题是......”。 Because this statement highlights the profound implications of your proposed implementation. 因为本声明强调了您提议的实施的深远影响。 Dynamically adding a column is the easy bit. 动态添加列很容易。

The difficult bit is the concomitant need to conduct an impact analysis of the code base and assess all the SQL which references that table. 困难的是同时需要对代码库进行影响分析并评估引用该表的所有SQL。 After all, it is not enough to include the new column in that calculation. 毕竟,在该计算中包含新列是不够的。 There may be other SELECTs which need to be amended (e,g, reports). 可能还有其他需要修改的SELECT(例如,报告)。 And of course you need to get data into the new column, so that means amending UPDATE and INSERT statements. 当然,您需要将数据放入新列,这意味着修改UPDATE和INSERT语句。 This sort of thing is hard enough to do with a human being in charge. 这种事情很难与一个人负责。 Writing dynamic SQL to do it automatically? 编写动态SQL来自动完成吗? Madness. 疯狂。

I do hope you're not writing your application for any organisation which has responsibility for my own tax affairs. 我希望你没有为任何负责我自己税务事务的组织编写你的申请。

Anyway, your problem derives from treating new data as a structural change, rather than what is it, a content change. 无论如何,你的问题来自于将新数据视为结构变化,而不是内容变化。 It is quite clear that a table declared as yours is, with repeating columns (TAX1, TAX2, TAX3), is a part of a poor data model and needs further normalisation. 很明显,声明为您的表是重复列(TAX1,TAX2,TAX3),是不良数据模型的一部分,需要进一步规范化。

It is true that this might make your tax calculation more complicated, but that is a good thing. 确实,这可能会使您的税务计算更加复杂,但这是一件好事。 The complexity should be in the business rule implementation not the data model. 复杂性应该在业务规则实现中,而不是数据模型中。 At least this way the complexity is constrained to a single program unit instead of being strewn through your application's CRUD functionality. 至少通过这种方式,复杂性受限于单个程序单元,而不是通过应用程序的CRUD功能散布。


For the sake of completeness, here is the path to the Mountains of Madness: 为了完整起见,这里是通往疯狂山脉的道路:

create or replace procedure add_new_col as
    n pls_integer;
begin
    select count(*) into n
    from user_tab_columns
    where table_name = 'YOUR_TABLE'
    and column_name like 'TAX%'; 

    execute immediate 'alter table your_table add tax'||trim(to_char(n+1))||' number';
end;

You can give the user execute privileges on this procedure without granting them broader rights on the underlying table. 您可以授予用户对此过程的执行权限,而无需授予他们对基础表的更广泛权限。

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

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