PostgreSQL naming conventions

后端 未结 2 940
半阙折子戏
半阙折子戏 2020-11-28 02:00

Where can I find a detailed manual about PostgreSQL naming conventions? (table names vs. camel case, sequences, primary keys, constraints, indexes, etc...)

2条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-28 02:34

    There isn't really a formal manual, because there's no single style or standard.

    So long as you understand the rules of identifier naming you can use whatever you like.

    In practice, I find it easier to use lower_case_underscore_separated_identifiers because it isn't necessary to "Double Quote" them everywhere to preserve case, spaces, etc.

    If you wanted to name your tables and functions "@MyAṕṕ! ""betty"" Shard$42" you'd be free to do that, though it'd be pain to type everywhere.

    The main things to understand are:

    • Unless double-quoted, identifiers are case-folded to lower-case, so MyTable, MYTABLE and mytable are all the same thing, but "MYTABLE" and "MyTable" are different;

    • Unless double-quoted:

      SQL identifiers and key words must begin with a letter (a-z, but also letters with diacritical marks and non-Latin letters) or an underscore (_). Subsequent characters in an identifier or key word can be letters, underscores, digits (0-9), or dollar signs ($).

    • You must double-quote keywords if you wish to use them as identifiers.

    In practice I strongly recommend that you do not use keywords as identifiers. At least avoid reserved words. Just because you can name a table "with" doesn't mean you should.

提交回复
热议问题