【数据库】INSERT,DELETE,UPDATE,事务

4.1 INSERT
4.1 INSERT插入一行数据:

INSERT INTO ProductIns(product_id,product_name,product_type,sale_price,purchase_price,regist_date) VALUES('0001','T恤衫','衣服',1000,500,'2009-09-20');
INSERT 0 1

4.3 全列插入时,列清单可以省略

postgres=# INSERT INTO ProductIns VALUES('0004','菜刀','厨房用具',3000,2800,'2009-09-20');
INSERT 0 1

4.6显式插入默认值:

postgres=# INSERT INTO ProductIns VALUES('0007','擦菜板','厨房用具',DEFAULT,790,'2009-04-28');

4.7隐式插入默认值,省略插入默认值的那一列。

INSERT INTO ProductIns (product_id,product_name,product_type,purchase_price,regist_date) VALUES('0007','擦菜板','厨房用具',790,'2009-04-28');

注意:省略了没有设定默认值的列,就会设置为NULL,如果是NOT NULL类型,就会报错。
4.10将一个表的内容复制到另一个表

postgres=# INSERT INTO ProductCopy (product_id,product_name,product_type,sale_price,purchase_price,regist_date)
postgres-# SELECT product_id,product_name,product_type,sale_price,purchase_price,regist_date
postgres-# FROM Product;
INSERT 0 8

4.12插入部分数据并分组

postgres=# INSERT INTO ProductType(product_type,sum_sale_price,sum_purchase_price)
postgres-# SELECT product_type,SUM(sale_price),SUM(purchase_price)
postgres-# FROM product
postgres-# GROUP BY product_type;
INSERT 0 3
postgres=# SELECT * FROM ProductType;
 product_type | sum_sale_price | sum_purchase_price
--------------+----------------+--------------------
 衣服         |           5000 |               3300
 办公用品     |            600 |                320
 厨房用具     |          11180 |               8590
(3 行记录)

4.2DELETE
删除整个表:DROP
删除表内的数据:DELETE
4.13清空Product表:

postgres=# DELETE FROM Product;
DELETE 8

4.14 DELETE+WHERE删除特定行

postgres=# DELETE FROM Product
postgres-# WHERE sale_price>=4000;
DELETE 0

注:TRUNCATE舍弃,删除全表,不加条件,速度快。
4.3UPDATE更新
4.15 更新登记日期

postgres=# UPDATE Product
postgres-# SET regist_date='2009-10-10';

4.16 有条件的更新数据UPDATE+WHERE

postgres=# UPDATE Product
postgres-# SET sale_price=sale_price*10
postgres-# WHERE product_type='厨房用具';

注:UPDATE语句可以将数据更新为NULL
4.4事务
定义:一系列更新处理的集合
4.21更新商品信息的事务:

postgres=# BEGIN TRANSACTION;
BEGIN
postgres=# UPDATE Product
postgres-# SET sale_price=sale_price-1000
postgres-# WHERE product_name='运动T恤';
UPDATE 0
postgres=# UPDATE Product
postgres-# SET sale_price=sale_price+1000
postgres-# WHERE product_name='T恤衫';
UPDATE 0
postgres=# COMMIT;
COMMIT

4.22 事务回滚:

postgres=# BEGIN TRANSACTION;
BEGIN
postgres=# UPDATE Product
postgres-# SET sale_price=sale_price+1000
postgres-# WHERE product_name='T恤衫';
UPDATE 0
postgres=# ROLLBACK;
ROLLBACK

注:ROLLBACK回滚,放弃保存操作
事务的四种特性:ACID
atomicity原子性:事务要么全部执行,要么不执行
consistency一致性:满足约束条件
isolation隔离性:不同事务互不干扰
durability持久性:提前保存以免发生故障

练习:
4.1 除了Oracle,其他数据每条SQL语句都是一个事务,所以不用COMMIT也可以

postgres=# BEGIN TRANSACTION;
BEGIN
postgres=# INSERT INTO Product VALUES ('0001', 'T恤', '衣服', 1000, 500, '2009-09-20');
INSERT 0 1
postgres=# INSERT INTO Product VALUES ('0002', '打孔器', '办公用品', 500, 320, '2009-09-11');
INSERT 0 1
postgres=# INSERT INTO Product VALUES ('0003', '运动T恤', '衣服', 4000, 2800, NULL);
INSERT 0 1
postgres=# SELECT * FROM Product;
 product_id | product_name | product_type | sale_price | purchase_price | regist_date
------------+--------------+--------------+------------+----------------+-------------
 0001       | T恤          | 衣服         |       1000 |            500 | 2009-09-20
 0002       | 打孔器       | 办公用品     |        500 |            320 | 2009-09-11
 0003       | 运动T恤      | 衣服         |       4000 |           2800 |
(3 行记录)

4.4两个条件更新两次

postgres=# UPDATE ProductMagin
postgres-# SET sale_price=3000
postgres-# WHERE product_id='0003';
UPDATE 1
postgres=# UPDATE ProductMagin
postgres-# SET margin=sale_price-purchase_price
postgres-# WHERE product_id='0003';
UPDATE 1
postgres=# SELECT * FROM ProductMagin;
 product_id | product_name | sale_price | purchase_price | margin
------------+--------------+------------+----------------+--------
 0001       | T恤          |       1000 |            500 |    500
 0002       | 打孔器       |        500 |            320 |    180
 0003       | 运动T恤      |       3000 |           2800 |    200
(3 行记录)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值