Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do you add a Column with a Case as the default in SQLite?

ALTER TABLE [table_name] ADD COLUMN alloc_strat varchar(25) NOT NULL 
 DEFAULT(CASE
    WHEN (shelf_life_unit = 'Months') THEN 'Min Remaining Shelf Life'
    ELSE 'FIFO')
;

This isn’t working, Is there a way to do this in one sql statement

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

In SQLite the added columns are not allowed to have expressions are their default.

The following illustrates the syntax of ALTER TABLE ADD COLUMN
statement:

ALTER TABLE table_name ADD COLUMN column_definition; Code language:
SQL (Structured Query Language) (sql) There are some restrictions on
the new column:

The new column cannot have a UNIQUE or PRIMARY KEY constraint. If the
new column has a NOT NULL constraint, you must specify a default value
for the column other than a NULL value. The new column cannot have a
default of CURRENT_TIMESTAMP, CURRENT_DATE, and CURRENT_TIME, or an
expression
. If the new column is a foreign key and the foreign key
constraint check is enabled, the new column must accept a default
value NULL.

Source

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading