2026/6/9 21:28:20
网站建设
项目流程
免费网站制造,佛山做网站的,用dede做网站后台,自学广告设计该怎么入手一、数据库操作1. 查询服务器版本-- 1.1 查询详细版本信息
select version();-- 1.2 查看版本信息#xff08;简洁版#xff09;
show server_version;-- 1.3 查看数字版本信息#xff08;含小版本号#xff09;
show server_version_num;
-- 或
select current_setting(se…一、数据库操作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;版本查询version() 查详细信息server_version_num 查数字版本可转整数适合版本兼容判断表操作核心创建表用 serial 实现自增清空数据优先用 truncate效率高修改字段约束前需清理不符合条件的数据实用工具 SQLpg_stat_activity 排查连接问题pg_tables/pg_class 查表元数据是日常运维高频使用的语句。有用请点赞养成良好习惯疑问、交流、鼓励请留言