8

我正在设计一个数据库,最近在表 DayOfWeek 中命名了一个列,完全忘记了 DayOfWeek 是 SQL Server 中的内置函数。现在我决定是否应该保持原样并使用方括号 [DayOfWeek] 引用列,或者更改列名以避免将来发生任何冲突。我对这个项目并不太了解,所以改变它并不难。我脑海中的争论是 DayOfWeek 的列名对其目的非常有意义,所以我真的很想使用它......但它是一个保留字......并且可能会在未来引起痛苦(特别是如果引用该列时,我总是必须在其周围加上方括号)。

大家怎么看?

4

7 回答 7

12

我会改变它 - 我有一个名为 user 的旧表 - 方括号一直都很痛苦。也许称它为 DayOfWeekName 或 DayOFWeekId

乔什

于 2009-06-19T10:52:53.353 回答
5

杰夫,

If you're not too far down the track to rename the column (relatively) painlessly then I'd recommend you change it. You've identified one probable-future-headache for the maintentance crew, and I guess it would be actually less costly (over time) to clean it up now, especially considering that renaming something isn't the hell-on-wheels since the advent of truly effective search and replace functionality in text-editors and IDE's.

The truly hard part of renaming it is gaining the understanding required to do the job safely. You are unique (being the author) in having that understanding. If you asked me (just for instance) to do the job, then it probably wouldn't be a cost effective business proposition.

So... +1 for fixing the sucker yourself... and +2 for not doing it again ;-)

Cheers. Keith.

于 2009-06-19T10:59:38.193 回答
1

I always make sure I never use a keyword as the start of any variable/object/function simply because you're never sure if your target language will like it. It can often generate wacky errors that take a while to track down. Even if syntax checking does pick it up, it means you've wasted more time than if it had been a different name like FurryKitten.

I would avoid DayOfWeek and opt for something which is a completely different, maybe Weekday or DayName. It just saves hassle.

Plus - square brackets just create headaches, and there are a lot of SQL developers out there who don't use the brackets - new developers will end up creating "non-bracketed" code out of habit for some time after they join the team. Uncommon conventions should be avoided if possible.

于 2009-06-19T12:38:06.107 回答
0

如果很容易改变它。但作为一种好的做法,我会在任何地方使用方括号。一旦你习惯了,就不会比在单词之间加空格更痛苦了。

于 2009-06-19T10:54:37.733 回答
0

我们这里有一个名为 User 的表。如果可能的话,我会改变它。尽管 Josh 是正确的,但您可以放置​​方括号来表示它是一个表格,该任务很快就会变老。使用保留字作为表格也使其他开发人员变得困难。如果有人不知道这是保留字,则可能很难确定查询为什么不起作用。

于 2009-06-19T10:54:40.170 回答
0

如果您使用任何数据库适配器/抽象库从数据库中获取数据,请不要担心。类将为您转义列名。如果您自己编写 SQL 查询,则可能会造成一些麻烦。您需要在查询中转义列名。

PS我记得,我有几次像你这样的情况。但我被命名为“订单”列:-)

于 2009-06-19T10:55:24.783 回答
0

我也会避免使用保留字,如果你不注意的话,你很容易陷入困境。

于 2009-06-19T10:55:49.807 回答