Is there any rule for the SQLite's column name?
Blanks spaces are restricted in the naming convention of the database object's name and column name of the table.
The at sign, dollar sign ($), number sign, or underscore.
Only the RENAME TABLE, ADD COLUMN, RENAME COLUMN, and DROP COLUMN variants of the ALTER TABLE command are supported. Other kinds of ALTER TABLE operations such as ALTER COLUMN, ADD CONSTRAINT, and so forth are omitted. FOR EACH ROW triggers are supported but not FOR EACH STATEMENT triggers.
Sqlite column names are case-insensitive, according to this.
If you’re using the SQLite with the version lower than 3.25.0 and could not upgrade, then you should follow these steps to rename a column: First, start a transaction. Second, create a new table whose structure is the same as the original one except for the column that you want to rename. Third, copy data from the original table to the new table.
{X.n = 0;} Except for placing "illegal" identifier names between double quotes "identifier#1", [ before and ] after works as well [identifire#2]. Valid field names are subject to the same rules as valid Table names. Checked this with SQlite administrator. The field name must begin with an alpha character or underline
Column Name Limitations. Column names can contain any valid characters (for example, spaces). If column names contain any characters except letters, numbers, and underscores, the name must be delimited by enclosing it in back quotes (`).
character is not legal in an ODBC name, even when the name is enclosed in back quotes. All other valid Microsoft Excel characters (except the pipe character (|)) can be used in a column name, including spaces. A delimited identifier must be used for a Microsoft Excel column name to include a space.
Can it have characters like '/'?
All examples are from SQlite 3.5.9 running on Linux.
If you surround the column name in double quotes, you can:
> CREATE TABLE test_forward ( /test_column INTEGER ); SQL error: near "/": syntax error > CREATE TABLE test_forward ("/test_column" INTEGER ); > INSERT INTO test_forward("/test_column") VALUES (1); > SELECT test_forward."/test_column" from test_forward; 1
That said, you probably shouldn't do this.
The following answer is based on the SQLite source code, mostly relying on the file parse.y
(input for the lemon parser).
The allowed series of characters for column and table names in CREATE TABLE
statements are
'
-escaped strings of any kind (even keywords)"
-escaped strings of any kind (even keywords)MSB=1
8-bit ASCII characters or 7-bit ASCII characters with 1
in the following table that doesn't form a keyword: INDEXED
because it's non-standardJOIN
for reason that is unknown to me.The allowed series of characters for result columns in a SELECT
statement are
AS
let's look at the syntax for CREATE TABLE
columns
// The name of a column or table can be any of the following: // %type nm {Token} nm(A) ::= id(X). {A = X;} nm(A) ::= STRING(X). {A = X;} nm(A) ::= JOIN_KW(X). {A = X;}
digging deeper, we find out that
// An IDENTIFIER can be a generic identifier, or one of several // keywords. Any non-standard keyword can also be an identifier. // %type id {Token} id(A) ::= ID(X). {A = X;} id(A) ::= INDEXED(X). {A = X;}
"Generic identifier" sounds unfamiliar. A quick look into tokenize.c
however brings forth the definition
/* ** The sqlite3KeywordCode function looks up an identifier to determine if ** it is a keyword. If it is a keyword, the token code of that keyword is ** returned. If the input is not a keyword, TK_ID is returned. */ /* ** If X is a character that can be used in an identifier then ** IdChar(X) will be true. Otherwise it is false. ** ** For ASCII, any character with the high-order bit set is ** allowed in an identifier. For 7-bit characters, ** sqlite3IsIdChar[X] must be 1. ** ** Ticket #1066. the SQL standard does not allow '$' in the ** middle of identfiers. But many SQL implementations do. ** SQLite will allow '$' in identifiers for compatibility. ** But the feature is undocumented. */
For a full map of identifier characters, please consult the tokenize.c
.
It is still unclear what are the available names for a result-column
(i. e. the column name or alias assigned in the SELECT
statement). parse.y
is again helpful here.
// An option "AS <id>" phrase that can follow one of the expressions that // define the result set, or one of the tables in the FROM clause. // %type as {Token} as(X) ::= AS nm(Y). {X = Y;} as(X) ::= ids(Y). {X = Y;} as(X) ::= . {X.n = 0;}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With