lundi 27 avril 2015

Setting default value using data from another table

When creating a table in SQLite, I want to define the default value of a column using value from another table (as this column is a foreign key). Something like this:

CREATE TABLE Test_1(A1 INTEGER PRIMARY KEY, B1 TEXT NOT NULL);
CREATE TABLE Test_2(A2 INTEGER PRIMARY KEY, B2 INTEGER NOT NULL DEFAULT [a value in column A1 of Test_1]);

I can of course pick a value from Test_1 and hard code it as the default value for B2, but that is error prone if I change the data of Test_1 later. At the same time, we cannot use SELECT statement inside a CREATE.

If this is not feasible in SQLite, is it possible for other SQL engines? Or more generally, is this something that should be enforced by application logic rather than by database design?

Aucun commentaire:

Enregistrer un commentaire