是否有类似“列符号链接”的东西?在甲骨文中?
我想暂时通过两个列名访问数据库中的一列。
为什么?列名选择错误,我想重构它。由于我希望我的 web 应用程序在更改列名称时保持稳定,因此最好
- 有一个名为 better_column_name 的符号链接(我们称之为),指向列 bad_column_name
- 更改Web应用程序使用better_column_name
- 删除符号链接并将列重命名为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
- have a (let's call it) symlink named better_column_name pointing to the column bad_column_name
- change the webapplication to use better_column_name
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
只要您有使用两个列名的代码,我就没有办法绕过该表中将有两个(真实)列的事实。
我将添加具有正确名称的新列,然后创建一个触发器来检查哪一列已被修改并相应地更新“其他”列。因此,无论更新什么,该值都会与其他列同步。
迁移所有使用旧列的代码后,删除触发器并删除旧列。
编辑
触发器会执行如下操作:
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:
The order of the
IF
statements controls which change has a "higher priority" in case someone updated both columns at the same time.重命名表:
使用原始表名创建一个视图,其中包含 bad_column_name 和 better_column_name 指向同一列(当然还有所有其他列):
因为该视图默认情况下是可更新的(我假设这里mytable 有一个主键),您可以从视图中插入/更新/删除,并且使用 bad_column_name 或 better_column_name 并不重要。
重构后,删除视图并重命名表和列:
Rename the table:
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):
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:
对此的最佳解决方案仅在 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.
您可以为表创建视图。并将您的应用程序移植为使用该视图而不是表。
you can create a view for the table. And port your application to use that view instead of the table.
如果您使用的是 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.