数据库教程:PostgreSQL数据库事务插入删除及更新操作示例

insert使用insert语句可以向表中插入数据。创建一个表:create table productins(product_id char(4) not null, produ

insert

使用insert语句可以向表中插入数据。

创建一个表:

create table productins  (product_id      char(4)      not null,   product_name    varchar(100) not null,   product_type    varchar(32)  not null,   sale_price      integer      default 0,   purchase_price  integer      ,   regist_date     date         ,   primary key (product_id));  

向表中插入数据:

insert语句格式:

里面的(列1,列2,…)称为列清单;(值1,值2,…)称为值清单。列清单和值清单个数要保持一致。

insert into <表名> (列1,列2,...) values (值1,值2,...);
insert into productins (product_id, product_name, product_type, sale_price, purchase_price, regist_date) values ('0001', 't恤' ,'衣服', 1000, 500, '2222-09-20');

对表的所有列进行insert时,可省略列清单。从左到右将值清单里面数据赋给每一列。

insert into productins values ('0005', '铁锅', '厨房用具', 6800, 5000, '2222-01-15');

插入null,直接在值清单里面写null就行,前提是插入null的列不能设置not null约束。

insert into productins values ('0006', '勺子', '厨房用具', 500, null, '2222-09-20');

插入默认值:

前面创建productins表设置sale_price默认值为0。在创建表的时候,设定了默认值,使用insert语句插入默认值的方法如下。

--显式方法设置默认值  insert into productins values ('0007', '筷子', '厨房用具', default, 790, '2222-04-28');  

隐式方法设置默认值,在列清单和值清单里面,省略设置为默认值的列。

如果省略未设置为默认值的列,该列的值将被置为null。这一列如果是not null约束,将报错。

--隐式方法设置默认值  insert into productins (product_id, product_name, product_type, purchase_price, regist_date) values ('0007', '筷子', '厨房用具', 790, '2222-04-28');  

从其他表中复制数据:

创建一张和product结构相同的表。

create table productcopy  (product_id      char(4)      not null,   product_name    varchar(100) not null,   product_type    varchar(32)  not null,   sale_price      integer      ,   purchase_price  integer      ,   regist_date     date         ,   primary key (product_id));  

可以像下面把product表中数据插入到productcopy表里。insert语句里面的select语句,可以使用where子句、group by子句等等。

insert into productcopy (product_id, product_name, product_type, sale_price, purchase_price, regist_date)  select product_id, product_name, product_type, sale_price, purchase_price, regist_date    from product;  

使用包含group by子句的select语句进行插入:

创建一个以商品种类汇总的表。

create table producttype  (product_type        varchar(32)  not null,   sum_sale_price      integer      ,   sum_purchase_price  integer      ,   primary key (product_type));  

通过下面,得到一个根据商品种类分组的表,并且计算出每个种类的价格的和。

insert into producttype (product_type, sum_sale_price, sum_purchase_price)  select product_type, sum(sale_price), sum(purchase_price)    from product   group by product_type;  

PostgreSQL数据库事务插入删除及更新操作示例

delete

drop table语句,将表删除。

delete语句,删除表里面的数据。

delete语句的对象是行,不是列,无法只删除部分列的数据。

删除全部数据行:

格式  delete from <表名>;  例子  delete from product;  

删除部分数据行:

格式  delete from <表名>  where <条件>;  例子  delete from product  where sale_price >= 2000;  

update

update语句用于改变表中数据。

格式:

update <表名>  set <列名> = <表达式>;  

改变regist_date列的所有数据。

update product     set regist_date = '2222-02-02';  

修改后效果如下。

PostgreSQL数据库事务插入删除及更新操作示例

更新部分数据行:

update <表名>  set <列名> = <表达式>  where <条件>;  
update product     set sale_price = sale_price * 10   where product_type = '厨房用具';  

将列更新为null:

前提是这个列没有设置not null约束和主键约束。

update product     set regist_date = null   where product_id = '0008';  

同时更新多个列:

-- 使用逗号,所有dbms中均可使用  update product     set sale_price = sale_price * 10,         purchase_price = purchase_price / 2   where product_type = '厨房用具';  
-- 列表形式,在某些dbms中无法使用  update product     set (sale_price, purchase_price) = (sale_price * 10, purchase_price / 2)   where product_type = '厨房用具';  

事务

事务transaction,需要在同一处理单元中执行的一系列更新处理的集合。

有时候要对一个表进行多个处理。比如为了某件事,需要把a的价格增加,把b的价格减少,此时,多个处理是作为同一个处理单元执行的。这个时候就可以用事务来处理。

格式:

事务开始语句;  dml语句1;  dml语句2;  ...  事务结束语句;  

例子:

其中,用户需要明确指出事务的结束。结束事务的指令有commit、rollback。

commit,提交事务包含的更新处理。一旦提交,无法恢复事务开始前的状态。

rollback,取消事务包含的更新处理,相当于放弃保存,恢复事务开始前的状态。

事务在数据库连接建立时,已经悄悄开始。

不使用开始语句情况下,sql server、postgresql、mysql里面默认使用自动提交模式,每条sql语句就是一个事务。

oracle里面,是直到用户执行commit或者rollback,算一个事务。

--postgresql  begin transaction;      update product         set sale_price = sale_price + 1000       where product_name = 't恤';      update product         set sale_price = sale_price - 1000       where product_name = '裤子';  commit;  

dbms的事务遵循acid特性。

原子性atomicity,事务结束时,其中的更新处理要么都执行(commit),要么都不执行(rollback)。

一致性consistency,事务中的处理,要满足数据库设置的约束,如主键约束、not null约束。

隔离性isolation,不同事务之间互不干扰。一个事务向表中添加数据,没提交前,别的事务看不到新添加的数据。

持久性durability,事务结束后,该时间点的数据状态会被保存。如果由于系统故障数据丢失,也能用一些方法恢复。

以上就是postgresql事务的插入删除及更新操作示例的详细内容,更多关于postgresql事务插入删除更新的资料请关注<计算机技术网(www.ctvol.com)!!>其它相关文章!

需要了解更多数据库技术:PostgreSQL数据库事务插入删除及更新操作示例,都可以关注数据库技术分享栏目—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/dtteaching/1086952.html

(0)
上一篇 2022年4月30日
下一篇 2022年4月30日

精彩推荐