Friday, October 3, 2008

Using the DEFAULT keyword in INSERT Statements

Did you know you can use the keyword DEFAULT when inserting rows via SQL? This can be handy if your table has a read-only field, an autoinc for example, and you don't want to specify a field list when inserting a row. You can use this:

CREATE TABLE tester ( id autoinc, name char(20) );
INSERT INTO tester VALUES ( DEFAULT, 'whomever' );


instead of having to specify a field list like this:

CREATE TABLE tester ( id autoinc, name char(20) );
INSERT INTO tester ( name ) VALUES ( 'whomever' );




If a column has a default field value, the DEFAULT keyword can be used to specify that default value should be used.

CREATE TABLE tester ( id autoinc, name char(20) DEFAULT 'unknown' );
INSERT INTO tester VALUES ( DEFAULT, DEFAULT );




No comments:

Post a Comment