[英]How to rename SQL table column name(s) and not break stuff
I revisited some of my code from my beginner days and found that some of the SQL table column names are so ambiguous it made me cringe. 我从初学者那里重新访问了一些代码,发现一些SQL表的列名是如此模糊,让我感到畏缩。
Now If I go ahead and change the names, the time and effort required to correct all the mappings in the code doesn't seem justifiable at this point. 现在,如果我继续更改名称,那么纠正代码中所有映射所需的时间和精力似乎在这一点上是不合理的。
I was wondering if its possible to provide an alias at all when inserting to the DB? 我想知道在插入数据库时是否可以提供别名?
I ask because you can use an alias on a SELECT
like this: 我问,因为你可以在SELECT
上使用别名,如下所示:
SELECT users.id, username, number AS order_number FROM users INNER JOIN orders ON users.id = orders.user_id;
Or does anyone have any other suggestions on how I can accomplish this? 或者有没有人对我如何做到这一点有任何其他建议?
While refactoring a database is no doubt a large and risk activity here are some tips to mitigate risk. 虽然重构数据库无疑是一个很大的风险活动,但这里有一些缓解风险的技巧。 Below are some suggestions with various pros and cons (as I see the them) hopefully they will help. 以下是一些有各种利弊的建议(我看到它们)希望他们会有所帮助。
Code 码
Depending on your programming language, comfort and time frame you can replace inline direct SQL with a RDBMS independent object relational mapper like Hibernate / NHibernate etc. 根据您的编程语言,舒适度和时间范围,您可以使用像Hibernate / NHibernate等RDBMS独立对象关系映射器替换内联直接SQL。
Pros 优点
Cons 缺点
Stored Procedures 存储过程
Depending on your RDBMS you can provide abstraction and additional security to the underlying data and schema using stored procedures. 根据您的RDBMS,您可以使用存储过程为底层数据和模式提供抽象和附加安全性。
Pros 优点
Cons 缺点
Views 查看
You could rename your existing table(s) Users
and Orders
to something else and use a view to offer the column name abstraction. 您可以将现有表Users
和Orders
重命名为其他Users
,并使用视图提供列名抽象。
Pros 优点
Cons 缺点
Facade tables Combined with the views suggestion you can create facade tables with revised column naming and security access. Facade表结合视图建议,您可以创建具有修订列命名和安全访问权限的Facade表。 When data is inserted into the facade table use triggers as the abstraction mechanism to populate the old tables. 将数据插入facade表时,使用触发器作为填充旧表的抽象机制。
Pros 优点
Cons 缺点
you can wrap your table in a view and then do inserts into the view. 您可以将表包装在视图中,然后在视图中插入。
create view view_nice_column_names
as
SELECT bad_name_1 as nice_name_1, bad_name_2_as nice_name_2 FROM blabla
GO
INSERT INTO view_nice_column_names (nice_name_1, nice_name_2) VALUES ( ...)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.