尧图网站建设 尧图网络
  • 首页
  • 关于我们
  • 服务项目
  • 案例展示
  • 建站流程
  • 资讯中心
  • 联系我们
首页/资讯中心/详情

【PostgreSQL】常用SQL

【PostgreSQL】常用SQL
📅 发布时间:2026/6/18 22:36:56

一、数据库操作

1. 查询服务器版本

-- 1.1 查询详细版本信息 select version(); -- 1.2 查看版本信息(简洁版) show server_version; -- 1.3 查看数字版本信息(含小版本号) show server_version_num; -- 或 select current_setting('server_version_num'); -- 1.4 转换版本号为整数类型(便于数值比较) select current_setting('server_version_num')::integer;

2. 创建数据库

create database testdb;

3. 修改数据库

-- 重命名数据库 alter database testdb rename to new_name; -- 修改数据库最大并发连接数 alter database testdb connection limit 10; -- 修改数据库默认表空间 alter database testdb set tablespace new_tablespace;

4. 删除数据库

-- 安全删除(不存在则不报错) drop database if exists testdb;

5. 其他数据库相关查询

-- 查询所有数据库用户 select usename from pg_user;

二、表操作

1. 新建表

-- 创建基础表(含自增主键、默认值) create table student( id serial primary key, -- 自增主键(默认从1开始,步长1) name varchar(64) not null, -- 姓名,非空 age integer not null, -- 年龄,非空 sex integer not null, -- 性别,非空 createtime timestamp without time zone not null default now(), -- 创建时间,默认当前时间 updatetime timestamp without time zone -- 更新时间 ); -- 复制表结构+数据(仅复制数据和结构,不复制约束/索引) create table student_copy as select * from student;

2. 删除表

注意:原内容中delete table student是错误写法,正确写法如下:

-- 删除表(含结构+数据,不可逆) drop table if exists student; -- 仅清空表数据(保留结构) -- 方式1:逐行删除(可回滚,速度慢) delete from student; -- 方式2:快速清空(不可回滚,速度极快) truncate table student;

3. 查询表相关信息

-- 查询student表是否存在(方式1:通过系统表pg_class) select * from pg_class where relname = 'student' and relkind = 'r'; -- 查询student表是否存在(方式2:通过系统视图pg_tables,更直观) select * from pg_tables where tablename = 'student';

4. 修改表

4.1 表结构操作

-- 4.1.1 重命名表 alter table student rename to new_student; -- 4.1.2 添加字段(非空约束需确保已有数据符合条件) alter table student add column height integer not null; -- 4.1.3 删除字段 alter table student drop column sex; -- 4.1.4 重命名字段 alter table student rename column name to new_name; -- 4.1.5 查看/修改字段属性 -- a) 查询表所有字段的完整属性(名称、类型、非空、注释) select c.relname as table_name, col_description(a.attrelid, a.attnum) as column_comment, format_type(a.atttypid, a.atttypmod) as column_type, a.attname as column_name, a.attnotnull as is_not_null from pg_class as c, pg_attribute as a where a.attrelid = c.oid and a.attnum > 0 and c.relname = 'student'; -- b) 查询表中指定字段的属性 select c.relname as table_name, col_description(a.attrelid, a.attnum) as column_comment, format_type(a.atttypid, a.atttypmod) as column_type, a.attname as column_name, a.attnotnull as is_not_null from pg_class as c, pg_attribute as a where a.attrelid = c.oid and a.attnum > 0 and c.relname = 'student' and a.attname = 'name'; -- c) 修改字段类型(int4→int8,无数据冲突时) alter table student alter column sex type bigint; -- d) 强制转换字段类型(处理空值/非数值文本) alter table student alter column name type integer using (trim(name))::integer; -- e) 增加/删除字段约束 -- e1 非空约束 -- 增加非空约束(需先确保字段无NULL值) delete from student where updatetime is null; -- 清理不符合约束的数据 alter table student alter column updatetime set not null; -- 删除非空约束 alter table student alter column updatetime drop not null; -- e2 检查约束(自定义条件) delete from student where age <= 3; -- 清理不符合条件的数据 alter table student add constraint ck_student_check_age check(age > 3); -- 添加检查约束 -- 删除检查约束 alter table student drop constraint ck_student_check_age; -- e3 唯一约束(多字段组合唯一) alter table student add constraint uk_student_unique_name_age unique(name,age); -- 删除唯一约束 alter table student drop constraint uk_student_unique_name_age;

4.2 表记录操作

-- 4.2.1 插入记录 -- 插入单条记录 insert into student (name, age, sex, createtime, updatetime) values('Tom', '18', 1, '2018-11-29 17:00:02', '2018-11-29 17:00:02'); -- 从其他表批量插入符合条件的记录 insert into student1 select * from student2 where age > 18; -- 4.2.2 删除记录 -- 删除指定条件记录 delete from student where id = 1; delete from student where age > 18; delete from student where createtime <= '2018-01-01 00:00:00'; -- 4.2.3 查询记录 -- 查询全部记录 select * from student; -- 查询符合条件的指定字段 select name, age, sex from student where age > 18; -- 查询数据库连接信息(排查连接/锁问题) select * from pg_stat_activity; -- 包含客户端user、IP、执行语句、状态、耗时等 -- 4.2.4 修改记录 -- 更新符合条件记录的更新时间(保留到秒) update student set updatetime = date_trunc('second', now()) where age = 18;
  1. 版本查询:version() 查详细信息,server_version_num 查数字版本(可转整数),适合版本兼容判断;
  2. 表操作核心:创建表用 serial 实现自增,清空数据优先用 truncate(效率高),修改字段约束前需清理不符合条件的数据;
  3. 实用工具 SQL:pg_stat_activity 排查连接问题,pg_tables/pg_class 查表元数据,是日常运维高频使用的语句。

有用请点赞,养成良好习惯!

疑问、交流、鼓励请留言!

相关新闻

  • 【PostgreSQL】常用SQL
  • 基于python的口腔诊所门诊管理系统的设计与实现_e47798hi
  • 【PostgreSQL】日常总结

最新新闻

  • M2.7自主进化:AI生长体的元认知闭环与企业级沙盒治理
  • MC68HC16Y3微控制器架构解析:CPU16、TPU、ADC与系统设计实战
  • 当前需要AI解决的问题
  • 可编辑的pdf转ppt免费工具?2026免费888PDF转换器PDF转PPT可编辑实测 - 工具测试专家
  • 2026年洛阳西工TOP5不坑人电器门店,凭啥成为市民首选?
  • Fcitx5-android输入法框架架构深度解析:模块化设计的艺术与实践

日新闻

  • 2026年不锈钢卷板厂家推荐排行榜:冷轧热轧/304/201不锈钢卷板,高颜值耐腐蚀源头厂家实力精选 - 企业推荐官【官方】
  • FLUX.1-dev FP8模型实战指南:24GB以下显卡高效部署方案
  • 2026佛山长途搬家价目表:跨省跨市搬家费用完整计算指南 - 从来都是英雄出少年

周新闻

  • 3步解锁iOS设备:applera1n激活锁绕过完全指南
  • 39 2026 人工智能证书终极盘点,普通人选 AI 证书可以从这些方向入手
  • Redis 暴露公网有多危险?从端口检查到补救步骤

月新闻

  • 【总结】入门篇:50句话让你记住架构核心概念
  • WeChatMsg技术方案解析:实现Mac微信数据自主管理的完整解决方案
  • WeChatMsg:革新性微信数据备份方案,打造你的专属数字记忆库

关于尧图

  • 公司简介
  • 团队介绍
  • 企业文化
  • 荣誉资质

服务项目

  • 定制开发
  • 电商建站
  • UI 设计
  • 运维服务

快速链接

  • 案例展示
  • 建站流程
  • 常见问题
  • 资讯中心

联系方式

  • 📍北京市朝阳区互联网产业园 A 座 10 层
  • 📞400-888-8888
  • ✉️contact@rkmt.cn
  • 🕐周一至周日 9:00-21:00

© 2024 北京尧图网络科技有限公司 版权所有 | 京 ICP 备 XXXXXXXX 号