Can I use anything other than BIGINT as a primary key data type in SQLite?

I am very excited about the possibility of using SQLite as a database solution during development, so I can focus on writing code first and use NHibernate’s ShemaExport feature to dynamically generate db at runtime However, I have encountered some problems, the most important of which is that it seems that SQLite requires me to use Int64 as my primary key (such as Int32 or Guid). Is there a way to solve it?

Note: I should specify that this is in the context of the application using NHibernate. Strictly speaking, you cannot create a table with the INT data type in SQLite, but the behavior when saving and retrieving data It seems to indicate that it is stored and/or retrieved as an Int64.

SQLite will allow you to add Any field of is used as a PRIMARY KEY. Doing so will implicitly create a UNIQUE index on the field. This is the field that you as a developer can think of as the primary unique identifier of the field. It can be any supported SQLite data type (Shown below).

SQLite will always create an implicit internal numeric identifier for each table. It will have several aliases, including RowID, OID, and _ROWID_. If you will The primary key is created as INTEGER PRIMARY KEY, then it will use the same fields as the primary key and SQLite’s internal numeric identifier.

SQLite does not have the concept of Int32 or Int64 or Guid data types. It has only four data types: INT, REAL, TEXT and BLOB. When you run DDL against SQLite, if you use anything other than these four identifiers, SQLite will use a set of rules to determine the type to use. Basically, Int32 and Int64 It is regarded as an alias of INT, and the same operation is completed in the end.

Even if you create a table with the data type mentioned for each field, all you set is the type affinity of the field. SQLite does not enforce Implement data types. Regardless of the declared type, any data can be put into any field. If possible, SQLite will use type affinity to convert the data, so if you insert “123” as a text string into an INT field, it will Stored as the number 123.

The only exception to type affinity is INTEGER PRIMARY KEY FIELDS. Those must be integers.

Integers in SQLite are always stored with variable length fields. Therefore, according to The size of the integer, may actually return Int32 for some rows and Int64 for other rows, all of these rows are in the same field. It depends on the wrapper you are using, in this case NHibernate (I want to use System.Data.SQLite).

I am excited about the possibility of using SQLite as a database solution during development, so I can focus on First write the code and make Use NHibernate’s ShemaExport function to dynamically generate db at runtime. However, I have encountered some problems, the most important of which is that it seems that SQLite requires me to use Int64 as my primary key (such as Int32 or Guid). Is there a solution?

Note: I should specify that this is in the context of the application using NHibernate. Strictly speaking, you cannot create a table with the INT data type in SQLite, but the behavior when saving and retrieving data It seems to indicate that it is stored and/or retrieved as an Int64.

SQLite will allow you to use any field in the table as a PRIMARY KEY. Doing so will A UNIQUE index is implicitly created on the field. This is the field that you as a developer can think of as the main unique identifier of the field. It can be any supported SQLite data type (as shown below).

SQLite will always create an implicit internal numeric identifier for each table. It will have several aliases, including RowID, OID, and _ROWID_. If you create the primary key as an INTEGER PRIMARY KEY, then it will use the same The field with the same primary key and the internal numeric identifier of SQLite.

SQLite does not have the concept of Int32 or Int64 or Guid data type. It has only four data types: INT, REAL, TEXT and BLOB. When you are interested in SQLite When running DDL, if you use anything other than these four identifiers, SQLite will use a set of rules to determine the type to use. Basically, Int32 and Int64 are treated as aliases for INT, and the same thing is done in the end .

Even if you create a table with the data type mentioned for each field, all you set is the type affinity of the field. SQLite does not enforce the data type. Regardless of the declared type, any data It can be put in any field. If possible, SQLite will use type affinity to convert the data, so if you insert “123” as a text string into the INT field, it will store it as the number 123.

The only exception to type affinity is INTEGER PRIMARY KEY FIELDS. Those must be integers.

Integers in SQLite are always stored with variable length fields. Therefore, depending on the size of the integer, it may actually be some rows Int32 is returned, and Int64 is returned for other rows, all of which are in the same field. It depends on the wrapper you are using, in this case NHibernate (I want to use System.Data.SQLite). < /p>

Leave a Comment

Your email address will not be published.