Identifier Names

Stai visualizzando una vecchia versione di questo article. Visualizza la versione più recente.

I nomi dei datavase, delle tabelle, degli indici, delle colonne, degli alias, delle viste, delle Stored Procedure, delle partizioni, dei tablespace, sono tutti noti come identificatori e devono seguire certe regole.

Gli identificatori possono essere virgolettati con il carattere backtick - #`#. Virgolettarli è opzionale se l'identificatore non contiene caratteri speciali e non è una parola riservata.

Da non virgolettare

I caratteri validi sono i seguenti, e gli identificatori composti solo da questi non hanno bisogno di essere virgolettati:

  • ASCII: [0-9,a-z,A-Z$_] (cifre 0-9,caratteri latini di base, sia maiuscoli che minuscoli, segno del dollaro, underscore)
  • Estesi: U+0080 .. U+FFFF

Da virgolettare

I seguenti caratteri sono validi, ma gli identificatori che li usano devono essere virgolettati:

  • ASCII: U+0001 .. U+007F (Unicode Basic Multilingual Plane (BMP)completo, escluso U+0000)
  • Estesi: U+0080 .. U+FFFF
  • Le virgolette possono essere usate come parte dell'identificatore, purché siano anch'esse virgolettate.

Altre regole

Vi sono altre regole:

  • Gli identificatori vengono registrati in Unicode (UTF-8)
  • Potrebbero non essere case-sensitive. Si veda Indentifier Case-sensitivity.
  • I nomi dei database, delle tabelle e delle colonne non possono terminare con un carattere di spazio
  • Gli identificatori possono iniziale con una cifra, ma non possono contenere cifre a meno che non siano virgolettati.
  • Gli identificatori non possono contenere il carattere ASCII NUL (U+0000) e caratteri supplementari (U+10000 e più alti).
  • I nomi come 5e6, 9e non sono proibili, ma si raccomanda caldamente di non usarli, perché possono introdurre ambiguità in alcuni contesti, perché potrebbero essere trattati come numeri o espressioni.
  • Le variabili utente non possono essere utilizzate come parti di un identificatore, né come identificatore completo, in una istruzione SQL.

Carattere per le virgolette

Normalmente il carattere per virgolettare gli identificatori è il backtick - #`#, ma se l'opzione ANSI_QUOTES dell'SQL_MODE è attiva, è anche possibile usare le virgolette doppie - #"#..

Maximum Length

  • Databases, tables, columns, indexes, constraints, stored routines, triggers, events, views, tablespaces, servers and log file groups have an maximum length of 64 characters.
  • Compound statement labels have a maximum length of 16 characters
  • Aliases have a maximum length of 256 characters, except for column aliases in CREATE VIEW statements, which adhere to the are checked against the maximum column length of 64 characters (not the maximum alias length of 256 characters).
  • Multi-byte characters do not count extra towards towards the character limit.

Multiple identifiers

MariaDB allows the column name to be used on its own if the reference will be unambiguous, or the table name to be used with the column name, or all three of the database, table and column names. A period is used to separate the identifiers, and the period can be surrounded by spaces.

Examples

Using the period to separate identifiers:

MariaDB [test]> CREATE TABLE t1 (i int);

INSERT INTO t1(i) VALUES (10);

SELECT i FROM t1;
+------+
| i    |
+------+
|   10 |
+------+

SELECT t1.i FROM t1;
+------+
| i    |
+------+
|   10 |
+------+

SELECT test.t1.i FROM t1;
+------+
| i    |
+------+
|   10 |
+------+

The period can be separated by spaces:

SELECT test . t1 . i FROM t1;
+------+
| i    |
+------+
|   10 |
+------+

Resolving ambiguity:

CREATE TABLE t2 (i int);

SELECT i FROM t1 LEFT JOIN t2 ON t1.i=t2.i;
ERROR 1052 (23000): Column 'i' in field list is ambiguous

SELECT t1.i FROM t1 LEFT JOIN t2 ON t1.i=t2.i;
+------+
| i    |
+------+
|   10 |
+------+

Creating a table with characters that require quoting:

CREATE TABLE 123% (i int);
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '123% (i int)' at line 1

CREATE TABLE `123%` (i int);
Query OK, 0 rows affected (0.85 sec)

CREATE TABLE `TABLE` (i int);
Query OK, 0 rows affected (0.36 sec)

Using double quotes as a quoting character:

CREATE TABLE "SELECT" (i int);
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '"SELECT" (i int)' at line 1

SET sql_mode='ANSI_QUOTES';
Query OK, 0 rows affected (0.03 sec)

CREATE TABLE "SELECT" (i int);
Query OK, 0 rows affected (0.46 sec)

Using an identifier quote as part of an identifier name:

SHOW VARIABLES LIKE 'sql_mode';
+---------------+-------------+
| Variable_name | Value       |
+---------------+-------------+
| sql_mode      | ANSI_QUOTES |
+---------------+-------------+

CREATE TABLE "fg`d" (i int);
Query OK, 0 rows affected (0.34 sec)

Commenti

Sto caricando i commenti......
Content reproduced on this site is the property of its respective owners, and this content is not reviewed in advance by MariaDB. The views, information and opinions expressed by this content do not necessarily represent those of MariaDB or any other party.