您是否根据保留字列表检查字段名称和表名称?

发布于 2024-07-05 10:49:59 字数 753 浏览 6 评论 0原文

有时,我的字段、表、视图顺序存储过程名称会出现问题。 示例:

   SELECT from, to, rate FROM Table1

问题是 from 是 SQL-92 中的保留字。 您可以将字段名放在双引号中来解决此问题,但是如果其他一些数据库工具想要读取您的数据库怎么办? 这是您的数据库设计,如果其他应用程序对您的数据库有问题,那是您的错。

还有许多其他保留字( ~300),我们应该避免所有这些。 如果您将 DBMS 从制造商 A 更改为 B,您的应用程序可能会失败,因为某些字段名称现在是保留字。 名为 PERCENT 的字段可能适用于 Oracle 数据库,但在 MS SQL Server 上,它必须被视为保留字。

我有一个工具可以根据这些保留字检查我的数据库设计; 你也是?

这是我的规则,

  1. 不要使用超过 32 个字符的名称(某些 DBMS 无法处理更长的名称),
  2. 仅使用 az、AZ、0-9 和下划线 (:-;,/&!=?+- )允许)
  3. 不要以数字开头名称
  4. 避免这些保留字

I've sometimes had a problem with my field-, table-, view- oder stored procedure names.
Example:

   SELECT from, to, rate FROM Table1

The Problem is that from is a reserved word in SQL-92.
You could put the fieldname in double quotes to fix this, but what if some other db tools wants to read your database?
It is your database design and it is your fault if other applications have problems with your db.

There are many other reserved words (~300) and we should avoid all of them.
If you change the DBMS from manufacturer A to B, your application can fail, because a some fieldnames are now reserved words.
A field called PERCENT may work for a oracle db, but on a MS SQL Server it must be treated as a reserved word.

I have a tool to check my database design against these reserved words ; you too?

Here are my rules

  1. don't use names longer than 32 chars (some DBMS can't handle longer names)
  2. use only a-z, A-Z, 0-9 and the underscore (:-;,/&!=?+- are not allowed)
  3. don't start a name with a digit
  4. avoid these reserved words

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

聽兲甴掵 2024-07-12 10:49:59

您不应该使用保留字作为表中的列名,即使您可以引用它们。

引用它们会使代码变得非常尴尬,因为您必须始终在代码中的 SQL 语句中转义引号字符。 在我看来,它还使 SQL 命令行成为真正的 PITA。

最后只是看起来很乱。 最好花时间想出一个不与 SQL 关键字冲突的不同单词。

你的规则对我来说很好。

You shouldn't use reserved words as column names in a table, even if you can quote them away.

Quoting them can make code really awkward as you have to escape the quote character all the time in your SQL statements within your code. It also makes the SQL command line a real PITA, in my opinion.

In the end it just looks messy. Far better to spend the time to think up of a different word that doesn't clash with SQL keywords.

Your rules look fine to me.

逐鹿 2024-07-12 10:49:59

简单的方法:只需确保每个字段名称都被引用即可。

编辑:任何有价值的明智的数据库工具都应该做同样的事情,我当然从未遇到过任何问题(至少在我自己的代码之外!)

Easy way: just make sure every field name is quoted.

Edit: Any sensible DB tool worth its salt should be doing the same thing, I have certainly never encountered any problems (outside of my own code, at least!)

脱离于你 2024-07-12 10:49:59

我同意 Yarik 关于保留字适用性的第二点。 在 OP 示例中,他使用“to”、“from”和“rate”。 我脑海中最直接的问题(因此也可能是未来开发人员的问题)是“往来于何处?” 也许考虑将这些列重命名为“EffectiveFromDate”和“EffectiveUntilDate”(如果这就是它们所代表的含义)。

I agree with Yarik's 2nd point about the suitability of reserved words. In the OPs example, he uses "to", "from" and "rate". The immediate question in my mind, and therefore possibly in that of a future developer is "To and from what?" Maybe consider renaming these columns to "EffectiveFromDate" and "EffectiveUntilDate", if that's what they represent.

</2c>

你げ笑在眉眼 2024-07-12 10:49:59

只要避免保留字即可。

请注意,大多数数据库(和数据库链接层)都有一种以编程方式列出所有保留字的方法。 您可以将其用作应用程序启动时的健全性检查,以确保您没有误入歧途。

引用确实有效,因此为了安全起见,您可以这样做。 然而,对于 DBA 和针对您的应用程序制作自定义报告的人来说,这让生活变得非常尴尬,因此这应该仅用作创可贴。

Just avoid reserved words.

Note that most databases (and database link-layers) have a way of programmatically listing all reserved words. You can use that as a sanity-check on application startup to ensure you haven't run astray.

Quoting does work, so you could do that for safety. However this makes life really awkward for DBAs and people making custom reports against your app, so that should be used as a band-aid only.

恰似旧人归 2024-07-12 10:49:59

抛开名称和保留字之间明显的混淆不谈,我认为至少有两个非常充分的理由避免使用保留字作为名称:

  1. 您不必使用引号(或 MS 世界中的方括号),这会严重损害可读性.

    注意:当您发现自己需要从 SQL(所谓的“动态 SQL”方法)或其他语言生成 SQL 代码时,可读性可能会受到特别损害。 您不希望单引号内有额外的双引号,或额外的重复双引号,或转义引号,或任何其他类似的模糊内容。

    例如,您想要这样的片段吗:

    <前><代码>-- SQL --------------------------
    将 @sql 声明为 varchar(4000)
    set @sql = '从 MyTable 中选择“至”、“来自”'

    'VB-------------------------
    将 sql 变暗为字符串
    sql = "从 MyTable 中选择""到""、""从"""

    // C++ ----------------------
    String sql = "从 MyTable 选择 \"To\", \"From\""

  2. 无论如何,大多数保留字都不适合命名表、列、变量等。 在绝大多数情况下,名词(有时是形容词)作为名称比动词、副词和介词好得多。 :-)

Putting aside obvious confusions between names and reserved words, I think there are at least two very strong reasons to avoid using reserved words as names:

  1. You would not have to use quotes (or square braces in MS world) that substantially hurt readability.

    NB: Readability may be especially damaged when you find yourself in need to generate SQL code from SQL (so-called "dynamic SQL" approach) or from other languages. You do not want extra double quotes inside single quotes, or extra repeated double quotes, or escaped quotes, or any other obscure stuff like that.

    For example, how would you like snippets like these:

    -- SQL -----------------------
    declare @sql as varchar(4000)
    set @sql = 'select "To", "From" from MyTable'
    
    ' VB -------------------------
    Dim sql as String
    sql = "select ""To"", ""From"" from MyTable"
    
    // C++ -----------------------
    String sql = "select \"To\", \"From\" from MyTable"
    
  2. Most of the reserved words are bad candidates for naming tables, columns, variables, etc. anyway. In the vast majority of cases nouns (sometimes adjectives) are much, much better for names than verbs, adverbs, and prepositions. :-)

鸠魁 2024-07-12 10:49:59

确实。 我有一个用于此目的的 SQL_RESERVED_WORDS 表。

顺便说一句,Oracle 只能处理 30 个字符的表名。 而且它们都是大写的。

只需要一个小时的不必要的调试就可以收回成本。

Definitely. I have a SQL_RESERVED_WORDS table for that very purpose.

Oracle can only handle 30 character table names BTW. And they're all upper case.

It only takes an hour of so of unnecessary debugging before the table pays for itself.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文