Home >Database >Mysql Tutorial >How Do PostgreSQL Table and Column Naming Conventions Avoid Parsing Ambiguity?
Table and Column Names: Naming Conventions in PostgreSQL
In PostgreSQL, table and column names are subject to specific naming conventions. One such convention is that these names cannot start with numeric characters. This restriction is defined by the SQL-92 standard (accessible at http://en.wikipedia.org/wiki/SQL-92), which specifies that identifier starts should be simple Latin letters.
While this naming convention may differ from common practices in programming, there are several reasons for this design choice.
Identifier Start Characteristics
In SQL, identifiers (including table and column names) are defined as strings of up to 63 characters. These strings must begin with Latin letters or underscores, with subsequent characters being letters, digits, or underscores.
Syntax Ambiguity
If table and column names were allowed to begin with numeric characters, there would be potential for ambiguity during parsing. Consider the following examples:
SELECT 2e2 3.4 FROM ...
In this query, the parser would be uncertain whether 2e2 and 3.4 represent column names or numerical expressions. When identifiers start with letters, the ambiguity is eliminated, and the parser can quickly identify the expressions correctly.
Design Consistency
By enforcing the convention that identifiers start with letters, the SQL standard ensures consistency in parsing and execution. If numeric leading characters were allowed, additional rules would be required to differentiate between identifiers and numerical expressions, potentially leading to complex parsing algorithms and reduced readability.
Double-Quoted Names
While numeric leading characters are not allowed by default, they can be used by enclosing the name in double quotes. This creates a quoted identifier that overrides the standard naming convention. For example, the table name 15909434_user can be created using double quotes:
CREATE TABLE "15909434_user" ( ... )
Using double quotes provides flexibility and allows for the creation of names that do not adhere to the standard convention. However, it is important to note that quoted identifiers are not as legible and may be more prone to errors.
The above is the detailed content of How Do PostgreSQL Table and Column Naming Conventions Avoid Parsing Ambiguity?. For more information, please follow other related articles on the PHP Chinese website!