SQL表别名-好还是坏? [英] SQL Table Aliases - Good or Bad?
问题描述
在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屋!