Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I create computed columns in SQLite?

Tags:

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

like image 982
Ej. Avatar asked Jul 14 '09 11:07

Ej.


People also ask

How do you create a computed column?

We can also create a computed column using the Object explorer window. Go to your database, right click on tables, select “New Table” option. Create all columns that you require and to mark any column as computed, select that column and go to column Properties window and write your formula for computed column.

Are there any disadvantages of using computed column?

Some Limitations. You can not reference columns from other tables for a computed column expression directly. You can not apply insert or update statements on computed columns.

What is a computed column?

A computed column is a virtual column that is not physically stored in the table, unless the column is marked PERSISTED. A computed column expression can use data from other columns to calculate a value for the column to which it belongs.

How do you calculate mode in SQLite?

To compute the mode, group by the browser column, get the COUNT(*) for each group, sort by that value, and take the record with the largest value. CL. Save this answer.


1 Answers

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 
like image 115
Diaa Sami Avatar answered Sep 25 '22 14:09

Diaa Sami