在 PostgreSQL 中,如何插入只有一个标识列的表?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12336789/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
In PostgreSQL how do you insert into a table with only one identity column?
提问by Charagh Jethnani
For instance:
例如:
{create table Participant ( id serial, primary key(id) );}
How do you insert into table in this case?
在这种情况下如何插入表?
回答by Akash KC
If you create the table like above,
如果你像上面那样创建表格,
You can use default
in following way to insert:
您可以使用default
以下方式插入:
INSERT INTO Participant values(default);
Check out SQLFIDDLE.
查看SQLFIDDLE。
Another way to insert is:
另一种插入方式是:
INSERT INTO Participant values(NEXTVAL('Participant_id_seq'));
CREATE TABLE
will create implicit sequence "Participant_id_seq"
for serial column "Participant.id"
.
CREATE TABLE
将为"Participant_id_seq"
串行列“创建隐式序列” Participant.id"
。
You can get the sequence for the table using pg_get_serial_sequence
function in following way:
您可以通过pg_get_serial_sequence
以下方式使用函数获取表的序列:
pg_get_serial_sequence('Participant', 'id')
It will take new value from sequence using NEXTVAL()
.
它将使用从序列中获取新值NEXTVAL()
。
Check out SQLFIDDLE
回答by user673207
insert into Participant values (default);