Add a Generated Column to a Table in SQLite

You can add a generated column to an existing table in SQLite by using the ALTER TABLE statement.

SQLite’s implementation of the ALTER TABLE statement is very limited, but it does allow you to add a column – including generated columns.

Generated columns (also known as “computed columns”) are columns that get their value from an expression that computes values from other columns.

Continue reading

How to Create a Computed Column in SQLite

Generated column support was added to SQLite in version 3.31.0, which was released on 22 January 2020.

Generated columns and computed columns are the same thing. They are columns whose values are a function of other columns in the same row.

In SQLite, generated columns are created using the GENERATED ALWAYS column-constraint when creating or altering the table.

There are two types of generated column; STORED and VIRTUAL. Only VIRTUAL columns can be added when altering a table. Both types can be added when creating a table.

Continue reading

Convert NULL Values to the Column’s Default Value when Inserting Data in SQLite

One of SQLite‘s non-standard extensions to SQL is the ON CONFLICT clause.

This clause allows you to determine what should happen when certain conflicts occur due to a constraint violation.

One of the things you can use this clause for is to replace NULL values with a column’s default value when inserting or updating data in a table.

By default, if you try to explicitly insert NULL into a column with a NOT NULL constraint, it will fail.

And if you try to explicitly insert NULL into a column without a NOT NULL constraint, then NULL will be assigned to that column, even if there’s a DEFAULT clause.

However, you can use the ON CONFLICT clause to set it to the default value instead of NULL.

Continue reading

How ON CONFLICT Works in SQLite

SQLite has the ON CONFLICT clause that allows you to specify how to handle constraint conflicts. It applies to UNIQUE, NOT NULL, CHECK, and PRIMARY KEY constraints (but not FOREIGN KEY constraints).

There are five possible options you can use with this clause:

  • ABORT
  • FAIL
  • IGNORE
  • REPLACE
  • ROLLBACK

This article provides examples and an explanation of each of these options.

The ON CONFLICT clause is used in CREATE TABLE statements, but it can also be used when inserting or updating data by replacing ON CONFLICT with OR.

Continue reading

How to Skip Rows that Violate Constraints When Inserting Data in SQLite

In SQLite, when you try to insert multiple rows into a table, and any of those rows violates a constraint on that table, the operation will fail.

This is to be expected, after all, that’s what the constraint is for.

But what if you just want to ignore any rows that violate constraints? In other words, if a row violates a constraint, you want SQLite to skip that row, then carry on processing the next row, and so on.

Fortunately, there’s an easy way to do this in SQLite.

Continue reading