SQL 表别名 - 好还是坏? [英] SQL Table Aliases - Good or Bad?

查看:34
本文介绍了SQL 表别名 - 好还是坏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 SQL 中使用表别名的优缺点是什么?我个人尽量避免使用它们,因为我认为它们会使代码的可读性降低(尤其是在阅读大型 where/and 语句时),但我很想听听对此的任何反对意见.通常什么时候使用表别名是一个好主意,您有任何首选格式吗?

What are the pros and cons of using table aliases in SQL? I personally try to avoid them, as I think they make the code less readable (especially when reading through large where/and statements), but I'd be interested in hearing any counter-points to this. When is it generally a good idea to use table aliases, and do you have any preferred formats?

推荐答案

在处理高度规范化的模式时,表别名是必不可少的.例如,我不是这个数据库的架构师,所以请耐心等待,它可能需要 7 个连接才能获得干净完整的记录,其中包括一个人的姓名、地址、电话号码和公司隶属关系.

Table aliases are a necessary evil when dealing with highly normalized schemas. For example, and I'm not the architect on this DB so bear with me, it can take 7 joins in order to get a clean and complete record back which includes a person's name, address, phone number and company affiliation.

我倾向于使用短单词别名,而不是有些标准的单字符别名,因此上面示例的 SQL 最终看起来像:

Rather than the somewhat standard single character aliases, I tend to favor short word aliases so the above example's SQL ends up looking like:

select person.FirstName
      ,person.LastName
      ,addr.StreetAddress
      ,addr.City
      ,addr.State
      ,addr.Zip
      ,phone.PhoneNumber
      ,company.CompanyName
from tblPeople person
left outer join tblAffiliations affl on affl.personID = person.personID
left outer join tblCompany company on company.companyID = affl.companyID

...等

这篇关于SQL 表别名 - 好还是坏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆