MakeProps
MakeProps

Reputation: 183

INSERT INTO with default values for a single column

I have a problem to insert the data into 1 table with 1 column

Name: user_id

Column: id

I am trying to add 1 line in this column with this query:

INSERT INTO user_id (id) VALUES ()

The problem is the above is invalid, I want the id take the last value id +1

This is not a syntax problem because this query works:

INSERT INTO user_id (id) VALUES (4)

So, I do not really know how to solve this problem.

Upvotes: 3

Views: 11387

Answers (2)

user330315
user330315

Reputation:

Assuming the id column is defined as serial or identity you can specify a column list and set the column value to default:

insert into user_id (id) values (default);

This also works if you have more columns, e.g:

insert into users (id, firstname, lastname) 
values (default, 'Arthur', 'Dent');

Or you can leave out the column list completely and request the default value(s) for all columns:

insert into user_id default values;

Upvotes: 7

Gordon Linoff
Gordon Linoff

Reputation: 1270973

SQL supports the default values statement.

So this will work:

create table t (id serial primary key);

insert into t 
    default values;

The syntax is described in the documentation.

Upvotes: 3

Related Questions