简体   繁体   English

如何重命名SQL表列名,而不是破坏东西

[英]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 优点

  • Provides abstraction for future database refactoring. 为未来的数据库重构提供抽象。
  • May provide improvements and re-usability. 可以提供改进和可重用性。
  • Reduces any SQL injection attacks. 减少任何SQL注入攻击。

Cons 缺点

  • A lot of effort and risk in rewiring your code base, but you can do this in a piecemeal approach. 重新编写代码库需要付出很多努力和风险,但是你可以零敲碎打地做到这一点。
  • Not suitable for every type of application / service (EG, reports). 不适用于所有类型的应用/服务(EG,报告)。

Stored Procedures 存储过程

Depending on your RDBMS you can provide abstraction and additional security to the underlying data and schema using stored procedures. 根据您的RDBMS,您可以使用存储过程为底层数据和模式提供抽象和附加安全性。

Pros 优点

  • More code to maintain. 更多代码需要维护。
  • Not easily testable, although depending on your RDBMS there are plenty of database testing frameworks which should include SP coverage. 虽然取决于您的RDBMS,但是有很多数据库测试框架应该包括SP覆盖,因此不易测试。
  • Improved data security and reduces the risk of SQL injection attacks assuming that you're not constructing any dynamic SQL in your stored procedures. 假设您没有在存储过程中构建任何动态SQL,那么可以提高数据安全性并降低SQL注入攻击的风险。

Cons 缺点

  • Can be abused to intertwine your data access with domain / business logic. 可以滥用将您的数据访问与域/业务逻辑交织在一起。
  • You'll still need to update your code base to use the stored procs. 您仍然需要更新代码库以使用存储过程。

Views 查看

You could rename your existing table(s) Users and Orders to something else and use a view to offer the column name abstraction. 您可以将现有表UsersOrders重命名为其他Users ,并使用视图提供列名抽象。

Pros 优点

  • Offers some abstraction for your select statements an column aliasing. 为select语句提供一些抽象列列别名。
  • Can be done quickly and relatively easily. 可以快速,相对容易地完成。
  • May provide improvements if schema bound / index views are used. 如果使用模式绑定/索引视图,可能会提供改进。

Cons 缺点

  • Limited to select statements. 仅限于选择陈述。
  • Can be confusing to develop against. 可能会混淆发展反对。
  • Doesn't off any security against SQL injection attacks. 没有任何针对SQL注入攻击的安全性。
  • More code to maintain. 更多代码需要维护。

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 优点

  • Can provide abstraction for inserting data. 可以提供插入数据的抽象。

Cons 缺点

  • Probably the most risky option for providing abstraction. 可能是提供抽象的最危险的选择。
  • Only suitable for insert statements. 仅适用于插入语句。
  • Still susceptible to injection attacks when using direct inline SQL. 使用直接内联SQL时仍然容易受到注入攻击。
  • Triggers may not be supported for your data types. 您的数据类型可能不支持触发器。
  • More code to maintain. 更多代码需要维护。
  • Triggers are difficult to debug and often disliked due to the "hidden" nature. 由于“隐藏”性质,触发器很难调试并且经常不喜欢。

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.

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