我认为这是主观问题之一,并且最终会成为一个偏好问题,但是:
从可读性的角度来看,我主张仅在必要时才明确。 SQL 语句通常足够复杂,无需阅读大量不必要的文本。
我认为
SELECT TOP 1000 [StoreNumber]
,[Address1]
,[Address2]
,[City]
,[St]
,[Zip]
,[ZipSuffix]
,[LocationType]
,[LocationSubType]
,[Corp]
,[Division]
,[ZoneNumber]
,[DistrictNumber]
,[StateNumber]
FROM [CommonData].[dbo].[vw_StoreData]
比
更具可读性
SELECT TOP 1000 [CommonData].[dbo].[vw_StoreData].[StoreNumber]
,[CommonData].[dbo].[vw_StoreData].[[Address1]
,[CommonData].[dbo].[vw_StoreData].[[Address2]
,[CommonData].[dbo].[vw_StoreData].[[City]
,[CommonData].[dbo].[vw_StoreData].[[St]
,[CommonData].[dbo].[vw_StoreData].[[Zip]
,[CommonData].[dbo].[vw_StoreData].[[ZipSuffix]
,[CommonData].[dbo].[vw_StoreData].[[LocationType]
,[CommonData].[dbo].[vw_StoreData].[[LocationSubType]
,[CommonData].[dbo].[vw_StoreData].[[Corp]
,[CommonData].[dbo].[vw_StoreData].[[Division]
,[CommonData].[dbo].[vw_StoreData].[[ZoneNumber]
,[CommonData].[dbo].[vw_StoreData].[[DistrictNumber]
,[CommonData].[dbo].[vw_StoreData].[[StateNumber]
FROM [CommonData].[dbo].[vw_StoreData]
(当您开始联接表时会变得更糟,如果您要跨不同数据库联接表,情况会更糟。)
我可以看出您在哪里可以争辩说第二个更具可读性如果您需要通过查看来确切知道特定字段来自哪个数据库、架构和表仅在查询中。
但在 SQL Server 中,例如,您可以在设计器中打开该查询,并以更友好的图形视图查看它。
恕我直言,我唯一会使用完整语法的情况是必要时、跨越表/数据库/模式边界时,或者如果您有两个表具有相同的字段名称。
例子:
SELECT TOP 1000 [CommonData].[dbo].[vw_StoreData].[StoreNumber]
,[Address1]
,[Address2]
,[City]
,[St]
,[Zip]
,[ZipSuffix]
,[LocationType]
,[LocationSubType]
,[Corp]
,[Division]
,[ZoneNumber]
,[DistrictNumber]
,[StateNumber]
FROM [CommonData].[dbo].[vw_StoreData]
Inner Join [CommonData].[dbo].[vw_StorePhones]
ON [CommonData].[dbo].[vw_StorePhones].[StoreNumber] = [CommonData].[dbo].[vw_StoreData].[StoreNumber]
即使在这种情况下,我也会使用 Table Aliases 来缩短它并使其更具可读性。
综上所述,在现实世界中,您很可能会发现自己为一家已经确定了标准格式的公司工作,并且您需要根据该公司的标准进行编码。