扫码一下
查看教程更方便
postgresql insert into 语句用于向表中插入新记录。我们可以插入一行也可以同时插入多行。
insert into 语句语法格式如下:
insert into table_name (column1, column2, column3,...columnn)
values (value1, value2, value3,...valuen);
在使用 insert into 语句时,字段列必须和数据值数量相同,且顺序也要对应。
如果我们向表中的所有字段插入值,则可以不需要指定字段,只需要指定插入的值即可:
insert into table_name values (value1,value2,value3,...valuen);
下表列出执行插入后返回结果的说明:
序号 |输出信息 | 描述 1 |insert oid 1 |只插入一行并且目标表具有 oid的返回信息, 那么 oid 是分配给被插入行的 oid。 2 |insert 0 #|插入多行返回的信息, # 为插入的行数。
在 jiyik_db 数据库中创建 company 表:
jiyik_db=# create table company(
id int primary key not null,
name text not null,
age int not null,
address char(50),
salary real,
join_date date
);
在 company 表中插入以下数据:
jiyik_db=# insert into company (id,name,age,address,salary,join_date) values (1, 'paul', 32, 'california', 20000.00,'2001-07-13');
insert 0 1
以下插入语句忽略 salary 字段:
jiyik_db=# insert into company (id,name,age,address,join_date) values (2, 'allen', 25, 'texas', '2007-12-13');
insert 0 1
以下插入语句 join_date 字段使用 default 子句来设置默认值,而不是指定值:
jiyik_db=# insert into company (id,name,age,address,salary,join_date) values (3, 'teddy', 23, 'norway', 20000.00, default );
insert 0 1
以下实例插入多行:
jiyik_db=# insert into company (id,name,age,address,salary,join_date) values (4, 'mark', 25, 'rich-mond ', 65000.00, '2007-12-13' ), (5, 'david', 27, 'texas', 85000.00, '2007-12-13');
insert 0 2
使用 select 语句查询表格数据:
jiyik_db=# select * from company;
结果如下:
id name age address salary join_date
---- ---------- ----- ---------- ------- --------
1 paul 32 california 20000.0 2001-07-13
2 allen 25 texas 2007-12-13
3 teddy 23 norway 20000.0
4 mark 25 rich-mond 65000.0 2007-12-13
5 david 27 texas 85000.0 2007-12-13