1.1创建用户
-- 创建用户
create user commandfight_db_user with password '123456';
-- 修改用户的密码
ALTER USER phonemonitor_db_user WITH PASSWORD '123456';
1.2创建数据库
-- 创建数据库
CREATE DATABASE commandfight_db OWNER commandfight_db_user;
-- 删除数据库
DROP database phonemonitor_db
-- 查询数据库
SELECT datname FROM pg_database
1.3序列使用
-- 创建序列
CREATE SEQUENCE public.t_capture_result_id_seq
INCREMENT BY 1
MINVALUE 1
MAXVALUE 9223372036854775807
START 1
CACHE 1
NO CYCLE;
-- 设置t_capture_result_temp的ID为自增序列
alter table t_capture_result_temp alter column id set default nextval('t_capture_result_id_seq');
-- 重置序列值
alter sequence t_capture_result_id_seq restart with 1
-- 删除序列
DROP SEQUENCE public.t_capture_result_id_seq;
1.3 常见函数的使用
-- 【聚合函数】string_agg 将结果集的某个字段所有行连接成字符串
-- 第一个参数为合并字段,第二个参数为分隔符
-- 可以用于分组合并
select id, string_agg(name, ',') from table5 group by id;
-- 【聚合函数】array_agg 与 string_agg 类似,但是它返回的是一个数组
select id, array_agg(name) from table5 group by id;
-- 【聚合函数】array_to_string 这个函数可以将数组类型数据转换成字符串
select id, array_to_string(array_agg(name), ',') from table5 group by id;
-- 【字符函数】-concat - 拼接字符串
select concat(id, name, '&&') from table8; -- 1001aladdin&&