Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What SQLite column name can be/cannot be?

Is there any rule for the SQLite's column name?

  • Can it have characters like '/'?
  • Can it be UTF-8?
like image 843
prosseek Avatar asked Jul 30 '10 15:07

prosseek


People also ask

Can SQLite column names have spaces?

Blanks spaces are restricted in the naming convention of the database object's name and column name of the table.

Which special characters are allowed in column names?

The at sign, dollar sign ($), number sign, or underscore.

Which is not supported by SQLite?

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.

Are column names case sensitive in SQLite?

Sqlite column names are case-insensitive, according to this.

How to rename a column in SQLite?

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.

What are the rules for field names in SQLite?

{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

What are the limitations of column names in SQL?

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 (`).

What characters are not allowed in an ODBC name?

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.


2 Answers

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.

like image 100
J. Polfer Avatar answered Sep 29 '22 08:09

J. Polfer


The following answer is based on the SQLite source code, mostly relying on the file parse.y (input for the lemon parser).

TL;DR:

The allowed series of characters for column and table names in CREATE TABLE statements are

  • '-escaped strings of any kind (even keywords)
  • Identifiers, which means
    • ``` and "-escaped strings of any kind (even keywords)
    • a series of the MSB=1 8-bit ASCII characters or 7-bit ASCII characters with 1 in the following table that doesn't form a keyword: Valid identifier characters
  • Keyword INDEXED because it's non-standard
  • Keyword JOIN for reason that is unknown to me.

The allowed series of characters for result columns in a SELECT statement are

  • Either a string or an identifier as described above
  • All of the above if used as a column alias written after AS

Now to the exploration process itself

  1. 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;} 
  2. 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.

  3. 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;} 
like image 27
user35443 Avatar answered Sep 29 '22 07:09

user35443