FEATURE REQUEST: use of DEFAULT in INSERTs
Hello sqlite community
I would like to be able to specify the use of DEFAULT values within INSERT statements (on column level):
Here is an example:
CREATE TABLE t1 ( a INTEGER DEFAULT 1 NOT NULL , b INTEGER DEFAULT 2 NOT NULL , c INTEGER DEFAULT 3 NOT NULL );
INSERT INTO t1 (a , b , c ) VALUES (10 , DEFAULT, DEFAULT) , (DEFAULT, 20 , DEFAULT) , (DEFAULT, DEFAULT, 30 ) ;
would be working with this feature.
I know that 3 different INSERT statements would work:
INSERT INTO t1 (a) VALUES (10); INSERT INTO t1 (b) VALUES (20); INSERT INTO t1 (C) VALUES (30);
or as well this statement:
INSERT INTO t1 DEFAULT VALUES;
I found this thread here, which explains more. (but is wrong in some parts)
Is there a chance to include this feature?
Thanks a lot!
(2) By Gunter Hick (gunter_hick) on 2020-11-23 11:57:26 in reply to 1 [link] [source]
Naming a column in the INSERT statement is a promise that you will provide a value and a request that SQL provide default values for the columns not mentioned; or an error message stating that such defaults are missing in the table declaration. You cannot just change your mind.
I agree with you. I see it less about changing my mind, more about having another option.
Using the word DEFAULT would allow me to specify the column for the insert, but choosing the default value from the table definition.
Mention col and a value -> use it
Mention col and DEFAULT OR not Mentioning col -> use the default from the CREATE TABLE definition
It helps to write bigger INSERTS, instead of several INSERTS.
In the example it seems silly, but that's due to its simplicity.
Thank you very much!
Do you mean perhaps to define the table as follows:
CREATE TABLE t1 ( a INTEGER NOT NULL ON CONFLICT REPLACE DEFAULT 1, b INTEGER NOT NULL ON CONFLICT REPLACE DEFAULT 2, c INTEGER NOT NULL ON CONFLICT REPLACE DEFAULT 3 );
so that if you try to insert a NULL value you get a default instead?
sqlite> CREATE TABLE t1 ...> ( ...> a INTEGER NOT NULL ON CONFLICT REPLACE DEFAULT 1, ...> b INTEGER NOT NULL ON CONFLICT REPLACE DEFAULT 2, ...> c INTEGER NOT NULL ON CONFLICT REPLACE DEFAULT 3 ...> ); sqlite> insert into t1 values (58,null,null); sqlite> insert into t1 values (null,36,null); sqlite> insert into t1 values (null,38,1748); sqlite> select * from t1; ┌────┬────┬──────┐ │ a │ b │ c │ ├────┼────┼──────┤ │ 58 │ 2 │ 3 │ │ 1 │ 36 │ 3 │ │ 1 │ 38 │ 1748 │ └────┴────┴──────┘ sqlite>
Exactly, this allows to write one INSERT statement and change the DEFAULTs with NULLs:
INSERT INTO t1 (a , b , c ) VALUES (10 , NULL , NULL) , (NULL , 20 , NULL) , (NULL , NULL , 30 ) ;
I didn't know about the "NOT NULL ON CONFLICT REPLACE" part. Thank you very much!
I see that all of PostgreSQL, MySQL, SQL Server, and Oracle support this. So SQLite probably should too.
However, due to technical reasons, this is a substantial change to SQLite. It would introduce unnecessary risk to add such an enhancement this close to a scheduled release. Maybe something can be done during the next release cycle.