Is there UID datatype in SQLITE if Yes then how to generate value for that

Pranita Patil picture Pranita Patil · Apr 11, 2012 · Viewed 35.3k times · Source

I am creating table like this:

CREATE TABLE foobar (id uniqueidentifier, foo text, bar text, PRIMARY  KEY (id))

How to insert or generate value for id field in table foobar?

Answer

Mike Sherrill 'Cat Recall' picture Mike Sherrill 'Cat Recall' · Apr 11, 2012

You can argue that SQLite doesn't support data types at all. In SQLite3, you can do this, for example.

sqlite> create table test (id wibblewibble primary key);

SQLite will happily create a column with the "data type" wibblewibble. SQLite will also happily create columns with the "data types" uuid, guid, and SuperChicken.

The crucial point for you is probably how to automatically generate a uid. SQLite can't help you much there.

You can leave it entirely up to the client program. If you're programming in python, use the uuid module. In ruby, you have the SecureRandom.uuid function. Other languages have similar features or workarounds.

You can write your own uid-generating function in C. (See Create or Redefine SQL Functions.) I'd call this a relatively extreme approach.

You can store it in either binary or text format.


Other conversations online suggest that there's a widespread misunderstanding about what a UUID is. A UUID is not simply a 128-bit random number. A UUID has structure and rules. See RFC 4122.