Re: going crazy with serial type
От | Tom Lane |
---|---|
Тема | Re: going crazy with serial type |
Дата | |
Msg-id | 4990.1012509214@sss.pgh.pa.us обсуждение исходный текст |
Ответ на | Re: going crazy with serial type ("Gregory Wood" <gregw@com-stock.com>) |
Список | pgsql-general |
"Gregory Wood" <gregw@com-stock.com> writes: > [ lots of good advice snipped ] > Or explicitly give the serial a NULL value: > insert into mytable (NULL, 1, 2); > insert into mytable (NULL, 5, 6); Oops, struck out on your last at-bat :-(. The above will insert NULLs. The *only* way to get the default value inserted is not to specify the column at all in the insert. The way you suggested works: > insert into mytable (a, b) VALUES (1, 2); Another option is to arrange the columns of the table so that the one(s) you typically default are at the end, and then you can leave off the column name list in INSERT: create table mytable (a int, b int, id serial); insert into mytable values (1, 2); However, a lot of people consider it good practice to explicitly write out a column name list in every INSERT anyway. The above shortcut will come back to bite you if you ever rearrange the columns again. An INSERT with column names listed is relatively impervious to schema rearrangements. regards, tom lane
В списке pgsql-general по дате отправления: