向表中插入数据
表创建完成后,表中没有数据,可以使用insert语句向表中插入一行或多行数据。
通过“\h insert”可以得到insert命令的帮助信息:
CREATE OR REPLACE FUNCTION mydist(id integer, value integer) RETURNS integer
AS
$$
BEGIN
RETURN ( id + value ) % 2;
END;
$$
LANGUAGE plpgsql
IMMUTABLE
STRICT;
CREATE TABLE test(id integer not null, value integer not null);
postgres=# \d+ test
Table "public.test"
Column | Type | Collation | Nullable | Default | Storage | Stats target | Description
--------+---------+-----------+----------+---------+---------+--------------+-------------
id | integer | | not null | | plain | |
value | integer | | not null | | plain | |
常用的insert语法包括:
- insert into table vaules (); 每次一行数据
- insert into table vaules (),(),(),()....; 每次多行数据
- insert into table select * from table2; 从其他表导入数据
- insert into t_count values (1,'a') ON CONFLICT (id) do update set name='b' ; insert的同时处理冲突
删除表中的数据
delete 用于删除表中的数据,语句比较简单:
postgres=# \h delete
Command: DELETE
Description: delete rows of a table
Syntax:
[ WITH [ RECURSIVE ] with_query [, ...] ]
DELETE FROM [ ONLY ] table_name [ * ] [ [ AS ] alias ]
[ USING using_list ]
[ WHERE condition | WHERE CURRENT OF cursor_name ]
[ RETURNING * | output_expression [ [ AS ] output_name ] [, ...] ]
在使用的时候,如果对执行语句不是很确定,可以将语句放在事务中执行,因为‘psql‘会自动提交:
postgres=# begin;
BEGIN
postgres=# select * from t_count ;
id | name
----+------
1 | c
(1 row)
postgres=# delete from t_count where id=1;
DELETE 1
postgres=# rollback;
ROLLBACK
postgres=# select * from t_count ;
id | name
----+------
1 | c
(1 row)
注意:
- 尽量避免在生产环境对全表进行delete操作。
- 如果delete的数据量比较大,可以考虑使用新建表的形式将不删除的数据放入新表,然后删除旧表,重命名新表。
更新表中的数据
update用于更新表中的数据,帮助信息如下:
postgres=# \h update
Command: UPDATE
Description: update rows of a table
Syntax:
[ WITH [ RECURSIVE ] with_query [, ...] ]
UPDATE [ ONLY ] table_name [ * ] [ [ AS ] alias ]
SET { column_name = { expression | DEFAULT } |
( column_name [, ...] ) = [ ROW ] ( { expression | DEFAULT } [, ...] ) |
( column_name [, ...] ) = ( sub-SELECT )
} [, ...]
[ FROM from_list ]
[ WHERE condition | WHERE CURRENT OF cursor_name ]
[ RETURNING * | output_expression [ [ AS ] output_name ] [, ...] ]
update与delete语句一样,可以通过条件控制来更新单条数据和多条数据。
注意:
- 尽量避免在生产环境对全表进行update操作。
- 如果update的数据量比较大,可以考虑使用新建表的形式将数据的新值放入新表,未更新的数据也放入新表中,然后删除旧表,重命名新表。
查看表中的数据
select 语句的用法比较丰富,配合本系统中的函数、表关联、group by、order by等关键字,高效完成用户的查询需求。
常用select语句:
select * from t;
select t1.col1,t1.col2 from t t1;
select t1.col1,t1.col2 from t t1 where t1.col1 < 10
select t1.col1,t2.col2 from a t1, b t2 where t1.id=t2.id;
select t1.col1,count(*) from a t1 group by t1.col1;
select t1.col1,count(*) from a t1 group by t1.col1 order by 2;
还可以将子查询与不同的子句一起使用,例如 SELECT、FROM、WHERE和HAVING 子句,与SELECT、INSERT、UPDATE和DELETE命令以及不同的运算符(如<、>、=、<=、>=、 BETWEEN、IN等)结合使用。
SELECT c1.car_name, c1.car_model,
(SELECT MIN (car_id)
FROM car c2
WHERE c1.car_id = c2.car_id) Subquery1
FROM car c1;
INSERT INTO employee
(phone, address)
SELECT phone, address
FROM department
WHERE dept_id < 5
AND department_name = 'OPERATION';
关于AntDB数据库
AntDB数据库始于2008年,在运营商的核心系统上,为全国24个省份的10亿多用户提供在线服务,具备高性能、弹性扩展、高可靠等产品特性,峰值每秒可处理百万笔通信核心交易,保障系统持续稳定运行近十年,并在通信、金融、交通、能源、物联网等行业成功商用落地。




