是否有类似“列符号链接”的东西?在甲骨文中?

发布于 2024-11-04 00:06:57 字数 387 浏览 1 评论 0原文

我想暂时通过两个列名访问数据库中的一列。

为什么?列名选择错误,我想重构它。由于我希望我的 web 应用程序在更改列名称时保持稳定,因此最好

  1. 有一个名为 better_column_name 的符号链接(我们称之为),指向列 bad_column_name
  2. 更改Web应用程序使用better_column_name
  3. 删除符号链接并将列重命名为better_column_name

“重构数据库”建议实际上添加在提交时同步的第二列以实现此目的。我只是希望 Oracle 有一种更简单的方法,可以减少工作量和开销。

I would like to have a column in my DB accessible via two column names temporarily.

Why? The column name was badly chosen, I would like to refactor it. As I want my webapp to remain stable while changing the column name, it would be good to

  1. have a (let's call it) symlink named better_column_name pointing to the column bad_column_name
  2. change the webapplication to use better_column_name
  3. drop the symlink and rename column to better_column_name

"Refactoring Databases" suggests to actually add a second column which is synchronized on commit in order to achieve this. I am just hoping that there might be an easier way with Oracle, with less work and less overhead.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

倦话 2024-11-11 00:06:57

只要您有使用两个列名的代码,我就没有办法绕过该表中将有两个(真实)列的事实。

我将添加具有正确名称的新列,然后创建一个触发器来检查哪一列已被修改并相应地更新“其他”列。因此,无论更新什么,该值都会与其他列同步。

迁移所有使用旧列的代码后,删除触发器并删除旧列。

编辑

触发器会执行如下操作:

CREATE OR REPLACE TRIGGER ...
    ...
    UPDATE OF bad_column_name, better_column_name ON the_table 
    ...
BEGIN
  IF UPDATING ('BAD_COLUMN_NAME') THEN 
     :new.better_column_name = :new.bad_column_name
  END IF;

  IF UPDATING ('BETTER_COLUMN_NAME') THEN 
     :new.bad_column_name = :new.better_column_name
  END IF;
END;

IF 语句的顺序控制哪个更改具有“更高优先级”,以防有人同时更新两列。

As long as you have code that uses both column names, I don't see a way to get around the fact that you'll have two (real) columns in that table.

I would add the new column with the correct name and then create a trigger that checks which column has been modified and updates the "other" column correspondingly. So whatever is being updated, the value is synch'ed with the other column.

Once all the code that uses the old column has been migrated, remove the trigger and drop the old column.

Edit

The trigger would so do something like this:

CREATE OR REPLACE TRIGGER ...
    ...
    UPDATE OF bad_column_name, better_column_name ON the_table 
    ...
BEGIN
  IF UPDATING ('BAD_COLUMN_NAME') THEN 
     :new.better_column_name = :new.bad_column_name
  END IF;

  IF UPDATING ('BETTER_COLUMN_NAME') THEN 
     :new.bad_column_name = :new.better_column_name
  END IF;
END;

The order of the IF statements controls which change has a "higher priority" in case someone updated both columns at the same time.

罗罗贝儿 2024-11-11 00:06:57

重命名表:

alter table mytable rename to mytable_old;

使用原始表名创建一个视图,其中包含 bad_column_name 和 better_column_name 指向同一列(当然还有所有其他列):

create or replace view mytable as
  select column1
  , column2
  , ...
  , bad_column_name
  , bad_column_name better_column_name
  from mytable_old
;

因为该视图默认情况下是可更新的(我假设这里mytable 有一个主键),您可以从视图中插入/更新/删除,并且使用 bad_column_name 或 better_column_name 并不重要。

重构后,删除视图并重命名表和列:

drop view mytable;
alter table mytable_old rename column bad_column_name to better_column_name;
alter table mytable_old rename to mytable;

Rename the table:

alter table mytable rename to mytable_old;

Create a view with the original tablename with both bad_column_name and better_column_name that point to the same column (and of course all the other columns):

create or replace view mytable as
  select column1
  , column2
  , ...
  , bad_column_name
  , bad_column_name better_column_name
  from mytable_old
;

Since this view is updatable by default (I assume here that mytable has a primary key), you can insert/update/delete from the view and it doesn't matter if you use bad_column_name or better_column_name.

After the refactoring, drop the view and rename the table and column:

drop view mytable;
alter table mytable_old rename column bad_column_name to better_column_name;
alter table mytable_old rename to mytable;
最美不过初阳 2024-11-11 00:06:57

对此的最佳解决方案仅在 Oracle 11g 第 2 版:基于版本的重定义中提供。这个非常酷的功能允许我们使用特殊的触发器和视图来维护不同版本的数据库表和 PL/SQL 代码。 了解详情

本质上,这是 Oracle 的 @ 的内置实现AHorseWithNoName 的建议

The best solution to this is only available in Oracle 11g Release 2: Edition-based Redefinition. This really cool feature allows us to maintain different versions of database tables and PL/SQL code, using special triggers and views. Find out more.

Essentially this is Oracle's built-in implementation of @AHorseWithNoName's suggestion.

∞琼窗梦回ˉ 2024-11-11 00:06:57

您可以为表创建视图。并将您的应用程序移植为使用该视图而不是表。

create table t (bad_name varchar2(10), c2 varchar2(10));
create view vt as select bad_name AS good_name, c2 from t;

insert into vt (good_name, c2) values ('blub', 'blob');

select * from t;
select * from vt;

you can create a view for the table. And port your application to use that view instead of the table.

create table t (bad_name varchar2(10), c2 varchar2(10));
create view vt as select bad_name AS good_name, c2 from t;

insert into vt (good_name, c2) values ('blub', 'blob');

select * from t;
select * from vt;
叫嚣ゝ 2024-11-11 00:06:57

如果您使用的是 11g,您可以考虑使用虚拟列。我可能会想稍微改变一下顺序;重命名真实列并使用旧(坏)名称创建虚拟列,然后可以在闲暇时将其删除。当然,您可能会受到限制,并且可能会对其他对象失效产生影响,从而使该订单不太适合您。

If you're on 11g you could look at using a virtual column. I'd probably be tempted to change the order slightly; rename the real column and create the virtual one using the old (bad) name, which can then be dropped at leisure. You may be restricted, of course, and there may be implications on other objects being invalidated that make this order less suitable for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文