DEV Community

Pawan Kukreja
Pawan Kukreja

Posted on

SQL Syntax (Part 01)

Lexical Structure:

A list of instructions makes up a SQL input sequence. A command is made up of a series of tokens, each of which is followed by a semicolon (";"). A command comes to a conclusion when the input stream is finished. Depending on the syntax of the particular command, some tokens are valid.

Key words, identifiers, quoted identifiers, literals (or constants), and special character symbols can all be considered tokens. Whitespace (space, tab, newline) is typically used to separate tokens, however it is not required if there is no ambiguity (which is typically only the case if a special character is close to another token type).

Regarding which tokens identify instructions and which are operands or arguments, the SQL syntax is not particularly consistent. The command name often appears in the first few tokens, thus in the example above, we would typically refer to the commands "SELECT," "UPDATE," and "INSERT." But this kind of INSERT also needs a VALUES to be finished, just as the UPDATE command always needs a SET token to exist in a certain spot. Part VI details each command's explicit syntactic requirements.

Identifiers and Key Words

Key words, or words with a predetermined meaning in the SQL language, include tokens like SELECT, UPDATE, or VALUES in the example above. Tokens like MY_TABLE and A serve as identifiers. Depending on the command they are used in, they indicate names of tables, columns, or other database objects. As a result, they are occasionally only referred to as "names". Since identifiers and key words have the same lexical structure, it is impossible to determine whether a token is an identifier or a key word without first understanding the language. In Appendix C, there is a comprehensive list of keywords.

A letter (a-z, but also letters with diacritical markings and non-Latin characters) or an underscore (_) must come before any SQL identifiers or key words. An identification or key word may contain additional letters, underscores, numerals (0–9), or dollar signs ($). The usage of dollar signs may make applications less portable because they are not permitted in identifiers per the letter of the SQL standard. Identifiers of this type are protected from potential conflict with future additions of the standard since the SQL Standard does not specify a key word that contains numbers or that begins or finishes with an underscore.

Top comments (0)