Can I create computed columns in SQLite?

Ej. picture Ej. · Jul 14, 2009 · Viewed 19.8k times · Source

What would be the syntax (if it's possible), for example, to create a table called Car_Model that has a foreign key to a table Car_Make, and give Car_Make a column which is the number of Car_Models that exist of that Car_Make.

(If this seems trivial or homework-like it's because I am just playing with some python at home trying to recreate a problem I was having at work. We use MS-SQL at work.)

Answer

Diaa Sami picture Diaa Sami · Jul 14, 2009

Update: As of version 3.31.0 (released on 2020-01-22), SQLite supports computed columns so the answer below applies to versions prior to 3.31.0

SQLite doesn't supported computed columns.

However your problem can be solved with a relatively simple SQL Query, you can then create a view to make it appear like a table with the extra computed columns.

SELECT Car_Make.Name, Count(*) AS Count 
FROM Car_Make, Car_Model 
WHERE Car_Make.Id = Car_Model.Make 
GROUP BY Car_Make.Name

This should return a table similar to the following

Name      Count
----      -----
Nissan    5
Toyota    20
Ford      10