How to insert default values in SQL table?
Solution 1:
Best practice it to list your columns so you're independent of table changes (new column or column order etc)
insert into table1 (field1, field3) values (5,10)
However, if you don't want to do this, use the DEFAULT
keyword
insert into table1 values (5, DEFAULT, 10, DEFAULT)
Solution 2:
Just don't include the columns that you want to use the default value for in your insert statement. For instance:
INSERT INTO table1 (field1, field3) VALUES (5, 10);
...will take the default values for field2
and field4
, and assign 5 to field1
and 10 to field3
.
Solution 3:
This works if all the columns have associated defaults and one does not want to specify the column names:
insert into your_table
default values
Solution 4:
Try it like this
INSERT INTO table1 (field1, field3) VALUES (5,10)
Then field2 and field4 should have default values.