views:

124

answers:

2

SQLite3 uses dynamic typing rather than static typing, in contrast to other flavors of SQL. The SQLite website reads:

Most SQL database engines (every SQL database engine other than SQLite, as far as we know) uses static, rigid typing. With static typing, the datatype of a value is determined by its container - the particular column in which the value is stored.

SQLite uses a more general dynamic type system. In SQLite, the datatype of a value is associated with the value itself, not with its container.

It seems to me that this is exactly what you don't want, as it lets you store, for example, strings in integer columns.

The page continues:

...the dynamic typing in SQLite allows it to do things which are not possible in traditional rigidly typed databases.

I have two questions:

  1. The use case question: What are some examples where SQLite3's dynamic typing is beneficial?
  2. The historical/design question: What was the motivation for implementing SQLite with dynamic typing?
+1  A: 

If you look at, say, Firefox's "about:config" page, I believe these settings are actually stored in an SQlite database (I'm not 100% sure, though). The benefit of using SQlite's dynamic typing is that each value in the settings can be strong-typed (e.g. the "alerts.totalOpenTime" setting is an integer, while "app.update.channel" is a string) without having to have one separate column per type.

It's basically the same argument as for programming languages, in the end: why have dynamic typing in a programming language over static typing?

Dean Harding
+2  A: 
Mike Cialowicz
> This is called type affinity in SQLite."Type affinity" is a specific aspect of sqlite's dynamic typing---columns have a recommended type, but the actual values are free to be of other types.> According to the SQLite website, they have done this "in order to maximize compatibility between SQLite and other database engines."Yes, they do say that, but it is regarding the type affinity, not the dynamic typing in general.
Bradford Larsen
Regarding the "overhead associated with strong typing": Assuming you in fact mean "static typing": I imagine that using static typing would allow for _more_ efficient implementation than dynamic typing. For instance, storage could be more compact, and many type checks could be avoided when querying.
Bradford Larsen
I apologize for the formatting of the earlier comments: I thought markdown was allowed. I'm new here.
Bradford Larsen