PostgreSQL 命名约定 [英] PostgreSQL naming conventions

查看:46
本文介绍了PostgreSQL 命名约定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在哪里可以找到有关 PostgreSQL 命名约定的详细手册?(表名与驼峰式大小写、序列、主键、约束、索引等...)

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

推荐答案

关于表名、大小写等,普遍的约定是:

Regarding tables names, case, etc, the prevalent convention is:

  • SQL 关键字:大写
  • 标识符(数据库、表、列等的名称):lower_case_with_underscores

例如:

UPDATE my_table SET name = 5;

这不是一成不变的,但强烈推荐关于小写标识符的部分,IMO.Postgresql 在不引用时不区分大小写(它实际上在内部将它们折叠为小写),并在引用时区分大小写;很多人都没有意识到这种特质.始终使用小写字母是安全的.无论如何,使用 camelCasePascalCase(或 UPPER_CASE)是可以接受的,只要你是一致的:要么引用标识符总是或从不(和这包括模式创建!).

This is not written in stone, but the bit about identifiers in lower case is highly recommended, IMO. Postgresql treats identifiers case insensitively when not quoted (it actually folds them to lowercase internally), and case sensitively when quoted; many people are not aware of this idiosyncrasy. Using always lowercase you are safe. Anyway, it's acceptable to use camelCase or PascalCase (or UPPER_CASE), as long as you are consistent: either quote identifiers always or never (and this includes the schema creation!).

我不知道更多的约定或风格指南.代理键通常由一个序列组成(通常使用 serial 宏),如果您手动创建这些序列(tablename_colname_seq),那么坚持这些序列的命名会很方便.

I am not aware of many more conventions or style guides. Surrogate keys are normally made from a sequence (usually with the serial macro), it would be convenient to stick to that naming for those sequences if you create them by hand (tablename_colname_seq).

另见一些讨论 此处此处 和(对于一般 SQL)here,都有几个相关链接.

See also some discussion here, here and (for general SQL) here, all with several related links.

注意:Postgresql 10 引入了identity 列作为 serial 的符合 SQL 的替代品.

Note: Postgresql 10 introduced identity columns as an SQL-compliant replacement for serial.

这篇关于PostgreSQL 命名约定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆