简体   繁体   English

在sql连接或嵌套查询中使用别名总是一个好习惯吗?

[英]Is it always a good practice to use aliases in sql joins or nested queries?

Is it always a best practice to use - 使用是否始终是最佳做法 -

Select E.Id,D.DeptName from Employee E join Dept D on E.DeptId=D.Id

instead of - 代替 -

Select Employee.Id,Dept.DeptName from Employee join Dept on Employee.DeptId=Dept.Id

Other than readability and reducing length of the query what are benefits of using aliases ? 除了可读性减少查询的长度 ,使用别名有什么好处? When I consulted with our Database expert he says query may break if there are no aliases at times...which I dont understand completely...I would appreciate if anyone would share their thoughts and what are the best practices to be followed...Thanks a lot. 当我咨询我们的数据库专家时,他说查询可能会破坏,如果有时候没有别名 ......我完全不理解......如果有人愿意分享他们的想法以及要遵循的最佳做法,我将不胜感激。 。非常感谢。

  1. You are probably confusing "needing to use a table prefix" and "needing to use an ALIAS" when referring to breaking things. 在提到破坏事物时,你可能会混淆“需要使用表前缀”和“需要使用ALIAS”。

    The query might indeed be more likely to break after adding a join if you don't use a table prefix; 如果不使用表前缀,则在添加连接后查询可能确实更容易中断; when your original table and a newly added table share a column with the same name. 当您的原始表和新添加的表共享具有相同名称的列时。 So for the sake of future maintenance, always using a table prefix is a GOOD idea for all columns in the query. 因此,为了将来的维护,始终使用表前缀对查询中的所有列都是一个好主意。

    However, this problem is solved by using ANY table prefix in front of columns, whether real table name or alias name. 但是,通过在列前面使用ANY表前缀来解决此问题,无论是真实的表名还是别名。

  2. The alias is needed (as opposed to actual table name) when you use the same table twice. 当您使用同一个表两次时, 需要别名(而不是实际的表名)。

  3. From a lot of experience with maintaining lots of complex SQL, I must say that my view is 100% opposite of yours. 从维护大量复杂SQL的大量经验来看,我必须说我的观点与你的观点完全相反。

    Namely, using a short - especially 1-letter - table alias makes for a HARDER to read/maintain code. 也就是说,使用短 - 特别是1个字母 - 表别名使HARDER能够读取/维护代码。

    When you're debugging a long piece of SQL with complex joints at 2am in the morning during production emergency, looking back/forth 10-15 lines above to see what table matches alias "e" is MUCH harder. 当你在生产紧急情况下早上2点调试一个带有复杂关节的长篇SQL时,在上面查看10-15行以查看哪个表匹配别名“e”要困难得多。

    This point has 2 exceptions 这一点有两个例外

    • when the business logic of the query uses the table for a purpose which is VERY distinct from the table name. 当查询的业务逻辑将表用于与表名非常不同的目的时。

    • when the table name is unreasonably long and unreasonable due to circumstances beyond your control - and then the alias should still be something readable and logical. 当表名由于你无法控制的情况而不合理地长而不合理时 - 别名应该仍然是可读和合乎逻辑的。 Eg " EmployeeTableIndexedByUIDSourcedFromHR " can and usually should be aliases as " Employee ", but not as " E " 例如,“ EmployeeTableIndexedByUIDSourcedFromHR ”可以并且通常应该是别名为“ Employee ”,但不是“ E

  4. Also, to avoid having overly long strings, it helps hreatly if you format your queries using newlines and alignment: 此外,为了避免使用过长的字符串,如果使用换行符和对齐格式化查询,则会有很大的帮助:

    Select Employee.Id,Dept.DeptName from Employee join Dept on Employee.DeptId=Dept.Id

vs VS

SELECT  Employee.Id
       ,Dept.DeptName
FROM    Employee
JOIN    Dept
ON      Employee.DeptId=Dept.Id

When you reference the same table twice, you have to use an alias. 当您两次引用同一个表时,必须使用别名。

Other than that, there isn't any technical reason I can think of. 除此之外,没有任何我能想到的技术原因。

I almost always do it because I hate to type out the full table name, and if you don't do it you can end up with ambiguous column names. 我几乎总是这样做,因为我讨厌输入完整的表名,如果你不这样做,你最终会得到模糊的列名。 Just don't use meaningless aliases such as t1, t2, etc. 只是不要使用无意义的别名,如t1,t2等。

The most readable way is to be explicit with a fully qualified name; 最可读的方式是使用完全限定名称显式; on that your DB expert and I agree. 在你的数据库专家和我同意。

But when you're developing them... aliases are your friend, hands down. 但是当你开发它们时......别名是你的朋友,请放下手。

  1. Increases readability 提高可读性
  2. Provides a way to join the same table on different join condition 提供在不同连接条件下连接同一个表的方法

I prefer to use the fully qualified table names in most cases. 在大多数情况下,我更喜欢使用完全限定的表名。 I usually only use an alias name for tables if a inner join is required between two tables in separate databases, just to make it a little more readable. 如果在单独的数据库中的两个表之间需要内部连接,我通常只对表使用别名,只是为了使它更具可读性。

Select  Employee.Name, sales.Amount
From    Employee
        Inner Join SalesDB.dbo.Sales as Sales On Employee.ID = Sales.EmployeeID

However, I would recommend using alias names for your fields in order to prevent changes required to calling applications down stream. 但是,我建议您为字段使用别名,以防止在下游调用应用程序所需的更改。

Select Employees.Name as [Name]
From   Employees

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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